-1

Im just messing around in sprite-kit working on my game design skills and I came across something that I dont know how to do. That is make the physicsworld gravity more like moon gravity. I have a SKSpriteNode which is a red ball and I can drag that ball and click wherever I want it to go. What I wanted to create is somewhat of a throwing mechanism. Is this possible with one line of physics world gravity code? Or is it way more in depth this is code that I have.

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 */
}

}

My game state right now is when I drag a ball to where I want and release it just drops. What I want to accomplish is for example if I grab the ball and drag up and to the right I want the ball to follow my the direction it was going when I released it. How would I create that in my code thanks!

Cj Miller
  • 51
  • 1
  • 7

1 Answers1

0

In my code all I do is self.physicsworld.gravity = CGVectorMake(0, 0) (this would turn off gravity completely). Then just mess around with that vector and find something that feels like moon gravity.

You might have to make your game-scene be a SKPhysicsContactDelegate

J.Doe
  • 1,502
  • 13
  • 47