1

Right, so I'm making this game in swift and there are these 4 coins, each have an individual value of 10, 20, 50, 100. Is there any idea how I can assign these values to these nodes and then recreate them during runtime? With this I mean that the exact same nodes with the same values are added onto the screen, perhaps with an animation, at different locations on the screen. Also, I want the 4 coins to be re-copied on screen at different intervals, for example: coin 10 is worth less so it would be recreating less often (ex: every 0.2 sec) than coin 50 (ex: every 1 sec). I have no idea how to even start this, so please help? I have included my code for GameScene.swift below to be as specific as possible.

//
//  GameScene.swift
//  Coin Grabber
//
//  Created by Viren Sareen on 13/07/2015.
//  Copyright (c) 2015 Viren Sareen. All rights reserved.
//

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

var coin10 = SKSpriteNode(imageNamed: "10S.png")
var coin100 = SKSpriteNode(imageNamed: "100S.png")
var coin50 = SKSpriteNode(imageNamed: "50S.png")
var coin20 = SKSpriteNode(imageNamed: "20S.png")
var wall1 = SKSpriteNode(imageNamed: "Wall1.png")
var wall2 = SKSpriteNode(imageNamed: "Wall2.png")
var bar = SKSpriteNode(imageNamed: "Bar.png")
var touchedcoin: SKSpriteNode?

var scorelabel = SKLabelNode()
var score = 0

var touchPoint: CGPoint = CGPoint()
var touching: Bool = false

enum ColliderType:UInt32 {
    case coin = 1
    case wall = 2
    case bars = 3

}

override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    //Adding coin10
    coin10.position = CGPointMake(self.size.width / 2, self.size.height / 5)
    coin10.physicsBody = SKPhysicsBody(circleOfRadius: coin10.size.width/1.5)
    coin10.physicsBody!.affectedByGravity = false
    coin10.physicsBody!.categoryBitMask = ColliderType.coin.rawValue
    coin10.physicsBody!.contactTestBitMask = ColliderType.wall.rawValue
    coin10.physicsBody!.collisionBitMask = ColliderType.wall.rawValue
    coin10.physicsBody!.dynamic = true
    self.addChild(coin10)

    //Adding coin100
    coin100.position = CGPointMake(self.size.width / 1.7, self.size.height / 5.1)
    coin100.physicsBody = SKPhysicsBody(circleOfRadius: coin100.size.width/1.3)
    coin100.physicsBody!.affectedByGravity = false
    coin100.physicsBody!.categoryBitMask = ColliderType.coin.rawValue
    coin100.physicsBody!.contactTestBitMask = ColliderType.wall.rawValue
    coin100.physicsBody!.collisionBitMask = ColliderType.wall.rawValue
    coin100.physicsBody!.dynamic = true
    self.addChild(coin100)

    //Adding coin50
    coin50.position = CGPointMake(self.size.width / 2.2, self.size.height / 4.9)
    coin50.physicsBody = SKPhysicsBody(circleOfRadius: coin50.size.width/1.5)
    coin50.physicsBody!.affectedByGravity = false
    coin50.physicsBody!.categoryBitMask = ColliderType.coin.rawValue
    coin50.physicsBody!.contactTestBitMask = ColliderType.wall.rawValue
    coin50.physicsBody!.collisionBitMask = ColliderType.wall.rawValue
    coin50.physicsBody!.dynamic = true
    self.addChild(coin50)

    //Adding coin20
    coin20.position = CGPointMake(self.size.width / 2.4, self.size.height / 5)
    coin20.physicsBody = SKPhysicsBody(circleOfRadius: coin20.size.width/1.5)
    coin20.physicsBody!.affectedByGravity = false
    coin20.physicsBody!.categoryBitMask = ColliderType.coin.rawValue
    coin20.physicsBody!.contactTestBitMask = ColliderType.wall.rawValue
    coin20.physicsBody!.collisionBitMask = ColliderType.wall.rawValue
    coin50.physicsBody!.dynamic = true
    self.addChild(coin20)

    //Adding wall1
    wall1.position = CGPointMake(self.size.width / 1.32, self.size.height / 1.04)
    wall1.physicsBody = SKPhysicsBody(rectangleOfSize: wall1.size)
    wall1.physicsBody!.affectedByGravity = false
    wall1.physicsBody!.categoryBitMask = ColliderType.wall.rawValue
    wall1.physicsBody!.contactTestBitMask = ColliderType.coin.rawValue
    wall1.physicsBody!.collisionBitMask = ColliderType.coin.rawValue
    wall1.physicsBody!.dynamic = false
    self.addChild(wall1)

    //Adding wall2
    wall2.position = CGPointMake(self.size.width / 4.8, self.size.height / 1.04)
    wall2.physicsBody = SKPhysicsBody(rectangleOfSize: wall2.size)
    wall2.physicsBody!.affectedByGravity = false
    wall2.physicsBody!.categoryBitMask = ColliderType.wall.rawValue
    wall2.physicsBody!.contactTestBitMask = ColliderType.coin.rawValue
    wall2.physicsBody!.collisionBitMask = ColliderType.coin.rawValue
    wall2.physicsBody!.dynamic = false
    self.addChild(wall2)

    //Adding bar
    bar.position = CGPointMake(self.size.width / 2, self.size.height)
    bar.physicsBody = SKPhysicsBody(circleOfRadius: bar.size.height/2)
    bar.physicsBody!.affectedByGravity = false
    bar.physicsBody!.categoryBitMask = ColliderType.bars.rawValue
    bar.physicsBody!.contactTestBitMask = ColliderType.coin.rawValue
    bar.physicsBody!.collisionBitMask = ColliderType.coin.rawValue
    bar.physicsBody!.dynamic = false
    self.addChild(bar)

    //Adding physics world properties
    self.physicsWorld.contactDelegate = self
    var scenebody = SKPhysicsBody(edgeLoopFromRect: self.frame)
    scenebody.friction = 0
    self.physicsBody = scenebody
    self.physicsWorld.gravity = CGVectorMake(0, 0)
    physicsWorld.contactDelegate = self

    //Scoreboard
    scorelabel = SKLabelNode(text: "0")
    scorelabel.position.y = (self.size.height/2)
    scorelabel.position.x = (self.size.height/2.3)
    addChild(scorelabel)

}

