0
votes

I want to know how to make a player jump only once till he hits the ground, just like in the most games. I tried this: swift - Jump only when landed, but it didn't really work for me because for whatever reason the player only jumped when I spammed tapping the screen and he only jumped like every 30 click. Here is how it looks like:

That's under the: class GameScene: SkScene {

var ableToJump = true
var Player = SKSpriteNode(imageNamed: "Spieler_rechts")

In the: didMoveToView(view: SKView)

Player.position = CGPoint(x: self.frame.size.width*0.1, y: Ground.frame.size.height + Player.frame.size.height)
Player.zPosition = 3
Player.physicsBody = SKPhysicsBody(rectangleOfSize: Player.size)
Player.physicsBody?.allowsRotation = false
Player.physicsBody?.affectedByGravity = true
Player.physicsBody?.restitution = 0
self.addChild(Player)

In the: override func update(currentTime: NSTimeInterval)

if Player.physicsBody?.velocity.dy == 0 {
    ableToJump = true
}
else {
    ableToJump = false
}

In the: override func touchesBegan(touches: Set, withEvent event: UIEvent?)

if ableToJump == true {
    Player.physicsBody?.applyImpulse(CGVectorMake(0, 20))
}
1
Did it fix your issue ?Coder1000
I can't figure out how to change the Baseline every time he collides with a block :/Lukas
The solution from this link doesn't work for me for whatever reason, thats why i made this post.Lukas
So, did you fix this ?Coder1000

1 Answers

0
votes

Here's how I did it:

EDIT:

    class MainScene: SKScene, SKPhysicsContactDelegate {

        let colorChanger = ColorChanger()
        let ground = SKSpriteNode(imageNamed: "Ground")
        let player = SKSpriteNode(imageNamed: "Player")
        let block1 = SKSpriteNode(imageNamed: "Block2")
        let block2 = SKSpriteNode(imageNamed: "Block2")
        let scoreLabel = SKLabelNode(fontNamed: "Chalkboard SE")
        let highScoreLabel = SKLabelNode(fontNamed: "Chalkboard SE")
        let life = SKSpriteNode(imageNamed: "Player")
        let life2 = SKSpriteNode(imageNamed: "Player")
        let life3 = SKSpriteNode(imageNamed: "Player")
        var firstTime = Int()

        var blockMaxX = CGFloat(0)
        var origBlockPositionX = CGFloat(0)
        var score = Int()
        var highScore = Int()
        var lives = Int()

        var groundPositionX = CGFloat(0)
        var maxBarX = CGFloat(0)
        var groundSpeed = 5
        var playerBaseLine = CGFloat(0)
        var more = CGFloat(0)

        var onGround = true
        var velocityY = CGFloat(0)
        let gravity = CGFloat(1.3)

        enum Collision:UInt32{
            case Player = 1
            case Block = 2
        }



override func didMoveToView(view: SKView) {
self.physicsWorld.contactDelegate = self
        let color1 = colorChanger.randomColor()
        runAction(SKAction.colorizeWithColor(color1, colorBlendFactor: 1, duration: 0.2))
        self.ground.anchorPoint = CGPointMake(0, 1)
        self.ground.position = CGPointMake(CGRectGetMinX(self.frame),CGRectGetMidY(self.frame)-23)
        self.ground.size = CGSize(width: 1100, height: 4000)

        self.groundPositionX = self.ground.position.x
        self.maxBarX = self.ground.size.width - self.frame.size.width - 100
        self.maxBarX *= -1

        self.player.size = CGSize(width: 50, height: 50)
        self.playerBaseLine = self.frame.height / 2
        self.player.position = CGPointMake(CGRectGetMinX(self.frame) + self.player.size.width / 1.5 , self.playerBaseLine)
        self.player.physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(self.player.size.width / 2))
        self.player.physicsBody!.affectedByGravity = false
        self.player.physicsBody!.categoryBitMask = Collision.Player.rawValue
        self.player.physicsBody!.contactTestBitMask = Collision.Block.rawValue
        self.player.physicsBody!.collisionBitMask = Collision.Block.rawValue

override func update(currentTime: NSTimeInterval) {

            self.velocityY  += self.gravity
            self.player.position.y -= velocityY

            if self.player.position.y < self.playerBaseLine {
                self.player.position.y = self.playerBaseLine
                velocityY = 0.0
                onGround = true
            }
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
            if self.onGround{
                self.velocityY = -18.0
                self.onGround = false
            }
    }

You need to play with gravity ;)