Sai Bharath Yadav Sai Bharath Yadav - 1 month ago 23
iOS Question

Sprite Kit: SKNodes adding to the screen with animation like Fruit Ninja

Right now I'm working on the game which is similar to Fruit Ninja, I started it with the sprite kit and developed the blade effect (swiping the screen). Now the second part is adding the nodes (like fruits in the game). I tried a lot but didn't get the animation and angle for adding fruit. Below is my code.

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)


}

Answer

I implemented it and working superb...

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)
}
Comments