在我的程序中,当您在 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