2

I just started learning coco2dx for android and I have successfully configured my environment but I am facing few problems.

  1. 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.
  2. I am not able to get auto complete feature in eclipse for cocos2dx.
  3. 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 :)

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
user3389247
  • 271
  • 1
  • 3
  • 10

1 Answers1

1

Change

sprite->addChild(sprite, 1);

to

this->addChild(sprite, 1);
PWiggin
  • 956
  • 2
  • 12
  • 24
  • Hey thansk a lot :) it worked :) Any idea on remaining two problems? – user3389247 Mar 08 '14 at 05:47
  • 1
    I've long given up trying to code in that abomination of an IDE. I do all of my coding for cocos2d-x in Xcode and just compile in Eclipse to run the emulator. That being said, check http://stackoverflow.com/questions/6912169/eclipse-enable-autocomplete-content-assist and feel free to mark my answer as correct. – PWiggin Mar 08 '14 at 05:53
  • 2
    Thanks :) Though I dint get the ans for 2 and 3 problem I will still select this ans since its the only one ans till now and also my 1st prob was of my major concern :) – user3389247 Mar 08 '14 at 06:00