简体   繁体   English

Spritekit如何根据触摸和按住的位置运行SKAction

[英]Spritekit how to run an SKAction depending on where a touch and hold is at

So I'm trying to implement a method into a game where if a player has their finger touching the screen at lets say 50 on the y axis or lower, an SKAction will run to move the character sprite down, and if their finger is touching above the 50, another action will run moving the sprite up. 因此,我尝试在游戏中实现一种方法,如果玩家的手指在y轴或更低的方向上触摸屏幕(例如说50),则SKAction将运行以将角色精灵向下移动,并且如果他们的手指在触摸高于50,另一个动作将使精灵向上移动。 I don't know how you have it recognize a touch and hold though. 我不知道您如何将它识别为触摸并按住。 Help please. 请帮助。

You will need to capture the touch event of the user in order to pull this off. 您需要捕获用户的触摸事件才能完成此操作。 I suggest checking out the excellent tutorial provided by RayWenderlich at: Animating Textures and Moving Them With Touch Events 我建议您查看RayWenderlich提供的出色的教程,该教程位于: 动画纹理和通过触摸事件移动它们

If you don't have time for that this is what your code might look like: 如果您没有时间这样做,那么您的代码可能会像这样:

First the knowledge that you will need is that the two most generic touch events are the: -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event and the: -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 首先,您需要了解的是两个最通用的触摸事件:-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event和:-( void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

Basically the touches began method is called when the user places their finger on the screen whereas the touches ended method will be triggered when the user takes their finger off the screen. 基本上,当用户将手指放在屏幕上时,将开始触摸开始方法,而当用户将手指从屏幕上移开时,将结束触摸结束方法。

For this example I will use touchesEnded, but feel free to change it if you want, its as easy as switching the "Ended" with "Began". 在此示例中,我将使用touchesEnded,但可以随意更改它,就像将“ Ended”与“ Began”切换一样容易。

All of the code I am about to show you will take place within the scenes implementation file: 我将向您展示的所有代码都将在场景实现文件中进行:

If you just want the sprite to move up when above a certain point and down when below a certain point use the following: 如果只想让精灵在特定点之上向上移动而在特定点之下向下移动,请使用以下命令:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //capture the location of the touch
    CGPoint location = [[touches anyObject] locationInNode:self];

    //used to hold the location to travel
    CGPoint moveLocation;

    //check if the Y location is less than 50
    if(location.y < 50)
    {
        //the the location to the bottom of the screen
         moveLocation = CGPointMake(sprite.position.x, 0);
    }
    //and then if its more than 50
    else
    {
        //set the locaiton to the top of the screen
        moveLocation = CGPointMake(sprite.position.x, self.frame.size.height);
    }

    //move the sprite

    //Check if their already moving
    if(sprite actionForKey:@"moving")
    {
        //If they are stop them so they can move in the new direction
        [sprite removeActionForKey:@"moving"];
    }
    SKAction *moveAction = [SKAction moveTo:location duration:5.0f];

    //remember to give the action a key so that we can check for it during the above if statement
    [sprite runAction:moveAction withKey:@"moving"];
}

And there you have it. 那里有它。 The only flaw is that it will always take 5 seconds to reach the location regardless of your distance from it, read on if you would like hints on how to correct that, or if you would like to see how to make the sprite travel to any touched location on the screen. 唯一的缺陷是,无论您与该位置的距离如何,到达该位置始终需要5秒钟,请继续阅读以下内容,以获取有关如何更正该位置的提示,或者如果您想了解如何使该精灵移动到任何位置触摸屏幕上的位置。

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //capture the location of the touch
    CGPoint location = [[touches anyObject] locationInNode:self];

    //move the sprite

    //Check if their already moving
    if(sprite actionForKey:@"moving")
    {
        //If they are stop them so they can move in the new direction
        [sprite removeActionForKey:@"moving"];
    }
    SKAction *moveAction = [SKAction moveTo:location duration:5.0f];

    //remember to give the action a key so that we can check for it during the above if statement
    [sprite runAction:moveAction withKey:@"moving"];
}

That code will make it so that for 5 seconds the sprite will move to the location touched on the screen. 该代码将使其保持原状,以便在5秒钟内将精灵移动到屏幕上触摸的位置。 Just remember to replace the instances of "sprite" with the name of your SKSpriteNode variable. 只要记住用您的SKSpriteNode变量的名称替换“ sprite”的实例即可。 For more complex movement try the following: 对于更复杂的运动,请尝试以下操作:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //capture the location of the touch
    CGPoint location = [[touches anyObject] locationInNode:self];
    //set the velocity of the subject
    float velocity = self.frame.size.width/3.0;
    //The above will ensure that it will take the sprite 3 seconds to travel the distance of the screen

    //Determine the difference between the point touched and the sprites position
    CGPoint moveDifference = CGPointMake(location.x - sprite.position.x, location.y - sprite.position.y);

    //Use pythagorean theorem to figure out the actual length to move
    float distanceToMove = sqrtf(moveDifference.x * moveDifference.x + moveDifference.y*moveDifference.y);

    //Use the distance to travel and the velocity to determine how long the sprite should move for
    float moveDuration = distanceToMove / velocity;

    //and finally move the sprite
    if(sprite actionForKey:@"moving")
    {
        [sprite removeActionForKey:@"moving"];
    }
    SKAction *moveAction = [SKAction moveTo:location duration:moveDuration];

    [sprite runAction:moveAction withKey:@"moving"];
}

That will set a velocity for the sprite, determine the length of the travel, and how long it will take to get to the new location. 这将为子画面设置速度,确定行进的长度,以及到达新位置所需的时间。

If you want to involve textures I highly suggest reading the link I provided. 如果要涉及纹理,我强烈建议您阅读我提供的链接。

Upon request I would be glad to offer examples using forces to control movement speed as well. 根据要求,我将很高兴提供使用力来控制运动速度的示例。

I hope that helps, let me know if there are any problems I wasn't in a position where I could run the code but I am sure that it is fine. 希望能对您有所帮助,让我知道我是否有可以运行代码的位置,但是我确定这是可以的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM