为什么我的节点收集硬币时会减速?

时间:2023-01-23 00:26:11

My game basically is a jumping game when you tap the screen the heroNode jumps and collects coins coming from the right part of the screen. When it collects the coin the hero node slows down and it goes out of the view. Why does this happen? Heres the code I have.

我的游戏基本上是一个跳跃游戏,当您点击屏幕时,heroNode跳跃并收集来自屏幕右侧部分的硬币。当它收集硬币时,英雄节点减速并且它离开视图。为什么会这样?这是我的代码。

func coins() {
    let moveToLeft = SKAction.moveByX(-self.size.width, y: 0, duration: 2.0)
    let repeatMoveToLeft = SKAction.repeatActionForever(moveToLeft)
    let removeFromScene = SKAction.removeFromParent()
    let sequenceThisMoveAndRemove = SKAction.sequence([repeatMoveToLeft, removeFromScene])


    goldCoins.position = CGPointMake(self.size.width / 0.6, self.size.height / 2)
    goldCoins.zPosition = 15
    goldCoins.setScale(0.9)
    goldCoins.runAction(sequenceThisMoveAndRemove)
    addChild(goldCoins)

    goldCoins.physicsBody = SKPhysicsBody(circleOfRadius: 5)
    goldCoins.physicsBody?.affectedByGravity = false
    goldCoins.physicsBody?.allowsRotation = false
    goldCoins.physicsBody?.categoryBitMask = GoldCoinCategory
    goldCoins.physicsBody?.contactTestBitMask = HeroCategory
    goldCoins.physicsBody?.collisionBitMask = 0


    func addHero() {
    let anim = SKAction.animateWithTextures([heroTextureOne, heroTextureTwo], timePerFrame: 0.2)
    let run = SKAction.repeatActionForever(anim)

    theHero = SKSpriteNode(texture: heroTextureOne)
    theHero.runAction(run)

    theHero.physicsBody = SKPhysicsBody(circleOfRadius: 50)
    theHero.physicsBody?.affectedByGravity = true
    theHero.physicsBody?.allowsRotation = false
    theHero.physicsBody?.categoryBitMask = HeroCategory

    theHero.setScale(0.5)
    theHero.position = CGPointMake(self.size.width / 4.0, self.size.height / 2.0)
    theHero.zPosition = 15
    addChild(theHero)






}

       if firstBody.categoryBitMask == HeroCategory && sixthBody.categoryBitMask == GoldCoinCategory {
        sixthBody.node!.removeFromParent()

1 个解决方案

#1


One possibility is that you're making a lot of gold coins that never get removed from the scene graph, and that's bogging down your performance.

一种可能性是,你制作的大量金币永远不会从场景图中删除,这会让你的表现陷入困境。

Look at your first four lines of coins(). You create a forever-repeating action, and then create a sequence with the forever-repeating action and then the "remove from scene" action. A sequence performs the given actions in order, but a forever-repeating action will never end, so that "remove from scene" action will never be triggered.

看看你的前四行硬币()。您创建一个永远重复的操作,然后使用永久重复操作创建一个序列,然后创建“从场景中删除”操作。序列按顺序执行给定的操作,但永远重复的操作永远不会结束,因此永远不会触发“从场景中删除”操作。

So when you addChild( goldCoins ), those coins are never going to go away. And the only other way they can apparently be removed is with a collision. So if you play the game, and if a lot of goldCoins get added, then you're going to have an unbounded number of coins in play. After a while, having enough of those coins, all running actions, could cause your game to slow down.

因此,当你添加孩子(金币)时,这些硬币永远不会消失。而他们显然可以被移除的唯一其他方式是碰撞。因此,如果您玩游戏,并且如果添加了大量的金币,那么您将获得无限数量的硬币。过了一会儿,拥有足够的硬币,所有正在运行的动作都可能导致游戏速度变慢。

Another possibility is that all you're removing is the sprite node and not the physics body from the simulation. This is suggested by that last line you included. If you remove the node, the coin will disappear, but the physics body will still be in play, still affecting other physics bodies. If you want to fully remove the coin - and its effect on the physics simulation - you'll need to remove its physics body, too.

另一种可能性是你要移除的只是精灵节点,而不是模拟中的物理体。这是您包含的最后一行的建议。如果移除节点,硬币将消失,但物理体仍将在游戏中,仍会影响其他物理实体。如果你想完全移除硬币 - 以及它对物理模拟的影响 - 你也需要移除它的物理体。

#1


One possibility is that you're making a lot of gold coins that never get removed from the scene graph, and that's bogging down your performance.

一种可能性是,你制作的大量金币永远不会从场景图中删除,这会让你的表现陷入困境。

Look at your first four lines of coins(). You create a forever-repeating action, and then create a sequence with the forever-repeating action and then the "remove from scene" action. A sequence performs the given actions in order, but a forever-repeating action will never end, so that "remove from scene" action will never be triggered.

看看你的前四行硬币()。您创建一个永远重复的操作,然后使用永久重复操作创建一个序列,然后创建“从场景中删除”操作。序列按顺序执行给定的操作,但永远重复的操作永远不会结束,因此永远不会触发“从场景中删除”操作。

So when you addChild( goldCoins ), those coins are never going to go away. And the only other way they can apparently be removed is with a collision. So if you play the game, and if a lot of goldCoins get added, then you're going to have an unbounded number of coins in play. After a while, having enough of those coins, all running actions, could cause your game to slow down.

因此,当你添加孩子(金币)时,这些硬币永远不会消失。而他们显然可以被移除的唯一其他方式是碰撞。因此,如果您玩游戏,并且如果添加了大量的金币,那么您将获得无限数量的硬币。过了一会儿,拥有足够的硬币,所有正在运行的动作都可能导致游戏速度变慢。

Another possibility is that all you're removing is the sprite node and not the physics body from the simulation. This is suggested by that last line you included. If you remove the node, the coin will disappear, but the physics body will still be in play, still affecting other physics bodies. If you want to fully remove the coin - and its effect on the physics simulation - you'll need to remove its physics body, too.

另一种可能性是你要移除的只是精灵节点,而不是模拟中的物理体。这是您包含的最后一行的建议。如果移除节点,硬币将消失,但物理体仍将在游戏中,仍会影响其他物理实体。如果你想完全移除硬币 - 以及它对物理模拟的影响 - 你也需要移除它的物理体。