func didBeginContact(contact: SKPhysicsContact) {

}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch in (touches as! Set<UITouch>) {
        let location10 = touch.locationInNode(self)
        let location100 = touch.locationInNode(self)
        let location20 = touch.locationInNode(self)
        let location50 = touch.locationInNode(self)

        if coin10.containsPoint(location10){
            touchPoint = location10
            touching = true
            touchedcoin = coin10
        }
        else if coin100.containsPoint(location100){
            touchPoint = location100
            touching = true
            touchedcoin = coin100
        }
        else if coin20.containsPoint(location20){
            touchPoint = location20
            touching = true
            touchedcoin = coin20
        }
        else if coin50.containsPoint(location50){
            touchPoint = location50
            touching = true
            touchedcoin = coin50
        }
    }
}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {

    for touch in (touches as! Set<UITouch>) {
        let location10 = touch.locationInNode(self)
        let location100 = touch.locationInNode(self)
        let location50 = touch.locationInNode(self)
        let location20 = touch.locationInNode(self)

        if coin10.containsPoint(location10){
            touchPoint = location10
        }
        else if coin100.containsPoint(location100){
            touchPoint = location100
        }
        else if coin50.containsPoint(location50){
            touchPoint = location50
        }
        else if coin20.containsPoint(location20){
            touchPoint = location20
        }
    }
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    touching = false
}

