views:

262

answers:

2

Hi all, I am using AtlasSpriteManager and AltasSprite to create frame by fram animation with 1 one file. I wanna write something that at first show a simple picture, without any animation and for example when I touch it, it shows some animation and return to the first position and frame. I just can't show the first frame without animation using this code :

Sprite *checker = [Sprite spriteWithFile:@"test.png"];
float frameWidth = [checker boundingBox].size.width / frameNumber;
float frameHeight = [checker boundingBox].size.height;

AtlasSpriteManager *mgr = [AtlasSpriteManager spriteManagerWithFile:fileName];
AtlasSprite *sprite = [AtlasSprite spriteWithRect:CGRectMake(pos.x, pos.y, frameWidth, frameHeight) spriteManager:mgr];
sprite.position = ccp(pos.x, pos.y);
[mgr addChild:sprite];

[layer addChild:mgr];

AtlasAnimation* animation = [AtlasAnimation animationWithName:@"Animation" delay:delay];
assert( animation != nil );

for(int i = 0; i < frameNumber; i++){
 [animation addFrameWithRect:CGRectMake(i * frameWidth, 0, frameWidth, frameHeight)];  
}

id action = [Animate actionWithAnimation:animation];
assert( action != nil );

id repeatAction;
if(repeatNumber == 0){
 repeatAction = [RepeatForever actionWithAction:action];
}else{
 repeatAction = [Repeat actionWithAction:action times:repeatNumber];
}

[sprite runAction:repeatAction];

any idea how to do that? thanx in advance

A: 

Updated Answer

I don't think I understood your question as you intended it.

As I understand now, you have a sprite whih you have an animation for. But when the animation is finished, it doesn't return to the frame that you want the sprite to be set to when standing still.

In our code you use actionWithAnimation:, have you tried setting that to actionWithAnimation:restoreOrigionalFrame: and then set restoreOrigionalFrame part to YES?

That should render the animation, but then when it stops, return to the frame it was before the animation.

Alternatively, you can make the action run, then when it stops, return to a certain frame manually by calling setTextureRect: on the AtlasSprite.

[sprite setTextureRect:CGRectMake( x, y, width, height )];

below this marker is my old answer:


The code that you have now will animate it immediately.

If you want the animation to start on touch, you'll have to check for touches. Then add the code to start the animation in the method that receives the touch.

There is sample code on how to use touches in the cocos2d download.

Basically: make your sprite a TargetedTouchDelegate (or create a new object to do that, but that's a bit of a hassle) and implement -(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event

nash
thanx for your reply, but my problem is not touching. The problem is for example A person stands without any movement. when I touch it, it starts to move and 2 secs later it stops and be still. I don't want to use a seprate sprite for showing the person without movement and when I touch it, I set its display to none and start the animation and at the end again show the person. If I do that, I need extra sprite for non moving objects.
Ali Bozorgkhan
thanx for your relpy, I found my problem. I was giving wrong coordinates to my AtlasSprite. thanx.
Ali Bozorgkhan
A: 

I think that you need CallFunc action combined with Sequence action.

From your code:


}
    .....
    .....
    id action = [Animate actionWithAnimation:animation];
    assert( action != nil );

    id repeatAction;
    if(repeatNumber == 0){
            repeatAction = [RepeatForever actionWithAction:action];
    }else{
            repeatAction = [Repeat actionWithAction:action times:repeatNumber];
    }
    id actionCallFunc = [CallFunc actionWithTarget:self selector:@selector(resetSprite)];
    id actionSequence = [Sequence actions: repeatAction, actionCallFunc, nil];

    [sprite runAction:repeatAction];
    ....
}

-(void)resetSprite{
    [sprite runAction:repeatAction];
}

First will be executed your repeatAction and when it ends the actionCallFunc will be executed calling resetSprite method where you can do what you want with your sprite.

Aram