iphone - Cocos-2d 操作 -- CCallFunc 不执行任何操作

标签 iphone ios cocos2d-iphone

基本上,我正在尝试为 Sprite 制作动画。当向右移动时,我希望播放一个动画,当向左移动时,我希望播放另一个动画。这是我的代码——什么也没发生, Sprite 只是显示在屏幕上。

-(void)moveLeft{

[sprite stopActionByTag:0];

NSMutableArray *spriteFrames2 = [NSMutableArray array];

CCSpriteFrame *frame4  = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"KnightSpritesLeft10.png"];
CCSpriteFrame *frame5  = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"KnightSpritesLeft11.png"];
CCSpriteFrame *frame6  = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"KnightSpritesLeft12.png"];


[spriteFrames2 addObjectsFromArray:[NSArray arrayWithObjects:frame4,frame5,frame4,frame6, nil]];

CCAnimation *anim = [CCAnimation animationWithFrames:spriteFrames2 delay:0.15f];
CCAnimate *animate = [CCAnimate actionWithAnimation:anim];
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:animate];
repeat.tag = 1;
[sprite runAction:repeat];

id moveToLeft = [CCMoveTo actionWithDuration:3.0 position:CGPointMake(0, sprite.position.y)];
[sprite runAction:moveToLeft];



}
 -(void)moveRight{

[sprite stopActionByTag:1];

CGSize winSize = [[CCDirector sharedDirector]winSize];


NSMutableArray *spriteFrames = [NSMutableArray array];
CCSpriteFrame *frame1 = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"KnightSpritesRight6.png"];
CCSpriteFrame *frame2 = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"KnightSpritesRight4.png"];
CCSpriteFrame *frame3 = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"KnightSpritesRight5.png"];

[spriteFrames addObjectsFromArray:[NSArray arrayWithObjects:frame1,frame2,frame1,frame3, nil]];

CCAnimation* anim = [CCAnimation animationWithFrames:spriteFrames delay:0.15f];
CCAnimate* animate = [CCAnimate actionWithAnimation:anim]; 
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:animate];
repeat.tag = 0;
[sprite runAction:repeat];

id moveToRight = [CCMoveTo actionWithDuration:3.0 position:CGPointMake(winSize.width, sprite.position.y)];
[sprite runAction:moveToRight];


}

-(id) init
{



if( (self=[super init])) {


    [[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"KnightImages2.plist"];

    sprite = [CCSprite spriteWithSpriteFrameName:@"KnightSpritesRight6.png"];
    sprite.position = ccp(240, 180);

    [self addChild:sprite];

    id actionSequence = [CCSequence actions:[CCCallFunc actionWithTarget:self selector:@selector(moveRight)],[CCCallFunc actionWithTarget:self selector:@selector(moveLeft)], nil];
    CCRepeatForever *repeatForever = [CCRepeatForever actionWithAction:actionSequence];

    [sprite runAction:repeatForever];
}
return self;
}

最佳答案

您的设置方式是逐帧发生以下情况:

第 1 帧

  • 执行右移
  • 执行向左移动

第 2 帧

  • 执行右移
  • 执行向左移动

无限循环。

由于每次调用 moveRight 或 moveLeft 时都会重新启动动画,因此实际上什么也没有发生。您需要等待一段时间才能运行另一个动画才能实际播放。您可以为此使用 CCDelayTime 操作:

id actionSequence = [CCSequence actions:
  [CCCallFunc actionWithTarget:self selector:@selector(moveRight)],
  [CCDelayTime actionWithDuration:2],
  [CCCallFunc actionWithTarget:self selector:@selector(moveLeft)], 
  [CCDelayTime actionWithDuration:2],
  nil];

关于iphone - Cocos-2d 操作 -- CCallFunc 不执行任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8567643/

相关文章:

iphone - 如何使用 MapKit 跟踪用户位置

iphone - 对于prepareForSegue方法,实例变量为null

ios - 在cocos2d 2中有条件地定位和显示给定iphone不同尺寸的 Sprite

ios - iOS线程函数的参数

ios - 如何在不使用大量内存的情况下缩放过渡 UILabel?

从终端进行 iPhone 静态代码分析未能找到丢失的 ivar 版本

iphone - UITableView:将一行移动到空白部分

iPhone SDK 兼容性包括使用 __IPHONE_OS_VERSION_MIN_REQUIRED

iOS 7 : different navigation items for tab bar controller

iphone - 登录钥匙串(keychain)中的 APNs 证书看起来不同