【问题标题】:Spritekit how to run an SKAction depending on where a touch and hold is atSpritekit 如何根据触摸和按住的位置运行 SKAction
【发布时间】:2014-09-30 15:37:05
【问题描述】:

所以我试图在游戏中实现一种方法,如果玩家的手指在 y 轴上的 50 或更低的位置触摸屏幕,SKAction 将运行以将角色精灵向下移动,如果他们的手指触摸到 50 以上,另一个动作将向上移动精灵。我不知道你是如何让它识别触摸并保持的。请帮忙。

【问题讨论】:

    标签: ios sprite-kit


    【解决方案1】:

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

    如果您没有时间,您的代码可能如下所示:

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

    基本上,当用户将手指放在屏幕上时会调用 touches started 方法,而当用户将手指从屏幕上移开时会触发 touches end 方法。

    在本例中,我将使用 touchesEnded,但如果您愿意,可以随意更改它,就像将“Ended”切换为“Began”一样简单。

    我即将向您展示的所有代码都将发生在场景实现文件中:

    如果您只想让精灵在某个点以上时向上移动,在某个点以下时向下移动,请使用以下命令:

    -(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 秒内使精灵移动到屏幕上触摸的位置。请记住用您的 SKSpriteNode 变量的名称替换“sprite”的实例。对于更复杂的运动,请尝试以下操作:

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

    这将为精灵设置一个速度,确定旅行的长度,以及到达新位置需要多长时间。

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

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

    希望对您有所帮助,如果有任何问题我无法运行代码,请告诉我,但我确信它没问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-16
      • 1970-01-01
      • 2014-09-03
      相关资源
      最近更新 更多