现在我里面有NSArray
很多x
坐标;我有一个精灵需要去每个点沿着选定的路径走。我尝试使用 for 循环,但这样做的速度如此之快,以至于它似乎只是传送到最终目的地。我已经尝试了一个 with 选择器,但我也无法让它们工作。有人知道怎么做吗?
问问题
198 次
2 回答
0
你必须使用 NSTimer
@implementation whatever
{
int count;
NSTimer *myTimer;
CCSprite *mySprite;
NSArray *locationArray;
}
然后从某个地方启动计时器...
count=0
//1.0 is one second, so change it to however long you want to wait between position changes
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(movealong) userInfo:nil repeats:YES];
然后它调用它直到它遍历所有对象
-(void)movealong{
//assuming array is full of CGPoints...
//mySprite.position = [locationArray objectAtIndex:count];
//try this then
CGPoint newPoint = [locationArray objectAtIndex:count];
mySprite.position = ccp(newPoint.x,newPoint.y);
//I think maybe THIS is what you need to do to make it work...
//if you want it to not jump directly there
//you can also use CCMoveTo
/*// under actionWithDuration: put the same amount of time or less of what you had
// under scheduledTimerWithTimeInterval in your NSTimer
CCFiniteTimeAction *newPos = [CCMoveTo actionWithDuration:1.0 position:[locationArray objectAtIndex:count]];
[mySprite runAction:newPos];
*/
count++;
if(count >= locationArray.count){
[myTimer invalidate];
myTimer = nil;
}
}
于 2012-09-30T14:44:31.483 回答
0
创建一个方法,将您的精灵放置在位置数组中某个索引处(例如,_i)的位置。并在此方法结束时再次延迟调用它,依次使用 CCDelayTime 和 CCCallFunc 动作。并且不要忘记增加索引。喜欢
// somewhere in code to start move
_i = 0;
[self setNewPosition];
// method that will set the next position
- (void) setNewPosition
{
sprite.position = // get position at index _i from your array here
_i++;
BOOL needSetNextPosition = // check if _i is inside bounds of your positions array
if( needSetNextPosition )
{
id delay = [CCDelayTime actionWithDuration: delayBetweenUpdate];
id callback = [CCCallFunc actionWithTarget: self selector: @selector(setNewPosition)];
id sequence = [CCSequence actionOne: delay two: callback];
[self runAction = sequence];
}
}
这只是示例,但我希望您可以根据需要对其进行调整。
于 2012-09-30T18:14:16.740 回答