2015-09-10 42 views
1

我正在尝试将一个精灵从左到右移动到屏幕上。我听说我可以通过使用velocity属性来做到这一点,或者其他方式也可以,但这里是我的代码。在Sprite工具包中的屏幕上移动精灵

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) { 

} 
} 

谢谢!

回答

1

您可能想要使用SKAction,假设您希望它滑过屏幕,而不是简单地从点a移动到点b。你可以做这样的事情:

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

这将创建一个名为SKAction此举移动到屏幕的右侧的节点,然后运行一个节点上的行动,在这种情况下,redBall。

要移动到左边,你只需要改变的x值的移动动作,以这样的:

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

我怎么会移回向左吗? –

+0

我更新了我的答案 – TheCodeComposer

相关问题