SpriteKit & Swift,如果条件为真,则将平台设置为 nil 将不起作用
SpriteKit & Swift, make Platforms nil if a condition is true wont work
嗨,我用 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()
}
}
平台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
}
}
有人可以帮助我吗?我因为三个弱点而陷入困境,我现在真的很沮丧:-/我相信我的问题可以由比我更有编码经验的人轻松解决
如果其他人遇到同样的问题,@Knight0fDragon 知道我的问题的解决方案。正如他所说,我所做的非常低效。最好不要在默认情况下在平台和播放器之间设置 collisionBitMask,当播放器的速度为负时将其打开,然后在 didEndContact 方法中再次将其关闭。
再次感谢@Knight0fDragon,我已经为这个问题苦苦挣扎了好几个星期。
这是为我完成的代码:
func didBeginContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
// check if on Platform
if (firstBody.categoryBitMask == PhysicsCatagory.Dragon && secondBody.categoryBitMask == PhysicsCatagory.Platform1 || firstBody.categoryBitMask == PhysicsCatagory.Platform1 && secondBody.categoryBitMask == PhysicsCatagory.Dragon) {
if Dragon.physicsBody?.velocity.dy <= -0.0 {
Platform1.physicsBody?.collisionBitMask = PhysicsCatagory.Dragon
Dragon.physicsBody?.collisionBitMask = PhysicsCatagory.Platform1
}
}
if (firstBody.categoryBitMask == PhysicsCatagory.Dragon && secondBody.categoryBitMask == PhysicsCatagory.Platform2 || firstBody.categoryBitMask == PhysicsCatagory.Platform2 && secondBody.categoryBitMask == PhysicsCatagory.Dragon) {
if Dragon.physicsBody?.velocity.dy <= -0.0 {
Platform2.physicsBody?.collisionBitMask = PhysicsCatagory.Dragon
Dragon.physicsBody?.collisionBitMask = PhysicsCatagory.Platform2
}
}
if (firstBody.categoryBitMask == PhysicsCatagory.Dragon && secondBody.categoryBitMask == PhysicsCatagory.Platform3 || firstBody.categoryBitMask == PhysicsCatagory.Platform3 && secondBody.categoryBitMask == PhysicsCatagory.Dragon) {
if Dragon.physicsBody?.velocity.dy <= -0.0 {
Platform3.physicsBody?.collisionBitMask = PhysicsCatagory.Dragon
Dragon.physicsBody?.collisionBitMask = PhysicsCatagory.Platform3
}
}
}
func didEndContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if (firstBody.categoryBitMask == PhysicsCatagory.Dragon && secondBody.categoryBitMask == PhysicsCatagory.Platform1 || firstBody.categoryBitMask == PhysicsCatagory.Platform1 && secondBody.categoryBitMask == PhysicsCatagory.Dragon) {
Platform1.physicsBody?.collisionBitMask = 0
Dragon.physicsBody?.collisionBitMask = PhysicsCatagory.Ground
}
if (firstBody.categoryBitMask == PhysicsCatagory.Dragon && secondBody.categoryBitMask == PhysicsCatagory.Platform2 || firstBody.categoryBitMask == PhysicsCatagory.Platform2 && secondBody.categoryBitMask == PhysicsCatagory.Dragon) {
if Dragon.physicsBody?.velocity.dy <= -0.0 {
Platform2.physicsBody?.collisionBitMask = 0
Dragon.physicsBody?.collisionBitMask = PhysicsCatagory.Ground
}
}
if (firstBody.categoryBitMask == PhysicsCatagory.Dragon && secondBody.categoryBitMask == PhysicsCatagory.Platform3 || firstBody.categoryBitMask == PhysicsCatagory.Platform3 && secondBody.categoryBitMask == PhysicsCatagory.Dragon) {
if Dragon.physicsBody?.velocity.dy <= -0.0 {
Platform3.physicsBody?.collisionBitMask = 0
Dragon.physicsBody?.collisionBitMask = PhysicsCatagory.Ground
}
}
}
}
这里的代码少了很多,更干净,更易于管理。 (所有平台应共享同一类别,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)
}
}
嗨,我用 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()
}
}
平台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
}
}
有人可以帮助我吗?我因为三个弱点而陷入困境,我现在真的很沮丧:-/我相信我的问题可以由比我更有编码经验的人轻松解决
如果其他人遇到同样的问题,@Knight0fDragon 知道我的问题的解决方案。正如他所说,我所做的非常低效。最好不要在默认情况下在平台和播放器之间设置 collisionBitMask,当播放器的速度为负时将其打开,然后在 didEndContact 方法中再次将其关闭。
再次感谢@Knight0fDragon,我已经为这个问题苦苦挣扎了好几个星期。
这是为我完成的代码:
func didBeginContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
// check if on Platform
if (firstBody.categoryBitMask == PhysicsCatagory.Dragon && secondBody.categoryBitMask == PhysicsCatagory.Platform1 || firstBody.categoryBitMask == PhysicsCatagory.Platform1 && secondBody.categoryBitMask == PhysicsCatagory.Dragon) {
if Dragon.physicsBody?.velocity.dy <= -0.0 {
Platform1.physicsBody?.collisionBitMask = PhysicsCatagory.Dragon
Dragon.physicsBody?.collisionBitMask = PhysicsCatagory.Platform1
}
}
if (firstBody.categoryBitMask == PhysicsCatagory.Dragon && secondBody.categoryBitMask == PhysicsCatagory.Platform2 || firstBody.categoryBitMask == PhysicsCatagory.Platform2 && secondBody.categoryBitMask == PhysicsCatagory.Dragon) {
if Dragon.physicsBody?.velocity.dy <= -0.0 {
Platform2.physicsBody?.collisionBitMask = PhysicsCatagory.Dragon
Dragon.physicsBody?.collisionBitMask = PhysicsCatagory.Platform2
}
}
if (firstBody.categoryBitMask == PhysicsCatagory.Dragon && secondBody.categoryBitMask == PhysicsCatagory.Platform3 || firstBody.categoryBitMask == PhysicsCatagory.Platform3 && secondBody.categoryBitMask == PhysicsCatagory.Dragon) {
if Dragon.physicsBody?.velocity.dy <= -0.0 {
Platform3.physicsBody?.collisionBitMask = PhysicsCatagory.Dragon
Dragon.physicsBody?.collisionBitMask = PhysicsCatagory.Platform3
}
}
}
func didEndContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if (firstBody.categoryBitMask == PhysicsCatagory.Dragon && secondBody.categoryBitMask == PhysicsCatagory.Platform1 || firstBody.categoryBitMask == PhysicsCatagory.Platform1 && secondBody.categoryBitMask == PhysicsCatagory.Dragon) {
Platform1.physicsBody?.collisionBitMask = 0
Dragon.physicsBody?.collisionBitMask = PhysicsCatagory.Ground
}
if (firstBody.categoryBitMask == PhysicsCatagory.Dragon && secondBody.categoryBitMask == PhysicsCatagory.Platform2 || firstBody.categoryBitMask == PhysicsCatagory.Platform2 && secondBody.categoryBitMask == PhysicsCatagory.Dragon) {
if Dragon.physicsBody?.velocity.dy <= -0.0 {
Platform2.physicsBody?.collisionBitMask = 0
Dragon.physicsBody?.collisionBitMask = PhysicsCatagory.Ground
}
}
if (firstBody.categoryBitMask == PhysicsCatagory.Dragon && secondBody.categoryBitMask == PhysicsCatagory.Platform3 || firstBody.categoryBitMask == PhysicsCatagory.Platform3 && secondBody.categoryBitMask == PhysicsCatagory.Dragon) {
if Dragon.physicsBody?.velocity.dy <= -0.0 {
Platform3.physicsBody?.collisionBitMask = 0
Dragon.physicsBody?.collisionBitMask = PhysicsCatagory.Ground
}
}
} }
这里的代码少了很多,更干净,更易于管理。 (所有平台应共享同一类别,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)
}
}