How to move the platform with speed

I need to move a node, but use speed instead of SKAction, because in this question, moving a node on top of a moving platform

I want the node to move with the platform, but since the platform has no speed, I cannot use the Epic Byte clause

This is how I move 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]))); 
+2
source share
1 answer

You must edit this to make it more understandable. It looks like you want to use a physical engine to achieve the effect of a moving platform instead of using SKActions, because you need your platforms to be dynamic and interact with the physical engine (like this example).

So, you have two options. First, move your node to a point and continue to check if the node has arrived at that point. If so, move the node back to the starting point. To move a node to a specific point using real-time motion, you can see my answer here . If your platforms move in only one direction (horizontal or vertical), you should only apply speed in that direction.

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

Below is the code for moving the platform horizontally using the centripetal motion effect described above. What is nice about this is that it allows you to set the radius, as well as the period of movement of the platform. But if you need your platform to move along an arbitrary path of points, this will not work, so you need to resort to using the first option, which 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*/); } } 
+3
source

Source: https://habr.com/ru/post/1267167/


All Articles