override func update(currentTime: CFTimeInterval) {
    if touching {

        let dt: CGFloat = 1.1/101.0
        let distance = CGVector(dx: touchPoint.x-touchedcoin!.position.x, dy: touchPoint.y-touchedcoin!.position.y)
        let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
        touchedcoin!.physicsBody!.velocity = velocity
        }
    }
 }
  • You can start with this : Creating random CGPoint : http://stackoverflow.com/questions/24438495/create-random-cgpoint-with-swift, Spawning nodes after random time interval http://stackoverflow.com/a/30764247/3402095 – Whirlwind Aug 02 '15 at 12:56
  • You could also make a custom coin class, and take advantage of OOP, making the coins super easy to replicate and assign values to. – Kendel Aug 02 '15 at 14:29
  • @Whirlwind, creating the random node question isn't very helpful as I don't see how it can be done with my code and it isn't well explained either + since I'm a beginner to swift it's completely un-understandable for me, and I can't advance to spawning the nodes after interval as I can't recreate the nodes, besides because these are 2 different articles I have no idea how to merge the code to work for me. – Viren Sareen Aug 02 '15 at 14:47
  • @Kendel, how would I do that exactly? Would I need to restructure my entire code for that? – Viren Sareen Aug 02 '15 at 14:48
  • No. You can either make it a subclass of `SKSpriteNode` or you can just make a coin object which holds an `SKSpriteNode` property. – Kendel Aug 02 '15 at 15:59
  • @VirenSareen I've posted those links because your question is too broad IMO and even if any of its part are not problematic to implement it can be time consuming for someone to give you an fully detailed answer. When asking a question try to concentrate on a single problem because like that it's more likely you'll get an answer in some reasonable period of time ... I will write you an example to show you how to implement something from those links, but I can't promise you that I will solve every part you are trying to accomplish (because of the reasons mentioned above :)) – Whirlwind Aug 02 '15 at 16:01
  • @Kendel, can you show an example how that's done? Sorry I'm just a beginner to this language, literally wrote all that code following YouTube tutorials – Viren Sareen Aug 02 '15 at 16:10
  • @Whirlwind, thanks, I'll wait for that code! But I'm not sure how to make this question more specific... I tried to make it as specific as possible – Viren Sareen Aug 02 '15 at 16:11
  • I posted an example of a coin class you could use to make reproducing the same node much easier. – Kendel Aug 02 '15 at 16:27

2 Answers2

3

I would go personally with with other option Kendal proposed, which is subclassing SKSpriteNode.

Coin.swift

import Foundation
import SpriteKit

enum ColliderType:UInt32 {
    case coin = 1
    case wall = 2
    case bars = 3

}


class Coin: SKSpriteNode {

    var value: Int

    init(coinValue: Int) {

         self.value = coinValue

         let texture = SKTexture(imageNamed: String(coinValue) + "S")

        super.init(texture: texture, color: nil, size: texture.size())


        self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width/2.0)
        self.physicsBody!.affectedByGravity = true
        self.physicsBody!.categoryBitMask = ColliderType.coin.rawValue
        self.physicsBody!.contactTestBitMask = ColliderType.wall.rawValue
        self.physicsBody!.collisionBitMask = ColliderType.wall.rawValue
        self.physicsBody!.dynamic = true
        self.name = "coin"

    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

GameScene.swift:

    import SpriteKit


class GameScene: SKScene,SKPhysicsContactDelegate
{

    let debugLabel = SKLabelNode(fontNamed: "Geneva")

    var coin10counter = 0
    var coin20counter = 0
    var coin50counter = 0
    var coin100counter = 0

    let gameDuration = 15

    var timeLeft = 15

