gpt4 book ai didi

ios - 无法对 SKPhysicsbody 施加脉冲

转载 作者:行者123 更新时间:2023-11-29 05:55:19 25 4
gpt4 key购买 nike

所以我正在开发一种横向卷轴游戏,这是第一个使用 Xcode 和 swift 的游戏,但这是我所拥有的:

    import SpriteKit
import GameplayKit

let jumpButton = SKSpriteNode(imageNamed: "jumpButton")
var jumpButtonPosition = CGPoint(x: 500, y: -250)
var selectedButton = "nodeName"

let background1 = SKSpriteNode(imageNamed: "Woods_Background")
let background2 = SKSpriteNode(imageNamed: "Woods_Background")
let background3 = SKSpriteNode(imageNamed: "Woods_Background")

let player = SKSpriteNode(imageNamed: "player")
var playerColor = UIColor.red
var playerSize = CGSize(width: 50, height: 100)
var playerPhysicsBody = player.physicsBody
let playerJumpVector = CGVector(dx: 0, dy: 50)

let ground = SKSpriteNode()

var camPosition = CGPoint()
var playerPosition = camPosition
let cam = SKCameraNode()

var anchorpoint = CGPoint(x: 0.5, y: 0.5)
let endPoint = CGPoint(x: 100000, y: 0)

class GameScene: SKScene {

override func didMove(to view: SKView) {

super.didMove(to: view)
self.camera = cam
self.addChild(cam)

spawnPlayer()
spawnBackground()
spawnJumpButton()
}

func spawnGround(){
scene?.addChild(ground)
ground.size = CGSize(width: size.width * 1, height: size.height * 0.1)
ground.position = CGPoint(x: size.width * 0.5, y: size.height * 0.05)
ground.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: size.height, height: size.height * 0.1))
ground.physicsBody?.isDynamic = false
}

func spawnPlayer() {
scene?.addChild(player)
player.position = camPosition
player.zPosition = 1
player.size = playerSize
player.physicsBody = playerPhysicsBody
playerPhysicsBody?.affectedByGravity = false
playerPhysicsBody?.mass = 50
}

func spawnJumpButton() {
scene?.addChild(jumpButton)
jumpButton.position = jumpButtonPosition
jumpButton.zPosition = 0.5
jumpButton.size = CGSize(width: 100, height: 100)
jumpButton.name = "jumpButton"
jumpButton.isUserInteractionEnabled = false
}

func jumpPlayer() {
playerPhysicsBody?.applyImpulse(playerJumpVector)
}

func spawnBackground() {
scene?.addChild(background1)
background1.anchorPoint = CGPoint(x: 0.5, y: 0.5)
background1.position.x = 0
background1.zPosition = 0
background1.size = CGSize(width: 3240, height: 1080)

scene?.addChild(background2)
background2.anchorPoint = CGPoint(x: 0.5, y: 0.5)
background2.position.x = 3240
background2.zPosition = 0
background2.size = CGSize(width: 3240, height: 1080)

scene?.addChild(background3)
background3.anchorPoint = CGPoint(x: 0.5, y: 0.5)
background3.position.x = 6480
background3.zPosition = 0
background3.size = CGSize(width: 3240, height: 1080)
}

override func update(_ currentTime: TimeInterval) {

if cam.position.x > background2.position.x {
background1.position.x = background3.position.x + 3240
}
if cam.position.x > background3.position.x {
background2.position.x = background1.position.x + 3240
}
if cam.position.x > background1.position.x {
background3.position.x = background2.position.x + 3240
}
camPosition.x = playerPosition.x
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
if selectedButton == "nodeName" {
if jumpButton.contains(touch.location(in: self)) {
jumpPlayer()
}
}
}
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
selectedButton = "nodeName"
}
}

我试图让玩家使用按钮跳跃,点击时玩家应该在空中跳跃大约 20 像素。这同样适用于玩家的运行 Action ,不能让它在没有动画的情况下永远运行,而你不能同时做其他事情。

最佳答案

player.physicalBody为零,需要处理!

   func spawnPlayer() {
scene?.addChild(player)
player.position = camPosition
player.zPosition = 1
player.size = playerSize

//Here adding two lines
player.physicsBody = SKPhysicsBody()
playerPhysicsBody?.linearDamping = 1

playerPhysicsBody?.affectedByGravity = false
playerPhysicsBody?.mass = 50
}


func jumpPlayer() {
//Here changing one line
playerPhysicsBody?.applyImpulse(playerJumpVector)
}

关于ios - 无法对 SKPhysicsbody 施加脉冲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55226270/

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