gpt4 book ai didi

swift - 从两个对象检测 SpriteKit 上的碰撞

转载 作者:搜寻专家 更新时间:2023-10-31 22:49:34 24 4
gpt4 key购买 nike

当我用大炮射击时,我在检测两个物体的碰撞时遇到了一些问题。目标是从中射出一颗子弹,并在它与障碍物碰撞后立即移除。似乎游戏中根本没有发生碰撞,因为我无法收到我设置的“碰撞”消息。看看下面的代码:

class GameScene: SKScene, SKPhysicsContactDelegate {


enum BodyType: UInt32 {
case bullet = 1
case line = 2
case breaker = 4
}

我已经列举了组件,所以我可以将它们设置在类别位掩码中。之后,我在触摸开始时进行了拍摄设置。

 override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

/* Called when a touch begins */

for touch in (touches as! Set<UITouch>) {

let bullet = SKSpriteNode(imageNamed: "bullet.png")

bullet.position = self.shooterTriangle.position

var actionRotate = SKAction.rotateToAngle(CGFloat(2 * -M_PI), duration: NSTimeInterval(0.1))

let actionMove = SKAction.moveTo(CGPoint(x: 500, y: size.height * 0.5), duration: NSTimeInterval(0.5))

let removeBullet = SKAction.removeFromParent()

bullet.runAction(SKAction.sequence([actionRotate]))

bullet.runAction(SKAction.sequence([actionMove, removeBullet]))

addChild(bullet)

// Bullet Physics Start ****

bullet.physicsBody? = SKPhysicsBody(circleOfRadius: bullet.size.width/2.0)

bullet.physicsBody?.dynamic = true
bullet.physicsBody?.affectedByGravity = false
bullet.physicsBody?.allowsRotation = false
bullet.physicsBody?.categoryBitMask = BodyType.bullet.rawValue
bullet.physicsBody?.contactTestBitMask = BodyType.line.rawValue
bullet.physicsBody?.collisionBitMask = BodyType.line.rawValue

}
}

然后我在 loadGameComponents 函数中添加了我希望子弹与之碰撞的行......

func loadGameComponents() {

//Load line....

let line = SKSpriteNode(imageNamed: "Line_1.png")

line.position = CGPoint(x: size.width * 0.95, y: size.height * (2.0))

addChild(line)

var actualSpeedDuration = 2.2

let lineActionMove = SKAction.moveTo(CGPoint(x: size.width * 0.95, y: size.height * 0.6), duration: NSTimeInterval(actualSpeedDuration))

line.runAction(SKAction.sequence([lineActionMove]))

// Line Physics starts! *****

line.physicsBody? = SKPhysicsBody(rectangleOfSize: CGSize(width: line.size.width, height: line.size.height))

line.physicsBody?.dynamic = false
line.physicsBody?.affectedByGravity = false
line.physicsBody?.allowsRotation = false
line.physicsBody?.categoryBitMask = BodyType.line.rawValue
line.physicsBody?.contactTestBitMask = BodyType.bullet.rawValue
line.physicsBody?.collisionBitMask = 0

最后,我在我的 didLoad 函数中定义了 contactDelegate = self 并包含了 didBeginContact 方法

func didBeginContact(contact: SKPhysicsContact) {
println("Contact!!!")
}

我根本没有收到消息,但我不确定出了什么问题!

感谢您的耐心等待! :)

最佳答案

看起来主要的问题是我曾经在声明 Sprite 的物理体时放置可选指示符 (?)。应该在这行中进行更正...

line.physicsBody? = SKPhysicsBody(rectangleOfSize: CGSize(width: line.size.width, height: line.size.height))

bullet.physicsBody? = SKPhysicsBody(circleOfRadius: bullet.size.width/2.0)

删除它们解决了我的问题:)

我还在我的 didMoveToView 方法中包含了 view.showPhysics = true,因此很容易看到 Sprite 之间的边界。

关于swift - 从两个对象检测 SpriteKit 上的碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31812357/

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