arrays - How do I update unlockable characters in SpriteKit game with Swift 3? -


i have made game featuring 1 player. made character screen user can choose character he/she wants play with. how make high score unlocks character, , allows user equip character use in game?

right player has own swift file defines properties of him:

import spritekit class player: skspritenode, gamesprite { var initialsize = cgsize(width:150, height: 90) var textureatlas: sktextureatlas = sktextureatlas(named: "rupert")  let maxflyingforce: cgfloat = 80000 let maxheight: cgfloat = 900 var health:int = 1 var invulnerable = false var damaged = false var damageanimation = skaction() var dieanimation = skaction() var forwardvelocity: cgfloat = 190 var poweranimation = skaction()    init() { super.init(texture:nil, color: .clear, size: initialsize) createanimations() self.run(soaranimation, withkey: "soaranimation") let bodytexture = textureatlas.texturenamed("pug3") self.physicsbody = skphysicsbody(texture: bodytexture, size: self.size) self.physicsbody?.lineardamping = 0.9 self.physicsbody?.mass = 10 self.physicsbody?.allowsrotation = false  self.physicsbody?.categorybitmask = physicscategory.rupert.rawvalue self.physicsbody?.contacttestbitmask = physicscategory.enemy.rawvalue | physicscategory.treat.rawvalue | physicscategory.winky.rawvalue | physicscategory.ground.rawvalue   func createanimations() { let rotateupaction = skaction.rotate(toangle: 0.75, duration: 0.475) rotateupaction.timingmode = .easeout let rotatedownaction = skaction.rotate(toangle: 0, duration: 0.475) rotatedownaction.timingmode = .easein let flyframes: [sktexture] = [ textureatlas.texturenamed("pug1"), textureatlas.texturenamed("pug2"), textureatlas.texturenamed("pug3"), textureatlas.texturenamed("pug4"), textureatlas.texturenamed("pug3"), textureatlas.texturenamed("pug2")     ] let flyaction = skaction.animate(with:flyframes, timeperframe: 0.07) flyanimation = skaction.group([skaction.repeatforever(flyaction), rotateupaction]) let soarframes:[sktexture] = [textureatlas.texturenamed("pug5")] let soaraction = skaction.animate(with: soarframes, timeperframe: 1) soaranimation = skaction.group([skaction.repeatforever(soaraction), rotatedownaction]) 

this not code point.

i say: let player = player() in gamescene file attaches attributes in player file player seen in gamescene. if able replace initial player different player, there many animations don't know how replace @ once. want set condition spans on both gamescene class , player class can sub out images other ones , keep same actions.

thank help!

here techniques can use making things more manageable:

  • have naming convention character images and/or sprite sheets, can pass in name player() constructor. then, instead of loading texturenamed("pug3"), load texturenamed("\(playername)3"). if difference between characters sprite sheets, need on player end.

  • if characters more complex, differences beyond images, being larger or having more health, want go more data-oriented approach. there couple of approaches this, handy 1 read texture names, hitbox sizes, health levels, etc., out of .plist file instead of hard-coding them. pass in name of .plist file load character want. then, create new character, create new .plist file. approach create "character definition" struct pass player constructor contains information need construct player (this, in turn, loaded .plist file, well, hard-code them or save them directly using codable serialization).

  • if neither of above approaches sufficient, if need different behavior between characters, go subclassing route - pull various parts , pieces out functions, , override functions add specific functionality need more complex characters.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -