0

在我的程序中,当您在 iOS Sim 中点击屏幕时,会使用 CCMoveTo 发牌。动画需要 0.4 秒才能完成。再次点击屏幕,将发出下一组牌。第二个动画也需要 0.4 秒。我要做的就是禁止我的程序在第一个动画完成之前分发第二组卡片。尝试过 sleep() ,显然无法达到预期的效果。还尝试了 Cocos2D 的 CCDelayTime 几种不同的方法,但无法产生预期的结果。执行此操作的最简单和/或最节省内存的方法是什么?

编辑:@crackity_jones - 这是我的 HelloWorldLayer 文件和我为 CCSprite 创建的允许移动的类别:

HelloWorldLayer.h

#import <GameKit/GameKit.h>
#import "CCSprite+MoveActions.h"
#import "cocos2d.h"

    // globals
CCSprite *card1;
...
CCSprite *card9;

@interface HelloWorldLayer : CCLayer {
    NSMutableArray *deck;
    ...
    int touchCount;
}

...

@end

HelloWorldLayer.m

...

-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    touchCount++;
    [self nextRound];
}

// one method to handle all rounds dealt
-(void)nextRound {
    if (touchCount == 1) {
        [self removeAllCards];
        [deck shuffle];
        [self dealFirstRound];
    } else if (touchCount == 2) {
        [self dealSecondRound];
    } else if (touchCount == 3) {
        [self dealThirdRound];
    } else if (touchCount == 4) {
        [self dealFourthRound];
    touchCount = 0;
    }
}

-(NSMutableArray *)makeDeck {
    // create mutable array of 52 card sprites
}

-(void)dealFirstRound {
    ...
    // declare, define, add card objects to layer at start-point here
    …
    // the "moveToPositionX" methods contain the animation code
    [firstCard moveToPosition1];
    [secondCard …2];
    [third...3];
    [fourth...4];
    }

// Also methods for removing all rounds of cards
…
@end

CCSprite(移动动作)

#import "CCSprite+MoveActions.h"
#define kCardTravelTime .1

@implementation CCSprite (MoveActions)

-(void)moveToPosition1 {
    CGSize size = [[CCDirector sharedDirector] winSize];
    [self runAction:[CCMoveTo actionWithDuration:kCardTravelTime
                                        position:CGPointMake(size.width/2 - card1.contentSize.width/4, card1.contentSize.height/2 + holeCard1.contentSize.height/5)]];
    CCDelayTime *waitTime = [CCDelayTime actionWithDuration:.4];
    [self runAction:waitTime];
}

-(void)moveToPosition2 {
    CGSize size = [[CCDirector sharedDirector] winSize];
    CCDelayTime *delay = [CCDelayTime actionWithDuration:.2];
    CCMoveTo *move = [CCMoveTo actionWithDuration:kCardTravelTime
                                        position:CGPointMake(size.width/2 + card1.contentSize.width/4, card1.contentSize.height/2 + holeCard1.contentSize.height/5)];
    [self runAction:[CCSequence actions:delay, move, nil]];
}
// rest of the methods 3-9 look like the above, essentially

@end
4

2 回答 2

1

你可以试试这个!如果需要,可以在 CCSequence 动作中插入一些 CCDelaytime 动作。

-(void)dealFirstRound {
    id card1Move = [CCCallBlock actionWithBlock:^{
        [firstCard moveToPosition1];
    }];
    id card2Move = [CCCallBlock actionWithBlock:^{
        [secondCard moveToPosition2];
    }];
    id card3Move = [CCCallBlock actionWithBlock:^{
        [thirdCard moveToPosition3];
    }];
    id card4Move = [CCCallBlock actionWithBlock:^{
        [fourthCard moveToPosition4];
    }];
    [self runAction:[CCSequence actions:
                     card1Move,
                     card2Move,
                     card3Move,
                     card4Move,
                     nil];
}
于 2012-07-02T14:54:36.287 回答
0

这样做CCSequence

[<<CCNode>> runAction:[[CCSequence actions:[deal hand],[CCDelayTime actionWithDuration:.4],[deal 2nd hand].. ,nil]];

这将执行第一个动作,延迟 0.4 秒,执行第二个动作..等等..你可以添加你想要多少个动作

于 2012-06-26T23:53:23.787 回答