SKAction move forward

家住魔仙堡 提交于 2019-11-30 09:07:12

问题


How do I get a SKNode to move forward?

I've been able to get the node to move in a fixed direction using [SKAction moveByX:y:duration:] but I want it to move in a direction relative to the direction its facing.

I am looking to make each node turn 45 degrees then "walk" forward 50 pixels.

It seems simple enough I just am not able to find it.


回答1:


This will rotate, then towards where you tap

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

for (UITouch *touch in touches) {

    for (UITouch *touch in touches) {

        CGPoint location = [touch locationInNode:self];

        CGPoint diff = CGPointMake(location.x - _myPlayer.position.x, location.y - _myPlayer.position.y);

        CGFloat angleRadians = atan2f(diff.y, diff.x);

        [_myPlayer runAction:[SKAction sequence:@[
                                [SKAction rotateToAngle:angleRadians duration:1.0],
                                [SKAction moveByX:diff.x y:diff.y duration:3.0]
                                ]]];
    }
}
}



回答2:


I have thought about this as well. I want to make a node that can seem to be turning before proceeding. One way I have considered doing this is to have a child node attached the the "forward-most" portion of the node that is moving in the scene. Then I would calculate the distance of the tap form the child node. Does that make sense? Essentially, I would be able to calculate the front of the node from a tap anywhere in the scene.



来源:https://stackoverflow.com/questions/19172140/skaction-move-forward

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!