在Sprite Kit中跨屏移动精灵

时间:2015-09-10 01:57:30

标签: swift sprite-kit

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

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 个答案:

答案 0 :(得分:1)

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

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

这会创建一个名为move的SKAction,它将节点移动到屏幕的右侧,然后在节点上运行该操作,在本例中为redBall。

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

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