    let startButton = SKSpriteNode(color: SKColor.greenColor(), size: CGSize(width: 80, height:30))
    let stopButton = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 80, height:30))

    let gameTimerLabel = SKLabelNode(fontNamed: "Geneva")



    override func didMoveToView(view: SKView)
    {

        //Setting up physics - default for dy is -9.81 but because of easier debugging I set it to -0.5
        self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)

        self.physicsWorld.gravity = CGVector(dx: 0.0, dy:-0.5)


        //Debug labels

        debugLabel.fontColor = SKColor.whiteColor()
        debugLabel.fontSize =  15
        debugLabel.text = "coin10: \(coin10counter) coin20: \(coin20counter) coin50: \(coin50counter) coin100: \(coin100counter)"
        debugLabel.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMaxY(self.frame)-200)
        self.addChild(debugLabel)


        gameTimerLabel.fontColor = SKColor.whiteColor()
        gameTimerLabel.fontSize =  20
        gameTimerLabel.text = "Time left : \(gameDuration)"
        gameTimerLabel.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMaxY(self.frame)-40)

        self.addChild(gameTimerLabel)

        self.backgroundColor = SKColor.blackColor()


        //Buttons

        startButton.position = CGPoint(x: CGRectGetMidX(self.frame)-80, y: CGRectGetMaxY(self.frame)-100)
        startButton.name = "start"
        stopButton.position = CGPoint(x: CGRectGetMidX(self.frame)+80, y: CGRectGetMaxY(self.frame)-100)
        stopButton.name = "stop"

        self.addChild(startButton)
        self.addChild(stopButton)


    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {


        let touch: AnyObject? = touches.anyObject()

        let location = touch?.locationInNode(self)

        println(location)

        let touchedNode = self.nodeAtPoint(location!)

        println(touchedNode.name)

        if(touchedNode.name == "start"){

            self.generateCoins()

        }else if(touchedNode.name == "stop"){

            self.stopGeneratingCoins()
        }

    }


    func getRandomCoin() ->Coin{


        let randomNumber = Double(arc4random() % 1000) / 10.0;

        switch(randomNumber) {

       //You can modify this to play with chances

        case 60..<90:

             coin20counter++
            return Coin(coinValue: 20)
        case 90..<97:
             coin50counter++
            return Coin(coinValue: 50)

        case 97..<100: // smallest chance

             coin100counter++
            return Coin(coinValue: 100)
        default:
            //biggest chance
             coin10counter++
            return Coin(coinValue: 10)
        }


    }

    func stopGeneratingCoins(){


        removeActionForKey("spawning")

        removeActionForKey("countdown")

        coin10counter = 0

        coin20counter  = 0

        coin50counter = 0

        coin100counter = 0

        self.enumerateChildNodesWithName("coin", usingBlock: {
            (node: SKNode!, stop: UnsafeMutablePointer <ObjCBool>) -> Void in

            node.removeFromParent()

        })

        timeLeft = gameDuration

        debugLabel.text = "coin10: \(coin10counter) coin20: \(coin20counter) coin50: \(coin50counter) coin100: \(coin100counter)"

        gameTimerLabel.text = "Time left : \(gameDuration)"
    }


    func countdown(){


        let updateTimeleftLabel = SKAction.runBlock({

            self.gameTimerLabel.text = "Time left : \(self.timeLeft--)"
        })

        let waitAndUpdate = SKAction.sequence([updateTimeleftLabel ,SKAction.waitForDuration(1)] )



        let countdown = SKAction.repeatAction(waitAndUpdate, count: self.gameDuration)

        let sequence = SKAction.sequence([countdown, SKAction.runBlock({

            self.stopGeneratingCoins()

        })])

        self.runAction(sequence, withKey:"countdown")

    }


    func generateCoins(){

        if(self.actionForKey("spawning") != nil){return}

        countdown()

        let timer = SKAction.waitForDuration(0.5, withRange: 0.3)

        let spawnNode = SKAction.runBlock {


            var coin = self.getRandomCoin()


            let spawnLocation = CGPoint(x:Int(arc4random() % UInt32(self.frame.size.width - coin.size.width/2) ),
                                        y:Int(arc4random() %  UInt32(self.frame.size.height - coin.size.width/2)))

            coin.position = spawnLocation





            self.debugLabel.text =
            "coin10 : \(self.coin10counter) coin20: \(self.coin20counter) coin50 : \(self.coin50counter) coin100 : \(self.coin100counter)"

            self.addChild(coin)

            println(spawnLocation)

        }

        let sequence = SKAction.sequence([timer, spawnNode])
        self.runAction(SKAction.repeatActionForever(sequence) , withKey: "spawning")


    }



}

So, this code is mostly based on Kendal's code, but there are few differences:

  • I set coin's position right before its added to the scene instead of passing the scene as a parameter in Coin's init method.
  • I run action withKey parameter which allows me to stop certain action by given key (eg. stop spawning coins)

Also I have implemented spawning of coins at random position as well as randomizing chance of spawning for certain coins - coins with lower values will be spawned more often than coins with higher value.

EDIT:

I've added a timer and some debugging labels to show how much time is left until game ends, and to allow you to keep track of an amount of spawned coins. Here is the result:

Generating coins example

As you can see, the "game" starts when user click green button and stops when red button is clicked. If not interrupted by the user game ends after period of time determined by gameDuration variable.

Also you can see how randomizing amount of coins determined by their value works... After ten seconds there is about:

Nine coin10 nodes, seven coin20 nodes, three coin50 and one coin100 nodes which I guess are the numbers you wanted. You can tweak getRandomCoin: method to get different results.

And there is a new method called stopGeneratingCoins which resets everything to default values. In that method, all running actions are removed, counters and similar variables are set to default values , and all coins are removed from it's parent using the - enumerateChildNodesWithName:usingBlock: When using this method, it is important to know that coin.name should be defined. So, I set coin's name inside Coin's init method which allows me to search for it (and remove it from scene) by a name. And that's it :-)

