开发者

Moving a "falling" CCSprite according to acceleration

I am using this code to move a "falling" sprite according to the acceleration of the UIDevice. It works nice, but is very laggy, meaning that there are jumps between the positions of the sprite.

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    if(paused)
开发者_运维知识库        return;
    directionPoint.x += acceleration.x * 100;
}
- (void)update:(ccTime)dt {
    if(paused)
        return;
    directionPoint.y = -500*dt;
    CGPoint playerDestinationPoint = player.position;
    playerDestinationPoint.x += directionPoint.x;
    playerDestinationPoint.y += directionPoint.y;
    if(playerDestinationPoint.x < player.contentSize.width/2) {
        playerDestinationPoint.x = player.contentSize.width/2;
    }
    else if(playerDestinationPoint.x > ([[CCDirector sharedDirector] winSize].width - play    er.contentSize.width/2)) {    
        playerDestinationPoint.x = [[CCDirector sharedDirector] winSize].width - player.contentSize.width/2;
    }
    [player setPosition:playerDestinationPoint];

    directionPoint = CGPointMake(0.0, 0.0);
}

I tried to use CCMoveTo this way:

- (void)update:(ccTime)dt {
    if(paused)
    return;
    [player stopAllActions];
    directionPoint.y = -500*dt;
    CGPoint playerDestinationPoint = player.position;
    playerDestinationPoint.x += directionPoint.x;
    playerDestinationPoint.y += directionPoint.y;
    if(playerDestinationPoint.x < player.contentSize.width/2) {
        playerDestinationPoint.x = player.contentSize.width/2;
    }
    else if(playerDestinationPoint.x > ([[CCDirector sharedDirector] winSize].width - player.contentSize.width/2)) {
        playerDestinationPoint.x = [[CCDirector sharedDirector] winSize].width - player.contentSize.width/2;
    }
    id actionMoveTo = [CCMoveTo actionWithDuration:dt position:playerDestinationPoint];
    [player runAction:actionMoveTo];

    directionPoint = CGPointMake(0.0, 0.0);
}

but the sprite won't move at all. Any hints?

MfG,

SideSwipe


In your first in your update method try:

    playerDestinationPoint.x += directionPoint.x*dt;

Since the dt interval can change depending on performance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