精灵套件:SKNodes在屏幕上添加了像Fruit Ninja这样的动画

时间:2016-09-27 09:00:40

标签: ios sprite-kit

现在我正在开发类似于Fruit Ninja的游戏,我用精灵套件启动它并开发了刀片效果(刷屏幕)。现在第二部分是添加节点(如游戏中的水果)。我尝试了很多,但没有获得添加水果的动画和角度。以下是我的代码。

func createApple() {
     fruitNode = SKSpriteNode(imageNamed: "apple")
     fruitNode.anchorPoint = CGPointZero
     fruitNode.position = CGPoint(x:568, y:50);
     fruitNode.name = "bulletNode"
     fruitNode.zPosition = 1.0;
     fruitNode.size.width = fruitNode.size.width / 6
     fruitNode.size.height = fruitNode.size.height / 6

     fruitNode.physicsBody = SKPhysicsBody(circleOfRadius: 60)
     fruitNode.physicsBody?.categoryBitMask =  PhysicsCategory.fruitNode.rawValue
      fruitNode.physicsBody?.contactTestBitMask = PhysicsCategory.Spiral.rawValue
       fruitNode.physicsBody?.affectedByGravity = false

       let wait = SKAction .moveToX(self.frame.origin.x -  fruitNode.size.width, duration: 4)
      fruitNode.runAction(wait, completion: { () -> Void in
        self.fruitNode.removeFromParent()
})

addChild(fruitNode)

}

2 个答案:

答案 0 :(得分:1)

我实施了它并且工作精湛......

func createPineApple() {
    pineAppleNode = SKSpriteNode(imageNamed: "pine")
    pineAppleNode.anchorPoint = CGPointZero
    pineAppleNode.name = "pineNode"
    pineAppleNode.zPosition = 1.0;
    pineAppleNode.size.width = pineAppleNode.size.width / 6
    pineAppleNode.size.height = pineAppleNode.size.height / 6
    pineAppleNode.position = CGPoint(x:CGFloat(randomsX()), y:-pineAppleNode.size.height);
    pineAppleNode.physicsBody = SKPhysicsBody(rectangleOfSize: pineAppleNode.size)
    pineAppleNode.physicsBody?.categoryBitMask = PhysicsCategory.PineApple.rawValue
    pineAppleNode.physicsBody?.contactTestBitMask = PhysicsCategory.Blade.rawValue
    pineAppleNode.physicsBody?.collisionBitMask = PhysicsCategory.None.rawValue
    pineAppleNode.physicsBody?.affectedByGravity = false

    let xAz = Int(randomsX())
    let yAz = Int(self.frame.size.height - pineAppleNode.size.height)

    let xAz1 = Int(randomsX())
    let yAz1 = Int(-pineAppleNode.size.height)

    let p = CGPoint(x:xAz,y:yAz)
    let p2 = CGPoint(x:xAz1,y:yAz1)

    let randPos = SKAction.moveTo(p, duration: 1)
    let randPos2 = SKAction.moveTo(p2, duration: 1)
    let sequence = SKAction.sequence([randPos,randPos2])
    pineAppleNode.runAction(SKAction.repeatAction(sequence, count: 1), withKey:  "moving")

    let waist = SKAction.waitForDuration(2.5)
    let run = SKAction.runBlock {
        //self.fruitNode.removeFromParent()
    }

    self.runAction(SKAction.sequence([waist, run]))
    addChild(pineAppleNode)
}

答案 1 :(得分:1)

你将使用动作作为位置值的移动与通过使用动作对物理对象施加力/冲动来创建动作混淆。

使用物理对象是一个好主意。你想要的是通过向物体施加力/脉冲,从屏幕下方的位置以不同的速率和角度投掷物体。

这有许多棘手的部分,其中最重要的是使用的力/脉冲尺寸范围以及与此相关的射角范围,因此所有水果都是可切片的。您还需要重新创建"发射器"水果。