【问题标题】:Sprite Kit - Move object to position of touchSprite Kit - 将对象移动到触摸位置
【发布时间】:2014-02-12 00:26:10
【问题描述】:

我的场景中有一个对象。当我触摸屏幕时,我希望对象的 y 位置成为我触摸的 y 位置。为此,我有以下代码:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    SKNode *player = [self childNodeWithName:@"player"];

    UITouch *touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    player.position = CGPointMake(player.position.x, positionInScene.y);
}

它适用于这段代码,但我如何让对象以稳定的速度移动到触摸 y 位置?因此,不要跳到触摸 y 位置,而是以预定义的速度移动到它。

【问题讨论】:

    标签: ios objective-c sprite-kit


    【解决方案1】:
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    
        SKNode *player = [self childNodeWithName:@"player"];
    
        UITouch *touch = [touches anyObject];
        CGPoint positionInScene = [touch locationInNode:self];
    
      // Determine speed
    int minDuration = 2.0;
    int maxDuration = 4.0;
    int rangeDuration = maxDuration - minDuration;
    int actualDuration = (arc4random() % rangeDuration) + minDuration;
    
    // Create the actions
    SKAction * actionMove = [SKAction moveTo:CGPointMake(player.position.x, positionInScene.y); duration:actualDuration];
    [player runAction:actionMove];
    
    }
    

    【讨论】:

    • 对不起,但这似乎不起作用。我看不出任何变化。
    • 非常感谢!我做了一个很小的改变(见编辑),它有效。你是最棒的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多