加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 物理检测多次碰撞

发布时间:2020-12-14 05:33:23 所属栏目:百科 来源:网络整理
导读:当球与杯子碰撞时,得分应该增加1.然而,它目前增加1,3或有时4,这意味着多次检测到碰撞. 我想我必须错误地检查碰撞: let ballCategory : UInt32 = 0x1 0 let cupCategory : UInt32 = 0x1 1 在我的didMoveToView中: //Set physicsBody of scene to a physics
当球与杯子碰撞时,得分应该增加1.然而,它目前增加1,3或有时4,这意味着多次检测到碰撞.

我想我必须错误地检查碰撞:

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

在我的didMoveToView中:

//Set physicsBody of scene to a physics body that borders the screen

self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
physicsWorld.contactDelegate = self

//Ball
ballSprite.physicsBody = SKPhysicsBody(rectangleOfSize: ballSprite.size)
ballSprite.physicsBody!.categoryBitMask = ballCategory
ballSprite.physicsBody!.contactTestBitMask = cupCategory

//Cup
cupSprite.physicsBody = SKPhysicsBody(edgeLoopFromRect: cupSprite.size) 
cupSprite.physicsBody!.categoryBitMask = cupCategory
cupSprite.physicsBody!.contactTestBitMask = ballCategory

在我的didBeginContact中:

var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody

//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
}

//contact between ball and cup
if firstBody.categoryBitMask == ballCategory && secondBody.categoryBitMask == cupCategory {

    score++
    println("point scored")
    moveBall() //Move ball back to start position
}

这是因为它正在检测碰撞,但是它发生了多次,而它应该只发生一次.

您可以设置一个BOOL值来检查是否允许联系处理(在开头设置YES).更新得分后,您可以将该变量设置为NO,然后再次运行moveBall()并在moveBall()方法结束时将BOOL值设置为YES.我认为这样的事情应该有效.

其他方法是从父节点移除球节点或杯节点并将其设置为nil(然后重新创建它们).另外我不知道SpriteKit有多喜欢这种方式(在物理模拟完成之前删除节点).

第三种方法是让Ball类具有一些BOOL属性,指示是否应该将球移动到起始位置.根据该值,您应该更新得分变量/处理联系人和冲突.因此,在更新得分后,将BOOL值设置为YES,然后运行moveBall(),然后将BOOL值设置为NO等…类似于我描述的第一种方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读