Cocos2d iphone 移动精灵动画

Cocos2d iphone 移动精灵动画,cocos2d-iphone,Cocos2d Iphone,我有一个CoCo2D世界,一个快速移动的精灵/身体。 当接触发生时,我调用动画函数。 问题是,当动画在当前精灵位置运行时,精灵已转到其他位置,因此动画不在正确的位置: 我将如何运行此动画功能来跟随我的精灵 代码: -(void)动画:(NSString*)动画 { NSLog(@“check:%@”,动画); [[CCSpriteFrameCache sharedSpriteFrameCache]添加SpriteFrameSwithFile:[NSString stringWithFormat:

我有一个CoCo2D世界,一个快速移动的精灵/身体。 当接触发生时,我调用动画函数。 问题是,当动画在当前精灵位置运行时,精灵已转到其他位置,因此动画不在正确的位置:

我将如何运行此动画功能来跟随我的精灵

代码:

-(void)动画:(NSString*)动画
{
NSLog(@“check:%@”,动画);
[[CCSpriteFrameCache sharedSpriteFrameCache]添加SpriteFrameSwithFile:[NSString stringWithFormat:@“%@.plist”,动画]];
sprite=[CCSprite SPRITEWITHPRITEFRAMENAME:[NSString stringWithFormat:@“%@00000.png”,动画]];//从plist获取此图片的相关内容
sprite.position=boy.position;
//精灵位置=ccp(160175);
CCSpriteBatchNode*spriteSheet=[CCSpriteBatchNode batchNodeWithFile:[NSString stringWithFormat:@“%@.png”,动画]];
[spriteSheet addChild:sprite];//将此坐标从spriteSheet添加到屏幕
[自添加子项:精灵表];
NSString*Path=[[NSBundle mainBundle]bundlePath];
NSString*animPath=[Path stringByAppendingPathComponent:[NSString stringWithFormat:@“%@.plist”,动画]];
NSDictionary*animSpriteCoords=[[NSDictionary alloc]initWithContentsOfFile:animPath];
NSDictionary*animsframesdata=[animSpriteCoords objectForKey:@“frames”];
int b=0;
int a=0;
NSMutableArray*animFrames=[NSMutableArray];
对于(int i=1;i<[animFramesData count];i++)
{
a=a+1;
如果(a==10)
{
b=b+1;
a=0;
}
CCSpriteFrame*frame=[[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:[NSString stringWithFormat:@“%@_000%0i%1i.png”,动画,b,a]];/[NSString stringWithFormat:@“eye_blinking_0000%1d.png”,i]
[动画帧添加对象:帧];
}
//cAnimation*dollAnimation=[cAnimation animation];
cAnimation*dollAnimation=[cAnimation animationWithFrames:AnimationFrames延迟:0.1f];
//cAnimation*dollAnimation=[cAnimation Animation With Name:@“舞蹈”Animation With Frames:AnimationFrames];
//[设置延迟:0.1f];
CCAnimate*Action=[CCAnimate actionWithAnimation:dollAnimation];
id call=[CCCallFunc actionWithTarget:self-selector:@selector(finishAnimation)];
id序列=[CCSequence actions:Action,[CCHide Action],call,nil];
[精灵运行操作:序列];
}

太多了。

CCFollow将让动画精灵跟随男孩精灵:

id follow = [CCFollow actionWithTarget:boy];
[sprite runAction:follow];

另一种方法是以预定的更新方法将动画精灵的位置持续更新为男孩的位置(sprite.position=boy.position)。

谢谢您的回答,但精灵男孩已经有一系列动作,其中一个就是该动画。所以我做了这个:[精灵运行:序列];[精灵运行动作:跟随];动画出现了,但它跟在我儿子的另一个方向上-如果它向右,动画就会向左。。而且它也不在同一个位置上。。为什么呢?
id follow = [CCFollow actionWithTarget:boy];
[sprite runAction:follow];