Search code examples
iosswiftsprite-kitconstantsskemitternode

Assigning different emitters to different nodes that are randomized


I have basic emitter nodes working in my first app. I have created a Constants File that references all nodes throughout my game, and converts them to strings. Great for a mini data base. Its a basic Whack a Mole Game, except I have 12 different characters.

GameScene Code

This is called in my didMoveToView method:

 func star(pos: CGPoint) {
        let emitterNode = SKEmitterNode(fileNamed: "star.sks")
        emitterNode!.particlePosition = pos
        self.addChild(emitterNode!)
        self.runAction(SKAction.waitForDuration(2), completion:
            {emitterNode!.removeFromParent() })
    }

This is called in my touchesDidBegin method:

if node.name == "charEnemy" {             
    let whackSlot = node.parent!.parent as! WhackSlot
    if !whackSlot.visible { continue }
    if whackSlot.isHit { continue }
    star(touchlocation)
    whackSlot.hit()
    ++score
}

I created my own custom class WhackSlot to handle the charEnemy Node and charFriend:

let name = Barray[Int((RandomFloat(min: 0.0, max: Float(Barray.count))))]
        charNode.texture = SKTexture(imageNamed: name)
        charNode.name = "charFriend"
        charNode.id = name

    } else {
        charNode.texture = SKTexture(imageNamed: Aarray[Int((RandomFloat(min: 0.0, max: Float(Aarray.count))))])
        charNode.name = "charEnemy"
        charNode.id = ""
    }

I created a custom class for Slot:

import UIKit
import SpriteKit

class Slot: SKSpriteNode {

    var id:String?
}

So it pulls the data from my constants file where the textures have been converted in strings:

struct Constants {
    struct DEV {
        static let  DEBUG = false
    }

    struct Zoo {

        struct Animal {

            struct DOG {
              static let  NAME = "dog"
               static let IMAGE = "D1" 
            }
            struct DONKEY {
                static let  NAME = "donkey"
                static let IMAGE = "D2"


            }
            struct CAT {
                static let  NAME = "cat"
                static let IMAGE = "D3"
            }

            static let Zoo = [DOG.IMAGE,DONKEY.IMAGE,CAT.IMAGE]

        }

I have it where Image represents a SKAction that plays a audio file correlating with my constants file. Instead of just giving Stars at the touch location I would like to have each image have its own emitter.

Things I have tried to this point: Endless tutorials, RW, Cartoon Smart. I have the one emitter working, and I can assign a different emitter to charFriend and charEnemy yet I can assign each individual animal their own emitter, I tried adding in the Struct Constants, no dice, I tried if-else statements breaking out each image yet charFriend supersedes it and the same for charEnemy,

This and adding an array of sounds in my Constants for each individual image are my hang ups.

Any suggestions would be helpful. I have hardcoded the game scene, version 2 will be done completely in SpriteKit Scene editor. This is last past of my "Juice" before I submit my first app to App Store. Any guidance would greatly appreciated.

Legacy Eternal.

Latest Software being used.

A little tip I learned a long my journey, if you Make your Game Scene the same size as the Apple TV universal assets @1X optimized for all devices, and for the weird iPad screen just extend your BGNode a little above the parameters and it will automagically convert itself to fit as well.


Solution

  • Although it is nice to think you can have random particles for each SpriteNode, (mine where in a array of images converted into Strings) I found that the best approach is to break out each individual node in the array. This called for creating multiple game scenes, with the nodes, that I wanted to have emitters being represented individually. Particles also eat up a lot performance, so short burst, still look cool, and are effective. adjusting the life time settings, achieved the desired effect.

    I decided to use the editor, to see my results in real time.

    It is also really enjoyable to see the type of effects, you can make, and manipulating the ranges in which they reach across the play screen. Here is on created making fire, with a smoky effect at the end. Using the alpha scale, to determine how much color, shows and where, as well as how fast the color presents itself.

    my main lesson was to break out the nodes out of the array, and created different logic without ruining the gaming experience.