hey i want my character to add a jumping animation to my doodlejump clone style. I think I know how to specify the sprites but I fail with the query isJumping = true. can someone help me?
the coin provides the platform on which the player lands the ground is only at the start
// GameScene.swift
// megaJump
//
// Created by Dennis Sergel on 26.06.23.
//
import Foundation
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate{
let background = SKSpriteNode(imageNamed: "background")
let player = SKSpriteNode(imageNamed: "Monster2")
let ground = SKSpriteNode(imageNamed: "ground")
let coin = SKSpriteNode(imageNamed: "coin")
enum bitmasks: UInt32 {
case player = 0b1
case coin = 0b10
}
override func didMove(to view: SKView) {
self.size = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
self.anchorPoint = .zero
background.position = CGPoint(x: size.width / 2, y: 1060)
background.zPosition = 1
background.setScale(1.3)
addChild(background)
physicsWorld.contactDelegate = self
ground.position = CGPoint(x: size.width / 2, y: 80)
ground.zPosition = 5
ground.setScale(2.5)
ground.physicsBody = SKPhysicsBody(rectangleOf: ground.size)
ground.physicsBody?.isDynamic = false
ground.physicsBody?.affectedByGravity = false
addChild(ground)
player.position = CGPoint(x: size.width / 2, y: 150)
player.zPosition = 80
player.setScale(0.14)
player.physicsBody = SKPhysicsBody(circleOfRadius:
player.size.height / 2)
player.physicsBody?.isDynamic = false // later is true
player.physicsBody?.restitution = 1
player.physicsBody?.friction = 0
player.physicsBody?.angularDamping = 0
player.physicsBody?.categoryBitMask = bitmasks.player.rawValue
player.physicsBody?.collisionBitMask = 0
player.physicsBody?.contactTestBitMask = bitmasks.coin.rawValue
addChild(player)
animatePlayer(isJumping: true)
makeCoin()
}
func animatePlayer (isJumping: Bool){
if isJumping {
player.texture = SKTexture(imageNamed: "monster")
}else{
player.texture = SKTexture(imageNamed: "Monster2")
}
}
func didBegin(_ contact: SKPhysicsContact) {
let contactA: SKPhysicsBody
let contactB: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask{
contactA = contact.bodyA // player
contactB = contact.bodyB // Coin
} else {
contactA = contact.bodyB // player
contactB = contact.bodyA // coin
}
if contactA.categoryBitMask == bitmasks.player.rawValue && contactB.categoryBitMask == bitmasks.coin.rawValue{
if player.physicsBody!.velocity.dy < 0 {
player.physicsBody?.velocity = CGVector(dx: player.physicsBody!.velocity.dx, dy: 120)
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
player.position.x = location.x
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
player.physicsBody?.isDynamic = true
player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 120))
}
func makeCoin() {
coin.position = CGPoint(x: size.width / 2, y: 150)
coin.zPosition = 5
coin.setScale(0.14)
coin.physicsBody = SKPhysicsBody(rectangleOf: coin.size)
coin.physicsBody?.isDynamic = false
coin.physicsBody?.allowsRotation = false
coin.physicsBody?.affectedByGravity = false
coin.physicsBody?.categoryBitMask = bitmasks.coin.rawValue
coin.physicsBody?.collisionBitMask = 0
coin.physicsBody?.contactTestBitMask = bitmasks.player.rawValue
addChild(coin)
}
}