您好,我在 spritekit 中使用 swift 制作了一个简单的 2d 横向卷轴游戏,我面临一个我无法解决的问题。我有一个名为(createPlatforms)的函数来生成一些平台(每个平台都在另一个之上),并且平台将从右到左形成。我还添加了一个名为 isJumping 的 Bool 变量,我希望如果变量 isJumping 为真,那么平台物理体的值为 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:"latform")
Platform2 = SKSpriteNode(imageNamed:"latform")
Platform3 = SKSpriteNode(imageNamed:"latform-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()
}
}
平台的实体为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
}
}
请问有人可以帮我吗?我因为三个弱点而陷入困境,我现在真的很沮丧:-/我相信我的问题可以由比我更有编码经验的人轻松解决
这里的代码少了很多,更简洁,更易于管理。 (所有平台应共享同一类别,龙类别需小于平台)
注意:未在 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,如果条件为真,则将 Platforms nil 设为无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38508650/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) | Powered by Discuz! X3.4 |