gpt4 book ai didi

ios - 你如何让一个 Sprite 只在地面上 swift 跳跃?

转载 作者:行者123 更新时间:2023-11-28 06:11:53 25 4
gpt4 key购买 nike

我正在使用 SpriteKit 制作一个简单的平台游戏。游戏正在使用 func didBegin 检测玩家何时触地,但由于某种原因, bool 值 playerTouchingGround(在我的 Player 类中)没有更改为 true。我将 var playerTouchingGround 初始化为 true,玩家可以跳跃一次,但不能再次跳跃。输出控制台打印“hit ground; left ground; hit ground”。我不明白我做错了什么。我可能忽略了一些简单的东西。

这是我的 GameScene 代码

import SpriteKit
import GameplayKit

class GameScene: SKScene{

let player: Player!
let ground: Ground!
let controller: ControllerNode!

let contactManager = Player()


// MARK: - Init


override init(size: CGSize) {

player = Player()
ground = Ground(size: size)
controller = ControllerNode(position: CGPoint(x:0, y: 0))

super.init(size: size)

self.physicsWorld.contactDelegate = contactManager

setup()

}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}



// MARK: - Setup

func setup() {


// Background Color
self.backgroundColor = UIColor.white


// Adding Player
addChild(player)
player.position.x = size.width/2
player.position.y = size.height/2


// Adding Ground
addChild(ground)
ground.position.x = size.width/2
ground.position.y = size.height/4


// Adding ControllerNode
addChild(controller)

}


override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
player.movement(directions: self.controller!.pressedDirections())

}
}

这是我的播放器类代码

import SpriteKit
enum CharacterMovement : Int {

case Up
case Right
case Down
case Left

}

enum ColliderType: UInt32 {

case player = 1
case ground = 2
}

class Player: SKSpriteNode, SKPhysicsContactDelegate {


var tick = 0


var currentMovement: CharacterMovement?


var lastMovement: CharacterMovement?


var contactMask: Int = 0

var playerTouchingGround = true

// MARK: - Init

init() {



let playerSize = CGSize(width: 30, height: 60)

super.init(texture: nil, color: UIColor.cyan, size: playerSize)

lastMovement = .Right

self.zPosition = 10

setup()

}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}


// MARK: - Setup

func setup() {


physicsBody = SKPhysicsBody(rectangleOf: size)
physicsBody?.usesPreciseCollisionDetection = true
physicsBody?.categoryBitMask = ColliderType.player.rawValue
physicsBody?.collisionBitMask = ColliderType.ground.rawValue
physicsBody?.contactTestBitMask = ColliderType.ground.rawValue
physicsBody?.allowsRotation = false
physicsBody?.restitution = 0
physicsBody?.isDynamic = true

}

func didBegin(_ contact: SKPhysicsContact) {

print("hit ground")
playerTouchingGround = true


}




func movement(directions: [ControllerInput]) {



// if (directions.index(of: .Up) != nil) && (touchingGround == true){

if (directions.index(of: .Up) != nil) && (playerTouchingGround) {


physicsBody?.applyImpulse(CGVector(dx: 0, dy: 30))
playerTouchingGround = false
currentMovement = .Up
print("left ground")
}

if directions.index(of: .Right) != nil {

physicsBody?.applyImpulse(CGVector(dx: 1, dy: 0))
currentMovement = .Right
}

if directions.index(of: .Left) != nil {

physicsBody?.applyImpulse(CGVector(dx: -1, dy: 0))
currentMovement = .Left
}

if currentMovement != nil {
lastMovement = currentMovement
}

if !directions.isEmpty && self.currentMovement != nil {
// Player Animations
}
else {
currentMovement = nil
}

tick += 1
if tick > 60 {
tick = 0
}

}

}

这是我的 Ground 类代码

import SpriteKit

class Ground: SKSpriteNode {


// MARK: - Init

init(size: CGSize) {

let groundSize = CGSize(width: size.width, height: 20)

super.init(texture: nil, color: UIColor.black, size: groundSize)

setup()
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}


// MARK: - Setup

func setup() {

physicsBody = SKPhysicsBody(rectangleOf: size)
physicsBody?.categoryBitMask = ColliderType.ground.rawValue
physicsBody?.usesPreciseCollisionDetection = true
physicsBody?.isDynamic = false
physicsBody?.restitution = 0
physicsBody?.categoryBitMask = ColliderType.ground.rawValue
physicsBody?.collisionBitMask = ColliderType.player.rawValue
physicsBody?.contactTestBitMask = ColliderType.player.rawValue

}
}

最佳答案

您的问题是您正在创建多个 Player 对象。

每次调用 Player() 时,都会创建一个全新的播放器。

这意味着 contactManagerplayer 不是同一个实例

let contactManager = Player()  //<---Alright a player


// MARK: - Init


override init(size: CGSize) {

player = Player()// <-----Uh oh, a new player, need to fix this
ground = Ground(size: size)
controller = ControllerNode(position: CGPoint(x:0, y: 0))

super.init(size: size)

self.physicsWorld.contactDelegate = contactManager

setup()

}

关于ios - 你如何让一个 Sprite 只在地面上 swift 跳跃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46308696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com