Hope this helps.

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • I updated my code to include Coin as a subclass of SKSpriteNode – Kendel Aug 02 '15 at 19:45
  • @VirenSareen I've forgot to upload Coin class :)... Sorry about that. Check out my answer again. – Whirlwind Aug 02 '15 at 20:11
  • I'm using Kendal's code, this one is giving me way to many errors and I'm building on that now, so thanks for writing this out for me but I'm trying to get Kendal's code right – Viren Sareen Aug 02 '15 at 20:20
  • @VirenSareen This code works without an error on my side... I am not sure what kind of errors you are getting ... – Whirlwind Aug 02 '15 at 20:32
  • Aah sorry about the errors, just fixed it, it was just me being silly, I do want this to stop using another timer that runs from 60 seconds as soon as the user touches the screen + the nodes don't hold values using this code do they? Also, the new nodes that are recreated, they can't be moved using touch like the original ones – Viren Sareen Aug 02 '15 at 20:56
  • @VirenSareen Okay, always is something silly actually :) I will update my answer with a timer to show you how to do it... I am not sure what you mean with last part about "holding the values" ... – Whirlwind Aug 02 '15 at 20:59
  • Haha thanks, I'm starting university in sep to learn Comp Sci, I really hope my profs are like you, so helpful! & I mean with that for example there are 4 coins: coin10, 20, 50 and 100, each holding a respective value that will be the score (10, 20, 50 and 100), the code above don't assign these 4 values to their respective coin do they? – Viren Sareen Aug 02 '15 at 21:03
  • 1
    @VirenSareen Each coin has value property which is appropriately initialized in coin's Init method. You can access that value simply by coin.value. – Whirlwind Aug 03 '15 at 00:11
  • Thanks this is a great piece of code! Sorry about the late reply I was really busy + trying to get my head around this code, it's very complex, i've implemented it, however, I am not able to move the randomly created nodes using touch, they're static. The idea is when they collide with an object, they disappear and the value on the coin is added onto the score, this is why the randomly created objects need to have the value of the coin, to be added to the score. – Viren Sareen Aug 05 '15 at 12:56
  • @VirenSareen About moving / not moving part... Well, I guess that's up to you. You can make their physics bodies static (or not affected by gravity), set their position, and when contact occurs, just remove them and update current score. Note that contact is detected only if at least one body is dynamic. – Whirlwind Aug 05 '15 at 13:11
  • Ah no, I want them to be EXACTLY like the original coins, dynamic, moveable by touch, and they should disappear when they collide with a specific node, when that happens, depending on the value of the coin, it should be added onto the score – Viren Sareen Aug 05 '15 at 13:15
  • @VirenSareen So, what is the problem actually ? What do you mean by "they are static". In my example, coins are dynamic and can be moved by applying impulses or by directly changing velocity vector. They can be moved even with SKActions, but you can use SKActions to move physic bodies only if you don't need them in physics simulation (eg. collisions) and you are interested just in contact detection. – Whirlwind Aug 05 '15 at 13:19
  • So they are literally just appearing as images and nothing can be done with them, what I want to be recreated is an exact replica of the original, same value, and the node is move-able using touch only with the same physics values as the original (not affected by gravity, bouncing animations, etc) + how would I be going about it if I want to update the score when a coin collides with the node, with the corresponding value? – Viren Sareen Aug 05 '15 at 13:23
  • I don't know how your current code looks, but as you can see from an example I've posted, coins are moving because they are dynamic and affected by gravity... You are updating the score simply by score+=coin.value. If you have further questions about updating the score or any other topic, I suggest you to ask a new question, because comments are not suitable for solving that. – Whirlwind Aug 05 '15 at 13:51
1

Here is an example of a coin class you could use:

import Foundation
import SpriteKit

enum ColliderType:UInt32 {
    case coin = 1
    case wall = 2
    case bars = 3

}

class Coin {

    var value: Int
    var coinNode: SKSpriteNode

    init(coinValue: Int, scene: SKScene) {
        value = coinValue
        let node = SKSpriteNode(imageNamed: String(coinValue) + "S")
        node.position = CGPointMake(node.size.width / 2, node.size.height / 5)
        node.physicsBody = SKPhysicsBody(circleOfRadius: node.size.width/1.5)
        node.physicsBody!.affectedByGravity = false
        node.physicsBody!.categoryBitMask = ColliderType.coin.rawValue
        node.physicsBody!.contactTestBitMask = ColliderType.wall.rawValue
        node.physicsBody!.collisionBitMask = ColliderType.wall.rawValue
        node.physicsBody!.dynamic = true
        scene.addChild(node)
        self.coinNode = node
    }
}

