【问题标题】:Removing sprite from scene when contact is made接触时从场景中移除精灵
【发布时间】:2015-08-05 17:02:20
【问题描述】:

所以在我的游戏中,我真的想让生活指数更加动态,而不仅仅是一个倒计时的标签。我在屏幕顶部添加了三个精灵,如下所示:

它们被这段代码插入到一个 for 循环中:

// Add Life to player

    var lifeIndexCount = 0
    var positionAdd:CGFloat = 10.0

    for lifeIndexCount in 0..<3 {

        let lifeIndex = SKSpriteNode(imageNamed: "bullet.png")

        lifeIndex.position = CGPoint(x: self.frame.size.width * -1.5, y: self.frame.size.height * 0.93)

        let lifeIndexMove = SKAction.moveTo(CGPoint(x: (size.width * 0.05) + positionAdd, y: size.height * 0.93), duration: NSTimeInterval(0.7))

        let lifeIndexRotation = SKAction.rotateByAngle(CGFloat(-2 * M_PI), duration: 0.3)

        lifeIndex.runAction(SKAction.sequence([lifeIndexMove, lifeIndexRotation]))

        addChild(lifeIndex)

        positionAdd = positionAdd + 25.0

我想在玩家飞船错过一球后立即移除每一个。就像生命精灵旋转和获取屏幕的小动画一样。我已经设置了我的联系人代表,但我不知道如何抓取它们并制作动画,以便在发生失误时它们可以关闭。

func didBeginContact(contact: SKPhysicsContact) {

    var contactBody1: SKPhysicsBody
    var contactBody2: SKPhysicsBody

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {

        contactBody1 = contact.bodyA
        contactBody2 = contact.bodyB

    } else {

        contactBody1 = contact.bodyB
        contactBody2 = contact.bodyA

    }

    if ((contactBody1.categoryBitMask == 1) && (contactBody2.categoryBitMask == 2)) {

        //Run when the bullet contacts the line

        contactBody2.node!.removeFromParent()

        // I WANT TO INSERT CODE HERE TO REMOVE A LIFE AT THE TOP


    } else if ((contactBody1.categoryBitMask == 2) && (contactBody2.categoryBitMask == 4)) {

        //Run when the bullet contacts the limbo

    }

}

谢谢!

【问题讨论】:

    标签: ios swift sprite-kit collision-detection


    【解决方案1】:

    有几种方法可以做到这一点,但它们几乎都可以归结为给你想要动画的节点一个名称,并在didBeginContact: 中匹配该名称。我喜欢enumerateChildNodesWithName 的可读性和易用性。如果您的用例有arrowSprite1、arrowSprite2等类似的东西,那么烘焙的模式匹配非常方便......

    清理节点可能正在执行的任何其他操作也是一个好主意,例如取消 SKAction 或发送到选择器的请求。

    https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKNode_Ref/index.html#//apple_ref/occ/instm/SKNode/enumerateChildNodesWithName:usingBlock:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-21
      • 1970-01-01
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多