gpt4 book ai didi

ios - 我如何正确检测接触和碰撞? swift iOS SKScene

转载 作者:可可西里 更新时间:2023-11-01 00:38:09 25 4
gpt4 key购买 nike

问题是我无法检测到碰撞//或以我在互联网上找到的任何方式联系

这是我的代码:

我的口罩声明:

private let ballCategory : UInt32 = 0x1 << 0
private let holeCategory : UInt32 = 0x1 << 1

球洞和球的加入:

func addHole(#size : CGSize) {

let actionMoveDown = SKAction.moveToY(CGRectGetMidY(self.frame)-500, duration: 4.7)
let hole = shapedHoles()
let UT = UTIL()
var position:CGFloat


let randomPosition = UT.randomNumberWith(Min: 1, Max: 3)

switch randomPosition{
case 1:
position = CGRectGetMidX(self.frame)
case 2:
position = CGRectGetMidX(self.frame)+size.width

default:
position = CGRectGetMidX(self.frame)-(size.width)

}



var createdHole = hole.createHoleAtPosition(position: CGPointMake(position ,CGRectGetMaxY(self.frame) + (size.height/2)),size: size )//CGSizeMake(CGRectGetMaxX(self.frame)/3 - 10, 70)

createdHole.physicsBody = SKPhysicsBody(rectangleOfSize: createdHole.frame.size)
createdHole.physicsBody?.categoryBitMask = holeCategory
createdHole.physicsBody?.collisionBitMask = 0
createdHole.physicsBody?.contactTestBitMask = ballCategory
createdHole.physicsBody?.affectedByGravity = false
createdHole.physicsBody?.dynamic = false


lastHolePosition = randomPosition

createdHole .runAction(actionMoveDown)

self.addChild(createdHole)



}

func addSphere(){
let mainCharacterController = circle()
let character: (SKNode) = mainCharacterController.createCircleAtPosition(position: CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinY(self.frame)+100))


character.physicsBody = SKPhysicsBody(circleOfRadius: character.frame.size.height/2)
character.physicsBody?.categoryBitMask = ballCategory
character.physicsBody?.collisionBitMask = 0
character.physicsBody?.contactTestBitMask = holeCategory
character.physicsBody?.affectedByGravity = false
character.physicsBody?.dynamic = false


self.addChild(character)


} func addHole(#size : CGSize) {

let actionMoveDown = SKAction.moveToY(CGRectGetMidY(self.frame)-500, duration: 4.7)
let hole = shapedHoles()
let UT = UTIL()
var position:CGFloat


let randomPosition = UT.randomNumberWith(Min: 1, Max: 3)

switch randomPosition{
case 1:
position = CGRectGetMidX(self.frame)
case 2:
position = CGRectGetMidX(self.frame)+size.width

default:
position = CGRectGetMidX(self.frame)-(size.width)

}



var createdHole = hole.createHoleAtPosition(position: CGPointMake(position ,CGRectGetMaxY(self.frame) + (size.height/2)),size: size )//CGSizeMake(CGRectGetMaxX(self.frame)/3 - 10, 70)

createdHole.physicsBody = SKPhysicsBody(rectangleOfSize: createdHole.frame.size)
createdHole.physicsBody?.categoryBitMask = holeCategory
createdHole.physicsBody?.collisionBitMask = 0
createdHole.physicsBody?.contactTestBitMask = ballCategory
createdHole.physicsBody?.affectedByGravity = false
createdHole.physicsBody?.dynamic = false


lastHolePosition = randomPosition

createdHole .runAction(actionMoveDown)

self.addChild(createdHole)



}

func addSphere(){
let mainCharacterController = circle()
let character: (SKNode) = mainCharacterController.createCircleAtPosition(position: CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinY(self.frame)+100))


character.physicsBody = SKPhysicsBody(circleOfRadius: character.frame.size.height/2)
character.physicsBody?.categoryBitMask = ballCategory
character.physicsBody?.collisionBitMask = 0
character.physicsBody?.contactTestBitMask = holeCategory
character.physicsBody?.affectedByGravity = false
character.physicsBody?.dynamic = false


self.addChild(character)


}

最后但同样重要的是 didBeginContactMethod

func didBeginContact(contact: SKPhysicsContact) {

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 & holeCategory) != 0 &&
(secondBody.categoryBitMask & ballCategory) != 0 {
println("HO")
}
}

谢谢你,希望你知道发生了什么,如果你需要任何额外的代码,请评论它

最佳答案

您必须确保您使用的 SKScene 子类也实现了 SKPhysicsContactDelegate 协议(protocol)。例如,它看起来像这样。

class MyScene : SKScene, SKPhysicsContactDelegate

然后您必须将您的物理世界联系委托(delegate)设置为您自己。

override init() {
self.physicsWorld.contactDelegate = self;
}

告诉我这是怎么回事,但它应该可以解决问题并成功地让您监听碰撞。

关于ios - 我如何正确检测接触和碰撞? swift iOS SKScene,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26499969/

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