Swift 在精灵套件中在屏幕上移动精灵

Swift 在精灵套件中在屏幕上移动精灵,swift,sprite-kit,Swift,Sprite Kit,我试图将一个红色小球的精灵从左到右移动到屏幕上。我听说我可以通过使用velocity属性来实现这一点,也许其他方法也可以,但这是我的代码 import SpriteKit class GameScene: SKScene, SKPhysicsContactDelegate { let redBall = SKSpriteNode(imageNamed: "redball") override func didMoveToView(view: SKView) { let thrus

我试图将一个红色小球的精灵从左到右移动到屏幕上。我听说我可以通过使用velocity属性来实现这一点,也许其他方法也可以,但这是我的代码

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

}
}
导入SpriteKit
类游戏场景:SKScene,SKPhysicContactDelegate{
让redBall=SKSpriteNode(图像名为:“redBall”)
覆盖func didMoveToView(视图:SKView){
让推力=cg浮动(0.12)
让方向=浮动()
//红球属性
redBall.physicsBody=SKPhysicsBody(圆形半径:redBall.size.width/2)
红球,物理体?,速度
addChild(红球)
//世界物理学性质
让worldBorder=SKPhysicsBody(edgeLoopFromRect:self.frame)
自身物理体?摩擦力=1
self.physicsBody=世界边界
self.physicsWorld.gravity=CGVectorMake(0,0)
}
覆盖func touchesBegined(触摸:设置,withEvent事件:UIEvent){
}
覆盖功能触摸移动(触摸:设置,withEvent事件:UIEvent){
}
}

谢谢

您可能希望使用SKAction,前提是您希望它在屏幕上滑动,而不是简单地从a点移动到b点。你可以这样做:

let move = SKAction.moveToX(self.frame.size.width, duration: 1.0)
redBall.runAction(move)
这将创建一个名为move的SKAction,它将节点移动到屏幕的右侧,然后在节点(在本例中为红球)上运行该操作

要向左移动,只需将移动操作中的x值更改为:

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

我怎样才能把它移回左边呢?