Is onEnter() a predefined class method by Cocos2dX?

My feedback
First of all, hope you have a valid reason for implementing your sprite that way or may be you’re doing it this way to learn.
Below comments are feedback on your code that I am sending right away.
After reading this see, notes below.

Anyways,
Here is what I got after reviewing your code together

  1. You’re doing
    this->addChild(_qBox,101);
    qBox.pushBack(_qBox);

What is happening here?
Adding into a vector using pushback creates a copy of the object and not reference. (if I am not mistaken).

So, you’re actually adding the sprite which get added to the layer but you cannot control it afterwards because after the for loop is over then your _qBox variable for each one of sprite is lost. By control, I meant ‘_qBox’

Still you have given a bit incomplete code on how are you managing touch and those sprites inside that Vector.
So, I am assuming that you’re trying to get touches on the sprites inside vector which are never added to the layer.

Just fix this issue. If problem still persists then I would be happy to spend more time…

More things to Note:
Your onEnter() is not getting executed when you’re creating a sprite as

And yeah, you don’t have to bother on calling onEnter… It will be called automatically.
You just have to take care on overiding it.

So, as of now… let me review the rest of the pieces.
And now you must have got the reason that why touch is not being detected on the sprites that you’ve added inside your vector… But still I am onto checking why onEnter didn’t work when you created sprite the above way…

:smile:

No… you don’t have to do so…
I already mentioned that you can attach the event listeners to individual nodes however it is not recommended if there are too many nodes on your scene on which you’re attaching… But in your case, it doesnot seems to be too many nodes.

hey @catch_up check the latest code I posted. It’s my complete implementation. from my CCqBox.h to CCqBox.cpp class to its implementation in Gamescene.cpp

I did not use a Vector this time. As you can see I only created one image as to avoid confusion how I handle my Vectors. (i want to focus on the touch events problem)

CCqBox* sampleTile = (CCqBox*)CCqBox::create( “questionbutton.png”);
sampleTile->setPosition(Vec2(visibleSize.width/2, visibleSize.height * .41));
addChild(sampleTile,200);

after creating CCqBox ojbect, it still didnt have a touch functionality. :frowning:

Hi… I will shortly change the way you created the sprite…
I think I got why your onEnter is not getting called…
Just give me sometime

1 Like

Yeh, i think the problem is the way I create my class Sprite. back in v2, I also had my own function of making a sprite object:

goes like this:

CCTiles.cpp // my Tiles class

CCTiles* CCTiles::createTileWithFileName(const char *fileName){
//create an object
CCTiles *sprite = new CCTiles();
//if object created…

if (sprite && sprite->initWithSpriteFrameName(fileName)) {  // <-- gives error in cocos2dx v3
   
  //  sprite->Reset();
    //memory management o perations :)
    sprite->autorelease();
    //show object in the screen
    return sprite;
}
//if up condition has mistakes, safely return NULL
CC_SAFE_DELETE(sprite);
return NULL;

}

In my GameScene.cpp

CCTiles *sampleTile = CCTiles::createTileWithFileName(“picture.png”);

and that would work. But now in V3 ‘initWithSpriteFrameName’ gives error. so I removed that class function

Just a small change in your code…
(This derives from what I suggested earlier… that when we use constructor we use new and when we use onEnter() we don’t use new…)

You’re creating a sprite as

Instead just create the object of the class CCqBox… where you’re writing above piece of code.
So, now when you just create the object of the class it will automatically call onEnter().
Now, inside onEnter() you must do new create sprite of that class… and attach event handlers and whatever you’ve done already… Now where you’re creating the object of this class… after the object is instantiated… You’ll now have this object containing sprite… Now set its position and add it to the layer…

It should work…

Slightly different way of implementing it…
But this it to be used in cases where you’ve multiple layers and these layers are not having implementation of creating scene inside them… I mean a scene created differently and then adding the layers one by one… I’ll tell you the reason shortly…
Now, if you’re not comfortable in setting position of this sprite where you’re actually creating it then you can do one more thing… You’ve left the constructor empty… Utilize this, just pass the layer while creating the object I told you in above way… Now, you can use this layer inside onEnter()…
Note… you cannot pass your layer for constructor while creating an object because you’re probably creating object inide init() of the layer and since your init() has not complete so we cannot pass layer as layer is actually not created…

