gpt4 book ai didi

ios - SpriteKit 和 Swift,如果条件为真,则将平台设置为 nil 将不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:58:56 25 4
gpt4 key购买 nike

您好,我正在使用 swift 在 spritekit 中制作一个简单的 2d sidescroller 游戏,我遇到了一个我无法解决的问题。我有一个名为 (createPlatforms) 的函数来生成一些平台(每个平台都在另一个平台之上)并且平台从右到左移动。我还添加了一个名为 isJumping 的 Bool 变量,我希望如果变量 isJumping 为真,则平台 physicsbody 的值为 nil,以便玩家可以跳到它上面并降落在它上面。我有点设法做到了。它适用于第一个平台,但不适用于其他平台。玩家跳到第一个平台上方并降落在上面,但他不能跳到第二个或第三个平台上方。有时大约有 10% 的时间它适用于所有平台,他可以跳到上面并降落在它们上,但大多数时候它不起作用。

提前谢谢你。

我的跳转方法:

func jump() {

isJumping = true

let jumpBlock = SKAction.runBlock({
() in
self.Dragon.physicsBody?.velocity = CGVectorMake(0, 0)
self.Dragon.physicsBody?.applyImpulse(CGVectorMake(0, 140)) // Jump Impulse
})

self.runAction(jumpBlock)

runAfterDelay(0.3) { /* 0.3 Seconds */
self.isJumping = false
}
}
//Delay function
func runAfterDelay(delay: NSTimeInterval, block: dispatch_block_t) {
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
dispatch_after(time, dispatch_get_main_queue(), block)
}

生成平台的方法:

 func createPlatforms() {

Platform1 = SKSpriteNode(imageNamed:"Platform")
Platform2 = SKSpriteNode(imageNamed:"Platform")
Platform3 = SKSpriteNode(imageNamed:"Platform-Small")

Platform1.size = CGSize(width: frame.width / 3, height: frame.height / 25)
Platform2.size = CGSize(width: frame.width / 3, height: frame.height / 25)
Platform3.size = CGSize(width: frame.width / 6, height: frame.height / 25)
Platform1.position = CGPoint(x: self.frame.width + 330, y: self.frame.height - 285)
Platform2.position = CGPoint(x: self.frame.width + 500, y: self.frame.height - 210)
Platform3.position = CGPoint(x: self.frame.width + 570, y: self.frame.height - 135)

Platform1.physicsBody = SKPhysicsBody(rectangleOfSize: Platform1.size)
Platform1.zPosition = 6
Platform1.physicsBody?.categoryBitMask = PhysicsCatagory.Platform1
Platform1.physicsBody?.contactTestBitMask = PhysicsCatagory.Dragon
Platform1.physicsBody?.collisionBitMask = PhysicsCatagory.Dragon
Platform1.physicsBody?.dynamic = false

Platform2.physicsBody = SKPhysicsBody(rectangleOfSize: Platform2.size)
Platform2.zPosition = 7
Platform2.physicsBody?.categoryBitMask = PhysicsCatagory.Platform2
Platform2.physicsBody?.contactTestBitMask = PhysicsCatagory.Dragon
Platform2.physicsBody?.collisionBitMask = PhysicsCatagory.Dragon
Platform2.physicsBody?.dynamic = false

Platform3.physicsBody = SKPhysicsBody(rectangleOfSize: Platform3.size)
Platform3.zPosition = 8
Platform3.physicsBody?.categoryBitMask = PhysicsCatagory.Platform3
Platform3.physicsBody?.contactTestBitMask = PhysicsCatagory.Dragon
Platform3.physicsBody?.collisionBitMask = PhysicsCatagory.Dragon
Platform3.physicsBody?.dynamic = false

Platform1.runAction(moveAndRemove)
Platform2.runAction(moveAndRemove)
Platform3.runAction(moveAndRemove)

self.addChild(Platform1)
self.addChild(Platform2)
self.addChild(Platform3)

let distance = CGFloat(self.frame.width + 1000)
let moveGroundObjects = SKAction.moveByX(-distance * 1.3, y: 0, duration: NSTimeInterval(0.011 * distance))
let removeGroundObjects = SKAction.removeFromParent()
moveAndRemove = SKAction.sequence([moveGroundObjects, removeGroundObjects])


runAfterDelay(7) {
self.createPlatforms()
}
}

让platform的physicbody为nil的方法:

func makePlatformsNil() {

if isJumping == true {

Platform1.physicsBody = nil
Platform2.physicsBody = nil
Platform3.physicsBody = nil

} else if isJumping == false {

Platform1.physicsBody = SKPhysicsBody(rectangleOfSize: Platform1.size)
Platform1.physicsBody?.categoryBitMask = PhysicsCatagory.Platform1
Platform1.physicsBody?.collisionBitMask = PhysicsCatagory.Dragon
Platform1.physicsBody?.contactTestBitMask = PhysicsCatagory.Dragon
Platform1.physicsBody?.dynamic = false
Platform1.physicsBody?.allowsRotation = false

Platform2.physicsBody = SKPhysicsBody(rectangleOfSize: Platform2.size)
Platform2.physicsBody?.categoryBitMask = PhysicsCatagory.Platform2
Platform2.physicsBody?.collisionBitMask = PhysicsCatagory.Dragon
Platform2.physicsBody?.contactTestBitMask = PhysicsCatagory.Dragon
Platform2.physicsBody?.dynamic = false
Platform2.physicsBody?.affectedByGravity = false

Platform3.physicsBody = SKPhysicsBody(rectangleOfSize: Platform3.size)
Platform3.physicsBody?.categoryBitMask = PhysicsCatagory.Platform3
Platform3.physicsBody?.collisionBitMask = PhysicsCatagory.Dragon
Platform3.physicsBody?.contactTestBitMask = PhysicsCatagory.Dragon
Platform3.physicsBody?.dynamic = false
Platform3.physicsBody?.affectedByGravity = false
}
}

有人可以帮帮我吗?我因为三个弱点而陷入困境,我现在真的很沮丧:-/我相信我的问题可以由比我更有编码经验的人轻松解决

最佳答案

这里的代码少了很多,更干净也更容易管理。 (所有平台应该共享同一个类别,Dragon类别需要小于Platform)

注意:未在 XCode 上进行验证测试

func didBeginContact(contact: SKPhysicsContact) {
let (firstBody,secondBody) = (contact.bodyA.categoryBitMask <= contact.bodyB.categoryBitMask) ? (contact.bodyA,contact.bodyB) : (contact.bodyB,contact.bodyA)

// check if on Platform

if (firstBody.categoryBitMask == PhysicsCatagory.Dragon && secondBody.categoryBitMask == PhysicsCatagory.Platform) {

if firstBody.velocity.dy < -0.0 {

secondBody.collisionBitMask |= physicsCatagory.Dragon
}

}



}

func didEndContact(contact: SKPhysicsContact) {
let (firstBody,secondBody) = (contact.bodyA.categoryBitMask <= contact.bodyB.categoryBitMask) ? (contact.bodyA,contact.bodyB) : (contact.bodyB,contact.bodyA)

// check if on Platform

if (firstBody.categoryBitMask == PhysicsCatagory.Dragon && secondBody.categoryBitMask == PhysicsCatagory.Platform) {

//No matter what way dragon is moving, if he is off the platform, let's turn off the collision

secondBody.collisionBitMask &= ~(physicsCatagory.Dragon)


}

}

关于ios - SpriteKit 和 Swift,如果条件为真,则将平台设置为 nil 将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38508650/

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