gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:52:38 25 4
gpt4 key购买 nike

基本上,我正在尝试为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com