I am quite sure creating sprite this way should work…
For now… just see the above way… This below way will not bother now…

If still a problem, you can ask…

If you’re a bit puzzled with the explaination I can write the code for you… I could have done that but I thought it would be better if you understand this thing…

i think i kinda grasp the idea… wait…will give you feedback, just a moment :smile:

Sure… no problem…
If you’re able to write the code, then fine…
Otherwise, I’ll give you…

:smiley:

can i take a look at your code snippet ?
im trying to make my v2 function work but to no avail

CCTiles.cpp // my Tiles class

CCTiles* CCTiles::createTileWithFileName(const char fileName){
//create an object
CCTiles sprite = new CCTiles();
//if object created…

if (sprite && sprite->initWithSpriteFrameName(fileName)) { // <-- gives error in cocos2dx v3

// sprite->Reset();
//memory management o perations :slight_smile:
sprite->autorelease();
//show object in the screen
return sprite;
}
//if up condition has mistakes, safely return NULL
CC_SAFE_DELETE(sprite);
return NULL;
}

@catch_up

Hey man, I got a working code. Found it in a site and it seems to be working for me. onEnter() runs automatically ! You’re so right :smile: my CCLOG inside onEnter() did execute! yey! :smile:

CCqBox* CCqBox::create( const std::string& filename){

//create an object
CCqBox *sprite = new(std::nothrow) CCqBox();
//if object created..

if (sprite && sprite->initWithFile(filename) ) {  

    sprite->autorelease();
    //show object in the screen
    return sprite;
}

CC_SAFE_DELETE(sprite);
return NULL;

}

this method allows me to create a sprite in my GameScene.cpp

but when I press the sprite, still nothing :frowning:

I don’t have code as such… I could write it…
Just to add more… I used onEnter with my cocos2dJS Game…
The aim is to be clear with what you’re using… So, I use whatever I feel like either with cocos2d-x/JS…

The code I wrote is as follows:

In your Gamescene.cpp

CCqBox sampleTile = new CCqBox::CCqBox(); // It will automatically call your onEnter() of the that class(which is extending sprite-node)
//CCqBox sampleTile = new CCqBox::CCqBox(“MySprite.png”); // Use this statement or above statement.
// Both of the above statement are like normal statement Sprite *sp= Sprite::create();

// After the above statement has been executed, now you have your sampleTile which will contain sprite of type CCqBox.
sampleTile->setPosition(Vec2(visibleSize.width/2, visibleSize.height * .41));
this->addChild(sampleTile,200);

make 2 constructors // This implementation requires to define atleast one constructor. There can be more than one way to implement with onEnter()

CqBox* CqBox::CCqBox(void)
{
this->initWithSpriteFrameName(“MyDefaultSprite.png”)
}

CqBox* CqBox::CCqBox(const char * spriteFile)
{
this->initWithSpriteFrameName(spriteFile);
}

CCqBox::onEnter()
{
CCLOG(“Cute Function”); //Check and please tell me whether there is any feedback from this CCLog
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(CCqBox::onTouchBegan, this); // this binds you to implement onTouchBegan inside CCqBox class.
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); // With ‘this’ keyword you’ve attached an event listener to this sprite-node

}

Also, are you trying to upgrade your v2 project to v3.?
Also you may have a look at
http://www.cocos2d-x.org/wiki/How_To_Subclass_Sprite_And_Add_Event_Listeners
could be of some help…

and I would like to know what error it gives at

Oh I didn’t see that you got the code :smiley:
great

The solution you got is similar as what I wrote …
In yours you’re overriding create function of the parent class…
Anyways, there are always many ways of doing same things :smiley:

Was just reading this and wanted to comment for any future readers - no, pushBack does not take a copy of the object - it’s a vector of pointers to sprites, not a vector of sprites.

(The site pointed out this was a 400+ day old post, and I’m sure you’ve realised this by now - but thought worth posting in case it confuses newcomers!

*and crucially Retains the sprite pointer

Thanks for putting that post. Yes, you’re correct.