Действия Cocos-2d - CCallFunc ничего не делает - PullRequest
0 голосов
/ 20 декабря 2011

По сути, я пытаюсь оживить спрайт.При движении вправо я хочу воспроизвести одну анимацию, а при движении влево - другую.Вот мой код - вообще ничего не происходит, спрайт просто отображается на экране.

-(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 Ответ

1 голос
/ 20 декабря 2011

Способ, которым вы настроили это, состоит в следующем, кадр за кадром:

Кадр 1

  • execute moveRight
  • Выполнить MoveLeft

Кадр 2

  • Выполнить MoveRight
  • Выполнить MoveLeft

Ad Infinitum.

Поскольку вы запускаете анимацию каждый раз, когда вызываете 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];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...