Manually move node over a period of time

前端 未结 1 615
清歌不尽
清歌不尽 2020-12-12 00:25

I currently have the following functions declared which moves a node to a target location which is called in the update function of the scene (In this situation, I can\'

1条回答
  •  有刺的猬
    2020-12-12 01:16

    I currently have the following functions declared which moves a node to a target location which is called in the update function of the scene (In this situation, I can't use SKAction.moveTo)

    You are correct not to use the SKActions in a scenario like this. I see you are trying to move a node to a position without using SKActions. I don't really understand the problem you are having with the code you posted. However I have written a small sample project that demonstrates moving a node to a position in the update function without SKActions. In this sample project, I have a sprite that moves to the current touch position. You can set the speed at which the node travels as well as the rate at which the velocity is applied. You can try messing with the rate to get the correct feel for your game. You will also need to handle the case when the node arrives at the target location (again, this will depend on your game).

    Let me know if this helps at all and/or if you have any questions.

    import SpriteKit
    class GameScene: SKScene {
        var sprite: SKSpriteNode!
        var travelPoint: CGPoint = CGPoint() //The point to travel to.
        let travelSpeed = CGVector(dx: 200, dy: 200) //The speed at which to travel.
        let rate:CGFloat = 0.1 //1.0 Completely responsive. 0.0 Completely unresponsive. I set this value to < 1 to smooth the application of motion.
        override func didMoveToView(view: SKView) {
            self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
            sprite = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 50, height: 50))
            sprite.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
            sprite.position = CGPoint(x: self.size.width/2.0, y: self.size.height/2.0)
            sprite.physicsBody?.affectedByGravity = false
            self.addChild(sprite)
        }
        override func touchesBegan(touches: Set, withEvent event: UIEvent) {
            let touch = touches.first as! UITouch
            let location = touch.locationInNode(self)
                travelPoint = location
        }
        override func touchesMoved(touches: Set, withEvent event: UIEvent) {
            let touch = touches.first as! UITouch
            let location = touch.locationInNode(self)
            travelPoint = location
        }
        override func update(currentTime: CFTimeInterval) {
            let disp = CGVector(dx: travelPoint.x-sprite.position.x, dy: travelPoint.y-sprite.position.y)
            let angle = atan2(disp.dy, disp.dx)
            let vel = CGVector(dx: cos(angle)*travelSpeed.dx, dy: sin(angle)*travelSpeed.dy)
            let relVel = CGVector(dx: vel.dx-sprite.physicsBody!.velocity.dx, dy: vel.dy-sprite.physicsBody!.velocity.dy)
            sprite.physicsBody!.velocity = CGVector(dx: sprite.physicsBody!.velocity.dx+relVel.dx*rate, dy: sprite.physicsBody!.velocity.dy+relVel.dy*rate)
        }
    }
    

    enter image description here

    0 讨论(0)
提交回复
热议问题