CCSprite retain() problem

Hello everyone,

Thanks for a great forum. I am both new to using cocos2dx and C++ in general.
However during the last of days I have started to develop a game using these two tools.

Now I have run into a problem with (at least I think this is the problem) my sprites not being retained properly.
Below is my code:

bool GameSceneView::init()
{
>
if (!CCLayerColor::initWithColor(ccc4(255,255,255,255)))
{
return false;
}
>
*backgroundNode = CCParallaxNode::create;
this~~>addChild;
>
CCSprite* *grass = CCSprite::create;
>*grass~~>retain;
>*backgroundNode->addChild(*grass, 0, ccp, ccp);
CCLog.x);
>
return true;
}
>
>
void GameSceneView::update
{
CCLog.x);
}

The game crashes with the error “Access violation reading location 0xCDCDCDCD” when the update function is called and I am trying to log the x coordinate of the*grass sprite.
What am I doing wrong with regard to retaining my _grass sprite?!

Thanks for your help!

//fischer

You are assigning created sprite to a local pointer here:
CCSprite* grass = CCSprite::create;
In update method you have:
CCLog.x);
I don’t know what is this
grass in your update method but this is not the same _grass that you use in your init method.

inside update method, there is no variable by name grass in the scope.
declare the your
grass as class member. then _grass will have scope through all the class functions.
that will solve the error.

The other option you have is to add the sprite to the node with a tag and then retrieve it in the update using the same tag.
You wont need the grass var or thegrass->retain.

Thank you guys… It solved my problem :slight_smile:

Great and responsive forum!