Cj Miller
Cj Miller

Reputation: 55

Moving Sprites Across Screen In Sprite Kit

I am trying to move a sprite that is a red ball from left and right to the screen. I hear I can do this by using the velocity property maybe or another way would be ok too but here is my code.

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

let  redBall = SKSpriteNode(imageNamed: "redball")

override func didMoveToView(view: SKView) {
    let thrust = CGFloat(0.12)
    let thrustDirection = Float()



    //RedBall Properties
    redBall.physicsBody = SKPhysicsBody(circleOfRadius: redBall.size.width/2)
    redBall.physicsBody?.velocity
    addChild(redBall)


    //World Phyisics Properties
    let worldBorder = SKPhysicsBody(edgeLoopFromRect: self.frame)
    self.physicsBody?.friction = 1
    self.physicsBody = worldBorder
    self.physicsWorld.gravity = CGVectorMake(0, 0)

}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {

}
}

Thanks!

Upvotes: 2

Views: 1379

Answers (1)

TheCodeComposer
TheCodeComposer

Reputation: 711

You may want to use an SKAction assuming you want it to slide across the screen as opposed to simply moving from point a to point b. You could do something like this:

let move = SKAction.moveToX(self.frame.size.width, duration: 1.0)
redBall.runAction(move)

This creates an SKAction called move that moves a node to the right side of the screen and then runs that action on a node, in this case the redBall.

To move to the left, you just change the x value in the move action to this:

let move = SKAction.moveToX(0, duration: 1.0)

Upvotes: 2

Related Questions