Jump SpriteKit build ?

Hello guys,


I am actually new to Swift, I am trying to build a game on iOS.

Unfortunately, I have a problem about my jump section the player doesn't even jump... for a runner game.

When I try to put in comment some sections that may be false, this time the player jumps like superman, even if I tried to put a condition that it can only jump again once touched the ground with the player.


I put you the code down below for you guys to see where could be the problem. I tried nearly everything, I am on it since 2 hours now...


Thank you for your help,


Good evening.



Class GameScene ->



import SpriteKit

class GameplayScene: SKScene, SKPhysicsContactDelegate {


var player = Player();

var canJump = false;


override func didMove(to view: SKView) {

initialize();

}


override func update(_ currentTime: TimeInterval) {

moveBackgroundsAndGrounds();

}


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

if canJump {

/

player.jump();

}

}


func didBeginContact(contact: SKPhysicsContact) {

var firstBody = SKPhysicsBody();

var secondBody = SKPhysicsBody();

if contact.bodyA.node?.name == "Player" {

firstBody = contact.bodyA;

secondBody = contact.bodyB;

} else {

firstBody = contact.bodyB;

secondBody = contact.bodyA;

}

if firstBody.node?.name == "Player" && secondBody.node?.name == "Ground" {

canJump = true;

}

}


func initialize() {

physicsWorld.contactDelegate = self;

createPlayer();

changeBG();

changeGround();

}


func createPlayer() {

player = Player(imageNamed: "Player 1");

player.initialize();

player.position = CGPoint(x: -10, y: 20);

self.addChild(player);

}


func changeBG() {

for i in 0...2 {

let bg = SKSpriteNode(imageNamed: "BG");

bg.name = "BG";

bg.anchorPoint = CGPoint(x: 0.5, y: 0.5);

bg.position = CGPoint(x: CGFloat(i) * bg.size.width, y: 0);

bg.zPosition = 0;

self.addChild(bg);

}

}


func changeGround() {

for i in 0...2 {

let bg = SKSpriteNode(imageNamed: "Ground");

bg.name = "Ground";

bg.anchorPoint = CGPoint(x: 0.5, y: 0.5);

bg.position = CGPoint(x: CGFloat(i) * bg.size.width, y: -(self.frame.size.height / 2));

bg.zPosition = 2;

bg.physicsBody = SKPhysicsBody(rectangleOf: bg.size);

bg.physicsBody?.affectedByGravity = false;

bg.physicsBody?.isDynamic = false;

bg.physicsBody?.categoryBitMask = ColliderType.Ground;

self.addChild(bg);

}

}


func moveBackgroundsAndGrounds() {

enumerateChildNodes(withName: "BG", using: ({

(node, Error) in

let bgNode = node as! SKSpriteNode;

bgNode.position.x -= 4;

if bgNode.position.x < -(self.frame.width) {

bgNode.position.x += self.frame.width * 3;

}

}))

enumerateChildNodes(withName: "Ground", using: ({

(node, Error) in

let bgNode = node as! SKSpriteNode;

bgNode.position.x -= 2;

if bgNode.position.x < -(self.frame.width) {

bgNode.position.x += self.frame.width * 3;

}

}))

}





Class player -> Jump :



import SpriteKit

struct ColliderType {

static let Player: UInt32 = 1;

static let Ground: UInt32 = 2;

static let Obstacle: UInt32 = 3;

}

class Player: SKSpriteNode {


func initialize() {

self.name = "Player";

self.zPosition = 2;

self.anchorPoint = CGPoint(x: 0.5, y: 0.5);

self.setScale(0.5);

self.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.size.width - 20, height: self.size.height

));

self.physicsBody?.affectedByGravity = true;

self.physicsBody?.allowsRotation = false;

self.physicsBody?.categoryBitMask = ColliderType.Player;

self.physicsBody?.collisionBitMask = ColliderType.Ground | ColliderType.Obstacle;

self.physicsBody?.contactTestBitMask = ColliderType.Ground | ColliderType.Obstacle;

}


func jump() {

self.physicsBody?.velocity = CGVector(dx: 0, dy: 0);

self.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 200));

}


}



Thank you again,

Replies

You've obviously tried changing the 200 to something lower right?...


self.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 200));


Just curious, why are you immediately scaling down the player? I would adjust your source artwork instead of messing with the scale. Tends to work out better for ya in the long run.