SpriteKit Gravity

时间:2015-08-27 20:44:41

标签: swift sprite-kit gravity

我只是在sprite-kit中搞砸我的游戏设计技巧,我遇到了一些我不知道该怎么做的事情。那就是物理世界的引力更像月亮引力。我有一个红色的SKSpriteNode,我可以拖动那个球,然后点击我要去的地方。我想要创造的是一种投掷机制。用一行物理世界引力代码可以实现吗?或者更深入的是这是我的代码。

import SpriteKit

class GameScene: SKScene {

let redBall = SKSpriteNode(imageNamed: "redball")


override func didMoveToView(view: SKView) {
    let worldBorder = SKPhysicsBody(edgeLoopFromRect: self.frame)
    self.physicsBody = worldBorder
    self.physicsBody?.friction = 1

    self.physicsWorld.gravity = CGVectorMake(0, -1)

    redBall.position = CGPoint(x:250,y:50)
    addChild(redBall)
    redBall.physicsBody = SKPhysicsBody(circleOfRadius: redBall.size.width/2)

}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    let touch = touches.first as! UITouch
    let location = touch.locationInNode(self)
    redBall.position = location
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    let touch = touches.first as! UITouch
    let location = touch.locationInNode(self)
    redBall.position = location
}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
}

}

我现在的游戏状态是当我将球拖到我想要的位置并将其释放时。我想要完成的是,例如,如果我抓住球并向上拖动到右边,我希望球能够跟随我释放它时的方向。我将如何在我的代码中创建它,谢谢!

1 个答案:

答案 0 :(得分:0)

在我的代码中,我所做的只是self.physicsworld.gravity = CGVectorMake(0,0)(这将完全关闭引力)。然后只是弄乱那个矢量,找到感觉像月球引力的东西。

您可能必须让您的游戏场景成为SKPhysicsContactDelegate