gpt4 book ai didi

快速碰撞不起作用。我认为 didBeginContact 中的代码有问题

转载 作者:行者123 更新时间:2023-11-30 10:18:50 24 4
gpt4 key购买 nike

我一直在开发一款游戏,每次我的测试节点与我的检查节点接触时都必须执行测试。由于这两个节点是游戏中唯一检查碰撞的节点,因此我决定使用此方法来启动测试。

问题是当 2 个节点物理交叉时,我一直无法让碰撞发挥作用。

当前代码将移动测试方 block ,直到它到达检查方 block ,但现在当它们相遇时没有任何反应。

我错过了什么吗?任何帮助将不胜感激。谢谢。

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {


var test:SKSpriteNode!
var testTexture = SKTexture(imageNamed: "redsquare.png")
var check:SKSpriteNode!
var checkTexture = SKTexture(imageNamed: "bluesquare.png")
var moving:SKNode!
let testCategory: UInt32 = 1 << 0
let checkCategory: UInt32 = 1 << 2


override func didMoveToView(view: SKView) {
/* Setup your scene here */

self.physicsWorld.gravity = CGVectorMake(0.0, 0.0)
self.physicsWorld.contactDelegate = self

moving = SKNode()
self.addChild(moving)

//create test block

test = SKSpriteNode(texture: testTexture)
test.xScale = 0.1
test.yScale = 0.1
test.position = CGPointMake(0, self.frame.height - test.size.height)

test.physicsBody = SKPhysicsBody(rectangleOfSize: test.size)
test.physicsBody?.dynamic = false

test.physicsBody?.categoryBitMask = testCategory
test.physicsBody?.collisionBitMask = checkCategory
test.physicsBody?.contactTestBitMask = checkCategory

self.addChild(test)

//creates check block

check = SKSpriteNode(texture: checkTexture)
check.xScale = 0.1
check.yScale = 0.1
check.position = CGPointMake(self.frame.width, self.frame.height - check.size.height)

check.physicsBody = SKPhysicsBody(rectangleOfSize: check.size)
check.physicsBody?.dynamic = false

check.physicsBody?.categoryBitMask = checkCategory
check.physicsBody?.collisionBitMask = testCategory
check.physicsBody?.contactTestBitMask = testCategory

self.addChild(check)


// move action

let actionMove = SKAction.moveToX(self.frame.width, duration: 1.5)
let actionMoveDone = SKAction.moveToX(0, duration: 0)

let moveForever = SKAction.repeatActionForever(SKAction.sequence([actionMove,actionMoveDone]))
test.runAction(moveForever)


}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */

for touch: AnyObject in touches {

}
}

override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}

func didBeginContact(contact: SKPhysicsContact) {

// help here!

var firstBody:SKPhysicsBody
var secondBody:SKPhysicsBody
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask){
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else{
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if((firstBody.categoryBitMask & testCategory) != 0 && (secondBody.categoryBitMask & checkCategory) != 0){


println("contact!")

}


}

}

最佳答案

根据Apple的文档,dynamic属性控制基于体积的物体是否受到重力、摩擦、与其他物体的碰撞以及直接施加到物体的力或冲量的影响。

dynamic property : The default value is true. If the value is false, the physics body ignores all forces and impulses applied to it. This property is ignored on edge-based bodies; they are automatically static.

因此,将您的 dynamic 属性设置为 true。

node.physicsBody?.dynamic = true

了解更多信息:https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKPhysicsBody_Ref/index.html#//apple_ref/occ/instp/SKPhysicsBody/

关于快速碰撞不起作用。我认为 didBeginContact 中的代码有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28470430/

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