How to make the player move to the opposite side while on the go?

enter image description here

I want the player (red circle) to move to the opposite side of the circle path when touched. I have already done that the player follows the path, but I can not find the answer to my question on the Internet.

    override func didMoveToView(view: SKView) {

    player = SKSpriteNode(imageNamed: "circulo")
    player.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 - 170)
    player.color = colorGris
    player.colorBlendFactor = 1
    player.size = CGSize(width: 25, height: 25)
    self.addChild(player)
    player.zPosition = 3

   }
   override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */

    if gameStarted == false {

        gameStarted = true
        moveClockWise()
        movingClockWise = true

    }

       }



   func moveClockWise(){



    let dx = player.position.x - self.frame.width / 2
    let dy = player.position.y - self.frame.height / 2

    let rad = atan2(dy, dx)

    path = UIBezierPath(arcCenter: CGPoint(x:self.frame.width / 2, y: self.frame.height / 2) , radius: 170, startAngle: rad, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)

    let follow = SKAction.followPath(path.CGPath, asOffset: false, orientToPath: true, speed: 200)
    player.runAction(SKAction.repeatActionForever(follow).reversedAction())

}
+4
source share
2 answers

One of the easiest ways to move an object along a circular path is

  • Create container SKNode
  • Create sprite
  • Add a container to the scene
  • Set the position of the sprite xto the radius of the circular path
  • Add sprite to container
  • Turn the container

If you want to move the sprite to the other side or change the radius of rotation, just

  1. x

,

:

// 1) Create the container node 
let node = SKNode()
// 2) Create a sprite
let sprite = SKSpriteNode(color:SKColor.blueColor(),size:CGSizeMake(20,20))
var rotation:CGFloat = CGFloat(M_PI)
let radius:CGFloat = 50

override func didMoveToView(view: SKView) {
    scaleMode = .ResizeFill
    node.position = view.center
    // 3) Add the container to the scene
    addChild(node)
    // 4) Set the sprite x position
    sprite.position = CGPointMake(radius, 0)
    // 5) Add the sprite to the container
    node.addChild(sprite)
    // 6) Rotate the container
    rotate()
}

// Rotate the container
func rotate() {
    let action = SKAction.rotateByAngle(rotation, duration: 4)
    node.runAction(SKAction.repeatActionForever(action),withKey: "rotate")
}

// 8) Reverse the direction of the rotation
func reverse() {
    rotation = -rotation
}

// Stop rotating the container
func stopRotation() {
    if node.actionForKey("rotate") != nil {
        node.removeActionForKey("rotate")
    }
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* 7) Change the sprite x-position  */
    if sprite.actionForKey("move") == nil {
        stopRotation()
        let opposite = -sprite.position.x * 2
        let move = SKAction.moveByX(opposite, y: 0, duration: 3)
        let rotate = SKAction.runBlock {
            self.rotate()
        }
        sprite.runAction(SKAction.sequence([move, rotate]), withKey: "move")
    }
}
+3

, :

  • runAction :

, :

let follow = SKAction.followPath(path.CGPath, asOffset: false, orientToPath: true, speed: 200)
player.runAction(SKAction.repeatActionForever(follow).reversedAction(),withKey:"followPath")

:

player.removeActionForKey("followPath")
  • , "moveToPoint" ( ) :

, :

var myCircle : CGMutablePath! = CGPathCreateMutable()
let newDx = player.position.x - self.frame.width / 2
let newDy = player.position.y - self.frame.height / 2
let newRad = atan2(newDy, newDx)
let newPath = UIBezierPath(arcCenter: CGPoint(x:self.frame.width / 2, y: self.frame.height / 2) , radius: 170, startAngle: newRad, endAngle: newRad + CGFloat(M_PI * 4), clockwise: true)
  • :

, :

var mirroring = CGAffineTransformMakeScale(1.0, -1.0) // flip horizontal
var mirrorPath : CGMutablePath! = CGPathCreateMutable()
CGPathAddPath(mirrorPath, &mirroring, newPath.CGPath)
  • runAction:

:

let newFollow = SKAction.followPath(mirrorPath, asOffset: false, orientToPath: true, speed: 200)
player.runAction(SKAction.repeatActionForever(newFollow).reversedAction(),withKey:"followPath")

: - , , ​​, CGPoint ( "moveToPoint" ). CGPath :

var mirrorPoints = mirrorPath.getPathElementsPoints()
let destinationPoint = mirrorPoints.first!

Sprite-kit jumpAction SKAction's, .
"" Y, , ().

+1

All Articles