【问题标题】:Delays between a sprites position change精灵位置变化之间的延迟
【发布时间】:2012-09-21 15:00:59
【问题描述】:

现在我有一个NSArray,里面有x的坐标数量;我有一个精灵需要去每个点沿着选定的路径走。我尝试使用 for 循环,但这样做的速度如此之快,以至于它似乎只是传送到最终目的地。我已经尝试了一个 with 选择器,但我也无法让它们工作。有人知道怎么做吗?

【问题讨论】:

  • 如何更新精灵位置?
  • 这就是我现在正在做的事情。我希望最终结果只使用 sprite.position = [locationsArray objectAtPosition: step];
  • errr,这会让他们从一个位置跳到下一个位置,这是你想要的吗?您可能想发出一个 CCMoveTo 到下一个点,当没有更多操作时,运行下一个 CCMoveTo。
  • 是的,精灵就像板上的碎片。我现在拥有的是:for (int i = 0; i<pathLength; i++) { t = [path objectAtIndex:i]; move = [CCMoveTo actionWithDuration:0 position:[self positionForTileCoord:t.position]]; [piece runAction:move]; }

标签: objective-c for-loop cocos2d-iphone sprite


【解决方案1】:

你必须使用 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];

然后它调用 this 直到遍历所有对象

-(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;
}
}

【讨论】:

  • 我在这一行收到了 EXC_BAD_ACCESS 电话mySprite.position = [locationArray objectAtIndex:count];
  • 你的数组中有哪些类型的对象?
  • 从这个错误中,看起来数组可能是空的,或者不是CGPoints数组,..
  • 它不是空的,但我必须这样做:CCSprite *p=[array objectAtIndex:i]; NSLog(@"Tile:%g,%g",p.position.x,p.position.y); 以获得积分
  • 因为某种原因sprite.position = [array objectAtPosition: x]; 不起作用。
【解决方案2】:

创建一个方法,将您的精灵放置到位置数组中某个索引处的位置(例如,_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];
    }

}

这只是示例,但我希望您可以根据自己的需要进行调整。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-04
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    相关资源
    最近更新 更多