Here is another option for a subclass of coin:

import Foundation
import SpriteKit

enum ColliderType:UInt32 {
    case coin = 1
    case wall = 2
    case bars = 3

}

class Coin: SKSpriteNode {

    var value: Int

    init(coinValue: Int) {
        value = coinValue
        let texture = SKTexture(imageNamed: String(coinValue) + "S")
        super.init(texture: texture, color: UIColor.clearColor(), size: texture.size())
        self.position = CGPointMake(self.size.width / 2, self.size.height / 5)
        self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width/1.5)
        self.physicsBody!.affectedByGravity = false
        self.physicsBody!.categoryBitMask = ColliderType.coin.rawValue
        self.physicsBody!.contactTestBitMask = ColliderType.wall.rawValue
        self.physicsBody!.collisionBitMask = ColliderType.wall.rawValue
        self.physicsBody!.dynamic = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

Then, write a timer to randomly make more instances of coins, and pick a value for the coin. As far as the timer is concerned do something like this when the scene is initialized, or when it moves to view.:

let timer = SKAction.waitForDuration(10, withRange: 2) 
let spawnNode = SKAction.runBlock {
    var coin = Coin(10, scene: self)
   //set coin position: coin.coinNode.position = whatever
}

let sequence = SKAction.sequence([timer, spawnNode])
self.runAction(SKAction.repeatActionForever(sequence))
Kendel
  • 1,698
  • 2
  • 17
  • 33
  • Ah thanks I'll Def try out this code! How do I write the timer to assign values to the coins and randomly make more instances of coins? – Viren Sareen Aug 02 '15 at 16:36
  • Ah thanks, is it defined in the timer code which coin will be recreated? And won't the position be specified by me? So it won't be randomly created on the screen? – Viren Sareen Aug 02 '15 at 16:41
  • You do choose a position, but you choose a random position. Also, you can add multiple timers for different coin types. – Kendel Aug 02 '15 at 16:47
  • Ooh okay! Thanks! What's the command in swift for the random position? – Viren Sareen Aug 02 '15 at 16:48
  • There is no command. You just have to pick a random x value, and a random y value within your scene's limits. Check out http://stackoverflow.com/questions/24466330/random-value-in-swift – Kendel Aug 02 '15 at 16:49
  • Okay I'll check it out, thank you so much for you help! – Viren Sareen Aug 02 '15 at 16:51
  • I've tried your code, the timer one is giving me compilation errors, 2 to be exact, the following lines of the code seem to be giving the problem: var coin = Coin(10, self) AND let sequence = SKAction.sequence([wait, spawnNode]) -- The 1st error states 'Use of unresolved identifier 'Coin'', 2nd error says 'Could not find an overload for the 'wait' that accepts the supplied arguments. Any ideas? – Viren Sareen Aug 02 '15 at 18:57
  • Still getting errors, where should I put the timer code? I'm putting it in the Coin class, is that correct? I took a screenshot cuz there are 3 errors this time and its faster to show u the situation: http://www.tiikoni.com/tis/view/?id=0689918 – Viren Sareen Aug 02 '15 at 19:25
  • They're visible in the screenshot – Viren Sareen Aug 02 '15 at 19:49
  • Change the scene parameter in the coin class to `GameScene` instead of an `SKScene` – Kendel Aug 02 '15 at 19:52
  • 1
    @Kendel Yeah, my answer is based on your suggestion...I just added the part about spawning nodes at random location and for randomizing chance of spawning certain coins. – Whirlwind Aug 02 '15 at 20:15
  • Also, this piece of code needs to stop after a timer will hit 0, so the game will have a 60 second time limit and it should end when that 60s timer hits 0. I have moved the smaller piece of code to 'touchesBegan' as I want it to run as soon as the user touches the screen – Viren Sareen Aug 02 '15 at 20:23
  • Thanks! I managed to fixed the errors, but I need to assign the 4 coins their respective values, that's not in the code is it? – Viren Sareen Aug 02 '15 at 20:57
  • Just assign the coin to a value... But if you want more than 1 coin at a time you should make a collection to hold all of the coins. – Kendel Aug 02 '15 at 21:00