English 中文(简体)
AtlasSpriteManager animation
原标题:

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

最佳回答

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

问题回答

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.





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签