Yes, declaring CCSprite in header so that you can access it in other functions of the class.
If you want to animate your sprite, use CCAnimate and run the action on sprite.
About autorelease, release and retain it took some time for me to understand too. This is what i understood.
Consider that your FPS is 60, that means that the scene is being drawn 60 times per second.
The this engine loop happens in two stages:
Stage 1: Draws all the objects on the screen. (Also running all your code including every function you scheduled, ever action you run on CCNode, all your AI calculation etc.)
Stage 2: It checks if there is any object in the pool that has reference count as 0 and delete those objects. Then go to stage 1.
The difference between autorelease and release is:
If you call autorelease on any CCObject, it just decrement reference count by 1. (referenceCount—) But it wont delete the object. It waits till the stage 1 is finished and in stage 2 the object is deleted if its reference count is 0.
But if you call release on any CCObject, it decrements reference count by 1, then checks if reference count is 0 and if it is 0, deletes the object then and there only. It wont wait for stage 1 to finish.
Calling retain will simply increment reference count, there by avoiding the object to be deleted in stage 2. So the object retains in the memory for your future uses.
Now that you know difference between autorelease and release lets go ahead.
When ever you call “create”, it returns you a “autoreleased object”, don’t get baffled by the word it just means object with reference count 0. Which means unless you do something to increase its reference count, you cant use the object in your next frame, stage 2 will deleted the object from memory. But don’t panic here and call retain, (!) avoid using retain and release as much as you can.
If calling retain is bad thing then how can you make object stay?
Well answer is pretty simple, if you add the node to another node (addChild), its retain count will increase by 1 and the moment you remove the node it decrements the reference count (removeChild). So in a way cocos2d-x is taking care of all memory management for you.
The only case i encountered, where you have to manually call retain and release is when you create a CCArray or CCPointArray. Since there are not nodes, you cant attach them to any other node. So when you create them you have to call retain on them.
one example explaining reference count:
CCArray *sampleArray = CCArray::create(); // reference count of sampleArray is 0
sampleArray->retain(); // reference count of sampleArray is 1
CCSprite *sampleSprite = CCSPrite::create("sampleImage.png"); // reference count of sampleArray is 1, reference count of sampleSprite is 0
sampleArray->addObject( sampleSprite ); // adding sprite to array, reference count of sampleArray is 1, reference count of sampleSprite is 1
this->addChild( sampleSprite ); // adding sprite to scene, reference count of sampleArray is 1, reference count of sampleSprite is 2
this->removeChild( sampleSprite ); // removing sprite from scene, reference count of sampleArray is 1, reference count of sampleSprite is 1
// even though sampleSprite is removed from scene its reference count is still 1, so it stays in memory. you can access it again using the array
sampleArray->removeObject( sampleSprite ); // removing sprite from array, reference count of sampleArray is 1, reference count of sampleSprite is 0
// at this stage there is no sampleSprite anymore, it is deleted.
Hope this clears it out