问题描述
所以我在游戏中对球进行了一些调整,为避免出现可选值错误,我对以下内容进行了表扬
So I made some adjustments to the ball in my game and to avoid an unwrapping optional value error, I commmented out the following
//didMoveToView
self.physicsWorld.gravity = CGVectorMake(0, 0)
self.physicsWorld.contactDelegate = self
let fieldBody = SKPhysicsBody.init(edgeLoopFromRect: self.frame)
self.physicsBody = fieldBody
self.physicsBody!.affectedByGravity = false
self.physicsBody!.usesPreciseCollisionDetection = true
self.physicsBody!.dynamic = true
self.physicsBody!.mass = 0
self.physicsBody!.friction = 0
self.physicsBody!.linearDamping = 0
self.physicsBody!.angularDamping = 0
self.physicsBody!.restitution = 1
self.physicsBody!.categoryBitMask = CollisionTypes.Floor.rawValue
self.physicsBody!.contactTestBitMask = CollisionTypes.Ball.rawValue
// Prepare the ball - physics engine.
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.width/2)
ball.physicsBody!.affectedByGravity = true
ball.physicsBody!.restitution = 0.8
ball.physicsBody!.linearDamping = 0
ball.physicsBody!.friction = 0.3
ball.physicsBody!.dynamic = true
ball.physicsBody!.mass = 0.5
ball.physicsBody!.allowsRotation = true
ball.physicsBody!.categoryBitMask = CollisionTypes.Ball.rawValue
ball.physicsBody!.contactTestBitMask = CollisionTypes.Floor.rawValue
ball.physicsBody!.collisionBitMask = CollisionTypes.Floor.rawValue
}
我做到了,我的游戏开始了,但是toucheBegan出现了同样的错误.我不知道如何解决所有这些无法解决的错误.我是新手,不知道如何调整这些物理学.我的contactBegin看起来像这样:
I did that, my game launced but as soon as toucheBegan, the same error popped up. I do not know how to get around all of these unrwapping errors. I am new to swift and not sure how i should adjust thse physics. My contactBegin looks this:
func didBeginContact(contact: SKPhysicsContact) {
let bitMaskAAndB = contact.bodyA.categoryBitMask == CollisionTypes.Floor.rawValue &&
contact.bodyB.categoryBitMask == CollisionTypes.Ball.rawValue
let ballAndBoardMask = CollideType.Ball.toMask() | boards.usedCollideMasks
// ball and board, handle it by board delegate
if bitMaskAAndB | ballAndBoardMask == ballAndBoardMask {
let boardNode: SKNode! = contact.bodyA.categoryBitMask == CollideType.Ball.toMask() ? contact.bodyB.node : contact.bodyA.node
let board = boardNode.bind as! BoardDelegate
board.didBeginContact(boardNode, ball: ball, contact: contact, game: self)
}
// ball and ceil => stop game
else if bitMaskAAndB == CollideType.toMask([.Ball, .Ceil]) {
stopGame()
}
// ball and floor => stop game add explosion or fall off screen
else if bitMaskAAndB == CollideType.toMask([.Ball, .Floor]) {
stopGame()
}
}
func didEndContact(contact: SKPhysicsContact) {
let ballAndBoardMask = CollideType.Ball.toMask() | boards.usedCollideMasks
// ball and board, handle it by board delegate
if contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask | ballAndBoardMask == ballAndBoardMask {
let boardNode: SKNode! = contact.bodyA.categoryBitMask == CollideType.Ball.toMask() ? contact.bodyB.node : contact.bodyA.node
let board = boardNode.bind as! BoardDelegate
board.didEndContact(boardNode, ball: ball, contact: contact, game: self)
}
}
我根据您的建议设置了物理世界,并调整了didBeginContact的乐趣,但是,使用以下代码,我遇到了多个错误:无法将bool类型的值转换为UInt32.
I set up my physics world to the suggestions you made and adjusted my didBeginContact fun however with the following code, I am getting multiple errors regarding: cannot convert value of type bool to UInt32.
PS.感谢您的帮助和耐心等待,这是所有新术语,我将尽我所能来理解. :)
PS. thank you for your help and patience, this is all new terminology and Im doing my best to understand. :)
推荐答案
我不知道您的CollideType
类或声明,因此下面的代码向您解释了如何构建正确的physicsWorld
和physicsBody
来在碰撞过程中没有问题.
I don't know your CollideType
class or declaration, so my code below explain to you how to build a correct physicsWorld
and physicsBody
to don't have issues during the collisions.
但是首先:我们将在此项目中最大程度地使用 categoryBitMask , contactTestBitMask 和 collisionBitMask 属性,因为我们具有使项目正常运行的非常精确的规则:
But first: we're going to be using the categoryBitMask, contactTestBitMask and collisionBitMask properties in their fullest for this project, because we have very precise rules that make the project work:
-
categoryBitMask 是一个数字,用于定义此对象的类型考虑碰撞.
categoryBitMask is a number defining the type of object this is forconsidering collisions.
collisionBitMask 是一个数字,用于定义此对象的类别节点应该碰撞.
collisionBitMask is a number defining what categories of object thisnode should collide with.
contactTestBitMask 是一个数字,用于定义我们要发生的碰撞被通知.
contactTestBitMask is a number defining which collisions we want tobe notified about.
SpriteKit期望使用UInt32
描述这三个位掩码.您的位掩码应从1开始,然后每次加倍.
SpriteKit expects these three bitmasks to be described using a UInt32
.Your bitmasks should start at 1 then double each time.
代码:
class GameScene: SKScene, SKPhysicsContactDelegate {
var ball: Ball!
let ballSpeedX = CGFloat(500)
enum CollisionTypes: UInt32 {
case Field = 1
case Ball = 2
}
override func didMoveToView(view: SKView) {
// ball
ball = Ball(imageNamed:"colorBall.png")
ball.position = CGPointMake(self.ball.frame.size.width,self.ball.frame.size.height)
// Prepare the world - physics engine.
self.physicsWorld.gravity = CGVectorMake(0, -6)
self.physicsWorld.contactDelegate = self
let fieldBody = SKPhysicsBody.init(edgeLoopFromRect: self.frame)
self.physicsBody = fieldBody
self.physicsBody!.affectedByGravity = false
self.physicsBody!.usesPreciseCollisionDetection = true
self.physicsBody!.dynamic = true
self.physicsBody!.mass = 0
self.physicsBody!.friction = 0
self.physicsBody!.linearDamping = 0
self.physicsBody!.angularDamping = 0
self.physicsBody!.restitution = 1
self.physicsBody!.categoryBitMask = CollisionTypes.Field.rawValue
self.physicsBody!.contactTestBitMask = CollisionTypes.Ball.rawValue
// Prepare the ball - physics engine.
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.width/2)
ball.physicsBody!.affectedByGravity = true
ball.physicsBody!.restitution = 0.8
ball.physicsBody!.linearDamping = 0
ball.physicsBody!.friction = 0.3
ball.physicsBody!.dynamic = true
ball.physicsBody!.mass = 0.5
ball.physicsBody!.allowsRotation = true
ball.physicsBody!.categoryBitMask = CollisionTypes.Ball.rawValue
ball.physicsBody!.contactTestBitMask = CollisionTypes.Field.rawValue
ball.physicsBody!.collisionBitMask = CollisionTypes.Field.rawValue
//ball.physicsBody!.categoryBitMask = CollideType.Ball.toMask()
//ball.physicsBody!.collisionBitMask = CollideType.toMask([.Scene, .Ceil, .Floor]) | boards.usedCollideMasks
//ball.physicsBody!.contactTestBitMask = CollideType.toMask([.Scene, .Ceil, .Floor]) | boards.usedCollideMasks
ball.hidden = false
self.addChild(ball)
}
func didBeginContact(contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == CollisionTypes.Field.rawValue &&
contact.bodyB.categoryBitMask == CollisionTypes.Ball.rawValue) {
print("contact between field and ball")
}
}
}
如您所见,第一个重要的事情是准备physicsWorld(跟踪您的物理必须起作用的边界),为其指定一个类别,并且所有其他参数需要与其他对象进行交互.然后,您将构建对象的PhysicalBody(在本例中为球).
As you can see, the first important thing is to prepare the physicsWorld (track the boundaries within wich your physics must work), assign it a category and all the other parameters need to interact with the other objects. Then you build the physicsBody of your object , in this case the ball.
这篇关于展开可选值:PhysicsBody-Swift SpriteKit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!