如何以速度移动平台

如何以速度移动平台

本文介绍了如何以速度移动平台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要移动一个节点而不是 SKAction 来使用速度因为在这个问题中 Moving Node on top of a移动平台

I need to move a node but instead of SKAction to use velocityBecause in this question Moving Node on top of a moving platform

我希望节点随平台移动,但由于平台没有速度,我无法使用 Epic Byte 建议

I want the node to move with the platform, but since the platform don't have velocity I can't use Epic Byte suggestion

这就是我移动节点的方式

This is how I move the node

// Move node vertically
let up = SKAction.moveByX(0, y: 200, duration: moveDuration);
let down = SKAction.moveByX(0, y: -200, duration: moveDuration);

node.runAction(SKAction.repeatActionForever(SKAction.sequence([up, down])));

// Move node horizontally
let right = SKAction.moveByX(400, y: 0, duration: moveDuration);
let left = SKAction.moveByX(-400, y: 0, duration: moveDuration);

node.runAction(SKAction.repeatActionForever(SKAction.sequence([right, left])));

推荐答案

您应该编辑这个以使其更清晰.看起来您想使用物理引擎来实现移动平台效果而不是使用 SKActions,因为您需要您的平台是动态的并与物理引擎交互(如 这个示例).

You should edit this one to make it more clear. It looks like you want to use the physics engine to achieve the moving platform effect instead of using SKActions because you need your platforms to be dynamic and interact with the physics engine (like in this example).

所以你有两个选择.第一个是将你的节点移动到一个点,并继续检查节点是否到达了那个点.如果是,则将节点移回其起点.要使用实时运动将节点移动到特定点,您可以在此处查看我的答案.如果您的平台只在一个方向(水平或垂直)移动,那么您应该只在该方向应用速度.

So you have two options. The first is to move your node to a point, and keep checking if the node arrived at that point. If it did, then move the node back to its starting point. To move a node to a particular point using real-time motion, you can see my answer here. If your platforms only move in one direction (horizontal or vertical) then you should only apply the velocity in that direction.

移动平台时我经常使用的另一种方法是向心运动.这将允许您在一个圆圈中移动平台.更酷的是,如果您将向心运动限制在一个方向(水平或垂直),那么您可以轻松移动平台并获得完美的缓入缓出效果.您可以在我的回答此处中查看如何模拟实时向心运动的示例.

Another approach I often use when moving platforms is centripetal motion. This will allow you to move platforms in a circle. What's even cooler is that if you restrict the centripetal motion to one direction (horizontal or vertical) then you can move the platform easily and get a perfect ease-in and ease-out effect. You can see an example of how to simulate real-time centripetal motion in my answer here.

下面是利用我上面描述的这种向心运动效果水平移动平台的代码.这样做的好处是它允许您设置平台运动的半径和周期.但是,如果您需要您的平台通过一些任意的点路径,这是行不通的,所以您需要使用我提到的第一个选项.

Below is the code for moving the platform horizontally by exploiting this centripetal motion effect I described above. What's nice about this is that it allows you to set the radius as well as the period of the platform's motion. But if you need your platform to travel some arbitrary path of points this won't work, so you will need to resort to using the first option that I mentioned.

class GameScene: SKScene {
    var platform: SKSpriteNode!
    var platformAngularDistance: CGFloat = 0

    override func didMoveToView(view: SKView) {
        physicsWorld.gravity = CGVector(dx: 0, dy: 0)
        platform = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 80, height: 20))
        platform.position = CGPoint(x: self.size.width/2.0+50, y: self.size.height/2.0)
        platform.physicsBody = SKPhysicsBody(rectangleOfSize: platform.size)
        self.addChild(platform)
    }
    override func update(currentTime: NSTimeInterval) {
        let dt: CGFloat = 1.0/60.0 //Delta Time
        let period: CGFloat = 3 //Number of seconds it takes to complete 1 orbit.
        let orbitPosition = CGPoint(x: self.size.width/2.0, y: self.size.height/2.0) //Point to orbit.
        let orbitRadius: CGFloat = 50 /*CGPoint(x: 50, y: 50)*/ //Radius of orbit.
        let normal = CGVector(dx:orbitPosition.x + CGFloat(cos(self.platformAngularDistance)) * orbitRadius, dy:0 /*orbitPosition.y + CGFloat(sin(self.node2AngularDistance))*orbitRadius.y*/)
        self.platformAngularDistance += (CGFloat(M_PI)*2.0)/period*dt;
        if (self.platformAngularDistance>CGFloat(M_PI)*2)
        {
            self.platformAngularDistance = 0
        }
        if (self.platformAngularDistance < 0) {
            self.platformAngularDistance = CGFloat(M_PI)*2
        }
        platform.physicsBody!.velocity = CGVector(dx:(normal.dx-platform.position.x)/dt ,dy:0/*(normal.dy-platform.position.y)/dt*/);
    }

}

这篇关于如何以速度移动平台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 03:01