gpt4 book ai didi

swift - 碰撞检测 Spritekit 位掩码

转载 作者:行者123 更新时间:2023-11-30 14:03:46 25 4
gpt4 key购买 nike

我花了几周时间尝试使用 BitMasking 完全掌握 Sprite-Kit 中的碰撞检测。我搜索了 google youtube 但找不到任何东西。我将发布我现在拥有的代码。当我的两个 SKSpriteNode 相互接触时,我现在拥有的代码当前无法工作。有人可以告诉我如何修复我的代码并告诉我步骤及其含义。

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate{

let ballCategory:UInt32 = 0x1 << 0
let boxCategory:UInt32 = 0x1 << 1
let ball = SKSpriteNode(imageNamed: "redball")
let redBall = SKSpriteNode(imageNamed: "redball")


override func didMoveToView(view: SKView) {
self.physicsWorld.contactDelegate = self
let box = SKSpriteNode(imageNamed: "box")
box.position = CGPoint(x: 400, y: 400)
box.physicsBody?.categoryBitMask = boxCategory
box.physicsBody?.contactTestBitMask = boxCategory
box.physicsBody?.collisionBitMask = boxCategory | ballCategory
addChild(box)

redBall.position = CGPoint(x: 200, y: 200)
redBall.physicsBody?.categoryBitMask = ballCategory
redBall.physicsBody?.contactTestBitMask = ballCategory
redBall.physicsBody?.collisionBitMask = ballCategory | boxCategory
addChild(redBall)

//Backround Properties
backgroundColor = UIColor(red: 70.0/255.0, green: 67.0/255.0, blue: 67.0/255, alpha: 1)

}




override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
redBall.position = touchLocation

}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
redBall.position = touchLocation
}
func didBeginContact(contact: SKPhysicsContact) {
let contactBodyA = SKSpriteNode()
let contactBodyB = SKSpriteNode()

if(contact.bodyA.categoryBitMask == ballCategory) && (contact.bodyB.categoryBitMask == boxCategory){

println("contact was made")
}
}
}

最佳答案

我已经修改了你的一些代码,现在它工作正常。这是您的代码:

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate{

let ballCategory:UInt32 = 0x1 << 0
let boxCategory:UInt32 = 0x1 << 1

let ball = SKSpriteNode(imageNamed: "redball")
let redBall = SKSpriteNode(imageNamed: "redball")


override func didMoveToView(view: SKView) {
self.physicsWorld.contactDelegate = self
//make gravity 0
self.physicsWorld.gravity = CGVectorMake(0.0, 0.0)

let box = SKSpriteNode(imageNamed: "redball")
box.position = CGPoint(x: 400, y: 400)
//add physicsbody for collisin detection
box.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "redball"), size: box.size)
//assign a correct cetogary
box.physicsBody?.categoryBitMask = boxCategory
box.physicsBody?.collisionBitMask = ballCategory
box.physicsBody?.contactTestBitMask = ballCategory
addChild(box)

redBall.position = CGPoint(x: 200, y: 200)
//add physics body
redBall.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "redball"), size: redBall.size)
//assign correct cetogary
redBall.physicsBody?.categoryBitMask = ballCategory
redBall.physicsBody?.contactTestBitMask = boxCategory
redBall.physicsBody?.collisionBitMask = boxCategory
addChild(redBall)

//Backround Properties
backgroundColor = UIColor(red: 70.0/255.0, green: 67.0/255.0, blue: 67.0/255, alpha: 1)

}




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

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first!
let touchLocation = touch.locationInNode(self)
redBall.position = touchLocation
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first!
let touchLocation = touch.locationInNode(self)
redBall.position = touchLocation
}

func didBeginContact(contact: SKPhysicsContact) {
// 1. Create local variables for two physics bodies
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody

// 2. Assign the two physics bodies so that the one with the lower category is always stored in firstBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}

// 3. react to the contact between ball and bottom
if firstBody.categoryBitMask == ballCategory && secondBody.categoryBitMask == boxCategory {
//TODO: Replace the log statement with display of Game Over Scene
//add your code here.
print("Called")
}
}
}

使用 xCode 7 进行测试。

HERE是关于 spriteKit 的很棒的教程,您可以在其中了解更多信息。

关于swift - 碰撞检测 Spritekit 位掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32656926/

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