Can a CCSprite switch between multiple animations? - cocos2d-iphone

Is it possible to make a CCSprite switch between two different animations? (both are CCRepeatForever)
I try to use stopAction: and runAction: but it crashes the app.
I can only use pauseSchedulerAndActions: and resumeSchedulerAndActions: with one animation.

To answer your question: Yes. If switching animations troubles you, use one sprite for each animation and keep them all updated to the same position, and only set the one to visible whose animation should be played.
The crash is certainly unrelated.

Related

Rendering issue regarding imagery versus functionality

As I understand rendering textures in SDL2, everything is waiting behind the scenes and a texture appears after using the SDL_RenderPresent() function and vanishes with SDL_RenderClear(), which you use before advancing to the next frame.
I understand that as far as it goes for imagery, but what about functionality? I have two button textures linked to mouse events that I want to see and use at different times in different places. I've got them rendering during different enum states and each button does indeed appear and disappear on cue when the states change.
However, since both button textures are always "there" even while not being rendered, I can still mouse click on the invisible button that isn't being rendered at any given time. This doesn't seem to be an issue for mouse motion events, just mouse button events. How do I make a texture inactive as well as invisible when it's not being rendered?
I solved this one with some tinkering and a more experienced programmer named mbozzi's help to clue me in the right direction as to what was going on. The underlying issue was due to my completely decoupling the GUI logic and GUI rendering. Which is what we are always told to do: decouple everything, right? But I needed to couple the logic and rendering that I want to occur at the same time and place.
My event poll>>mouse input>>image rendering code was one giant loop. However, when I split that giant loop into separate mini event poll>>mouse input>>image rendering loops that each runs independently (but not concurrently, I just put them in their own different enum game states), that clears up the issue. So, if anyone has a similar problem with clicking invisible buttons, hopefully this will help.

Replacing scenes when using a UITabBar

My application has four tabBarItems. Two of the tabBarItems use CCScene (Cocos2d) classes. I start on one scene which works well, and then I switch over to the second scene using the tabBar which also works well. My problem is that when I try to return to the original scene, my view is the first scene but the actual running scene hasn't changed from the second scene.
Is there a way to switch scenes when toggling on a tabBar?
Should each tabBarItem simply link to a CCLayer, and then have all those layers become children to a single CCScene object?
Any suggestions will be appreciated.

DIfference between a scene and a layer in cocos2d

can anyone please make me clear about the difference between a Scene & a Layer in COCOS2D. I am bugged up with this.
Buddy, it is very simple. Whenever you use COCOS2D, you will be having a class which extends Layer. And which you are going call from Main activity. So for one displayable screen, there can be only one layer. In the class(which extends Layer) can have any number of scenes. You can even set the dimensions for the individual scenes too. But, make sure that, you release the memory immediately after you switch from one scene to another... Enjoy... :)

How To Retrieve Actions From Sprite In cocos2d

I have a CCSprite that I'm using in a scene and have created multiple CCAnimation actions to apply to it all using a single CCSpriteFrameCache sharedSpriteFrameCache. While everything is working and I'm able to switch between animations, I feel like I'm doing poorly and would like to simplify my code by retrieving the running action(s) on the CCSprite to stop them individually before running the next action on it.
To help create some context, lets assume the following situation:
We have a CCSprite called mySprite
We have 3 separate CCAnimation actions defined for walking to the right, walking to the left, and sitting looking forward called: actionAnimWalkRight, actionAnimWalkLeft, and actionAnimSitForward respectively.
We want to have the sprite walk to the right when someone touches the screen right of mySprite, walk left when someone touches the screen left of mySprite and sit when someone touches mySprite.
The approach I'm using to accomplish this is as follows:
Place CCSprite as a child in the scene.
Tell the sprite to run an action using: [self runAction:actionWalkRight];
When I want to change the action after someone touches, I have a method called stopAllAnimationActions which I call before I apply a new action that stops any animation action no matter what's running. Basically lists ALL the CCAnimation/CCActions I have defined and stops each one individually since I don't want to use stopAllActions. as follows: [self stopAction:actionWalkRight]; [self stopAction:actionWalkLeft]; [self stopAction:actionSitForward];
Then I apply the new animation after the above method fires using: [self runAction:actionWalkLeft];
While this works, it just seems like a poor design to stop items that I know aren't running just because I don't know exactly what is running. So just looking for advice and the best recommended practice to do something like this within very complex situations so tracking every possible situation is difficult. Any feedback would be appreciated.
When creating the actions set the tag of that action with a constant:
actionWalkRight.tag= kCurrentAction;
[self runAction:actionWalkRight];
Then, retrieve the running action by that tag and stop it.
[self stopActionByTag:kCurrentAction];
I recommend you simplify your process and take advantage of the native Cocos features, including stopAllActions. Don't re-use actions, always create them from scratch as it has been well discussed among Cocos developers that re-using actions can be buggy.
Cocos is well optimized and features like stopAllActions are not performance hogs. It would probably be faster than your approach, actually.

Cocos2d change image of hero in accelerometer when an object hits it

Hi. I am new on this website and also in cocos2d. I am a student and I need your help.
I am making a game based on one of the tutorials in a cocos2d game development book. The concept is simple; different objects are falling from the top of the screen and I have to avoid or catch them by tilting the device. The main character, which is one which has to avoid objects, has different properties which can change by grabbing different objects (e.g. the player may have a shield if it grabs one). In order to display the shield I have to change the sprite of the player. I am not sure how I can achieve this. Could anyone help me in providing some guidelines on this?
Use setTexture to switch the image (texture) of your current sprite with another:
[playerSprite setTexture:[[CCTextureCache sharedTextureCache] addImage:#"playerWithShield.png"]];