AtlasSpriteManager动画
我正在使用 AtlasSpriteManager 和 AltasSprite 通过 1 个文件创建逐帧动画。我想写一些东西,首先显示一张简单的图片,没有任何动画,例如当我触摸它时,它会显示一些动画并返回到第一个位置和帧。我只是无法使用此代码显示没有动画的第一帧:
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];
知道如何做到这一点吗? 提前致谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要 CallFunc 操作与 Sequence 操作相结合。
从您的代码中:
首先将执行您的repeatAction,当它结束时,将调用resetSprite方法执行actionCallFunc,您可以在其中对您的精灵执行您想要的操作。
I think that you need CallFunc action combined with Sequence action.
From your code:
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.
更新的答案
我认为我没有按照您的意图理解您的问题。
据我现在了解,你有一个精灵,你有一个动画。但是当动画完成时,它不会返回到您希望精灵静止不动时设置的帧。
在我们的代码中,您使用
actionWithAnimation:
,您是否尝试将其设置为actionWithAnimation:restoreOrigionalFrame:
,然后将restoreOrigionalFrame
部分设置为 YES?这应该渲染动画,但是当它停止时,返回到动画之前的帧。
或者,您可以让动作运行,然后当它停止时,通过调用 AtlasSprite 上的
setTextureRect:
手动返回到特定帧。该标记下方是我的旧答案:
您现在拥有的代码将立即为其设置动画。
如果您希望动画在触摸时开始,则必须检查触摸。然后在接收触摸的方法中添加启动动画的代码。
cocos2d 下载中有关于如何使用触摸的示例代码。
基本上:使您的精灵成为
TargetedTouchDelegate
(或创建一个新对象来执行此操作,但这有点麻烦)并实现-(BOOL)ccTouchBegan:(UITouch *)touch withEvent: (UIEvent *)事件
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 toactionWithAnimation:restoreOrigionalFrame:
and then setrestoreOrigionalFrame
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.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