Spritekit如何根据触摸和保持的位置运行SKAction

时间:2014-09-28 21:58:18

标签: ios sprite-kit

所以我试图在游戏中实现一种方法,如果玩家的手指在y轴或更低的位置上触摸屏幕,那么SKAction将运行以移动角色精灵,如果他们的手指触摸50以上,另一个动作将运行精灵向上移动。我不知道你怎么认识到触摸和握住它。请帮助。

1 个答案:

答案 0 :(得分:1)

您需要捕获用户的触摸事件才能将其关闭。我建议查看RayWenderlich提供的优秀教程:Animating Textures and Moving Them With Touch Events

如果您没有时间,那么这就是您的代码的样子:

首先,您需要的知识是两个最通用的触摸事件是: - (void)touchesEnded:(NSSet *)触及withEvent:(UIEvent *)事件 和: - (void)touchesBegan:(NSSet *)触及withEvent:(UIEvent *)事件

基本上,当用户将手指放在屏幕上时会调用触摸开始方法,而当用户将手指从屏幕上移开时,将触发触摸结束方法。

对于这个例子,我将使用touchesEnded,但如果你愿意,可以随意更改它,就像切换"结束"一样简单。用"开始"。

我要向您展示的所有代码都将在场景实现文件中进行:

如果您只想让精灵在某个点以上向上移动而在某个点以下向下移动,请使用以下内容:

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

你有它。唯一的缺陷是它总是需要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"];
}

该代码将使精灵在5秒内移动到屏幕上触摸的位置。只记得替换&#34; sprite&#34;的实例。使用SKSpriteNode变量的名称。对于更复杂的运动,请尝试以下方法:

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

这将设置精灵的速度,确定行程的长度,以及到达新位置所需的时间。

如果您想涉及纹理,我强烈建议您阅读我提供的链接。

根据要求,我很乐意提供使用力来控制移动速度的例子。

我希望有帮助,让我知道如果有任何问题我不能处理我可以运行代码的位置,但我确信它没问题。