为什么会有多个碰撞调用Sprite Kit Swift [英] Why is there multiple collision calls Sprite Kit Swift

查看:77
本文介绍了为什么会有多个碰撞调用Sprite Kit Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建带有碰撞的iOS快速游戏。英雄被来自屏幕右侧的小星星轰炸。每次有星星击中英雄时,都会重复进行比分并移除星星。但是,得分会增加一个以上的分数。

I am building an iOS swift game with collision. The hero is being bombarded by small stars coming from the right side of the screen. Every time a star hits the hero, the score is iterated and the star is removed. But, more than one point is added to the score.

节点设置:

var star = SKSpriteNode(imageNamed: "star")
star.size = CGSizeMake(30, 30)
star.zPosition = 10
var starPhysicsRect = CGRectMake(star.position.x - star.frame.size.width / 2, star.position.y - star.frame.size.width / 2, star.frame.size.width, star.frame.size.height)
star.physicsBody = SKPhysicsBody(edgeLoopFromRect: starPhysicsRect)
star.physicsBody?.restitution = 0
star.name = "star"
star.physicsBody?.mass = 0
return star

didBeginContact函数:

func didBeginContact(contact: SKPhysicsContact) {
    //collision variables
    var firstBodyNode = contact.bodyA.node as! SKSpriteNode
    var secondBodyNode = contact.bodyB.node as! SKSpriteNode
    var firstNodeName = firstBodyNode.name
    var secondNodeName = secondBodyNode.name

    //other variables
    var scoreLabel: SKLabelNode = constantsInstance.scoreLabel(position: CGPointMake(self.frame.size.width * 0.86, self.frame.size.height * 0.928))


    //check if hero hit star
    if firstNodeName == "hero" && secondNodeName == "star" {
        println("star hit")
        secondBodyNode.removeFromParent()

        //iterate score
        childNodeWithName("scoreLabel")?.removeFromParent()
        scoreLabel.text = "\(counterForScore)"
        counterForScore++
        self.addChild(scoreLabel)
    }
}

我也多次在控制台上打印 star hit。如果在第一次调用该函数时删除星号,为什么 didBeginContact 函数会被多次调用?

I get "star hit" printed to the console several times as well. Why would the didBeginContact function be called more than once if I remove the star on the first call of the function?

我进行了较小规模的模拟,发现如果没有删除命令,星星会深入英雄(由于SKAction)。

I have run simulations on a smaller scale and found that the star goes fairly deep into the hero (because of the SKAction), if there is no remove command. Could it be that the didBeginContact function is called many times while the star is going into the hero and before it is removed?

非常感谢所有帮助。

推荐答案

可能是由于PhysicalBody造成的:

Could it possibly be due to the physicsBody:

var star = SKSpriteNode(imageNamed: "star")
star.size = CGSizeMake(30, 30)
star.zPosition = 10
var starPhysicsRect = CGRectMake(star.position.x - star.frame.size.width / 2, star.position.y - star.frame.size.width / 2, star.frame.size.width, star.frame.size.height)
star.physicsBody = SKPhysicsBody(edgeLoopFromRect: starPhysicsRect)

看看创建starPhysicsRect的逻辑:

Have a look at the logic creating the starPhysicsRect:

(star.position.x - star.frame.size.width / 2, star.position.y - star.frame.size.width / 2, star.frame.size.width, star.frame.size.height)

从我所看到的翻译为机翼值:
(-15,-15、30、30)那些负位置坐标似乎很奇怪。您确定不只是想要: SKPhysicsBody(rectangleOfSize:star.size)吗?

From what I can see this translates to the following values: (-15, -15, 30, 30) those negative position-coordinates seem odd. Are you sure you don't just want: SKPhysicsBody(rectangleOfSize: star.size) ?

这篇关于为什么会有多个碰撞调用Sprite Kit Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