Help on handling events and updating a label.

Hello all,

First of all. I am new to cocos2d-x and c*+* development. I do have a background in .NET development.

I have done some testing and must say i am impressed with the framework. What I do miss however is some solid documentation / a starting point for development. But as i noticed people are working on that. Good job, thanks!

I have got a question regarding handling events .

My goal is to be able to detect when an image is touched. When the image is touched I want to perform some actions. For example:

  • update a label
  • change the image.

I am aware there is a thing like menuItem and MenuItemImage , however I feel those classes won’t do the trick for me on the long term. What i like here is that I can provide a menu_selector, how to implement such a feature myself?

At this moment i have made some tests, i extended the CCSprite class, added some logic to detect a touch and write this to the log. This works as expected.

This however does not allow me to update a label in the parent layer. Question is, how do i do this?

Let’s say we use the HelloWorld project in cocos2d-x 2.0.0.3 to describe my case.

Then II add the following code inside the body of CCScene* HelloWorld::scene()
START CODE:

`CCLayer *MyLayer = CCLayer::Create();

CCSprite *firstImage = CCSprite::Create(“myFirstImage.png”);
CCSprite *secondImage = CCSprite::Create(“mySecondImage.png”);
CCLabelTTF *myLabel = CCLabelTTF::Create(“Start clicking images to start count”, “Arial”, 16);

scene->AddChild(firstImage);
scene->AddChild(secondImage);
scene->AddChild(myLabel);`
END CODE

How do I update the label when an image is clicked in the scenario above?

I would like a solution similar to the following approach:

  • add a method to the HelloWorld class named (void?) anImageWasTouched(someParamType paramValue, CCNode pSender)

Assuming such a thing will allow me to cast the received pSender to my custom type, check some conditions and update the label.

Is there anybody who can help me out on this.

With kind regards

Matthijs

You can use the subclass CCMenuItem how base to create your CCMenuItemSprite objects.
Or add touch delegate in your CCSprite(but it’s is bad idea)

Misha, Thanks for the response. I tried the approach you suggested and so far it seems to work for me:)