I just started learning coco2dx for android and I have successfully configured my environment but I am facing few problems.
- I am adding a sprite when I touch on my device. The touch event is getting detected but I am not able to add the sprite on my screen.
- I am not able to get auto complete feature in eclipse for cocos2dx.
- I am getting a syntax error on my header file ('cocos2d.h' in HelloWorld.h and 'USING_NS_CC' in my HelloWorld.cpp). But I get no errors on compiling.
Below is my code:
HelloWorld.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayer
{
public:
virtual bool init();
static cocos2d::CCScene* scene();
void menuCloseCallback(CCObject* pSender);
void ccTouchesBegan(cocos2d::CCSet* pTouches, cocos2d::CCEvent* pEvent);
LAYER_CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorld.cpp
#include "HelloWorldScene.h"
USING_NS_CC;
CCScene* HelloWorld::scene()
{
CCScene *scene = CCScene::create();
HelloWorld *layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if ( !CCLayer::init() )
{
return false;
}
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCSprite* bg = CCSprite::create("moles_bg.png");
bg->setPosition( ccp(size.width/2, size.height/2) );
this->addChild(bg, -1);
float rX = size.width / bg->getContentSize().width;
float rY = size.height / bg->getContentSize().height;
bg->setScaleX(rX);
bg->setScaleY(rY);
this->setTouchEnabled(true);
return true;
}
void HelloWorld::ccTouchesBegan(cocos2d::CCSet* pTouches, cocos2d::CCEvent* pEvent)
{
CCTouch* touch = (CCTouch* )pTouches->anyObject();
CCPoint location = touch->locationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
CCSprite* sprite = CCSprite::create("moles_icon.png");
sprite->setPosition(location);
sprite->addChild(sprite, 1);
//This is shown in my log cat
CCLog("Sprite Touched");
}
My environment configuration is as follow:
- OS: Win7
- Cocos2d-x Ver: cocos2dx 2.0.1
- Eclipse: From here (dont know which version it is).
Any help on above mentioned problems will be greately appreciated :)