i'm working on first iphone game , have skspritenode subclass alien
(which abstract class) later inherited class normalien
. apple's docs, i've been having lot of trouble figuring out class initialization , inheritance in swift. here code both classes:
//generic alien type: blue-print of sorts class alien:skspritenode{ static func normalizevector(vector:cgvector) -> cgvector{ let len = sqrt(vector.dx * vector.dx + vector.dy * vector.dy) return cgvector(dx:vector.dx / len, dy:vector.dy / len) } func motion(velocity:cgvector, multiplier:cgfloat){ //alien.physicsbody?.velocity.dx = velocity.dx * multiplier //why no work? self.physicsbody?.velocity.dx = velocity.dx * multiplier self.physicsbody?.velocity.dy = velocity.dy * multiplier } let velocityvector:cgvector let startpos:cgpoint init(texture:sktexture, startposition startpos:cgpoint,movespeed: cgfloat,velocityvector:cgvector){ self.velocityvector = alien.normalizevector(velocityvector) self.startpos = startpos //makes sure skspritenode initialized before modifying properties super.init(texture: texture, color: uicolor.clearcolor(), size: texture.size()) //physicsbody property of super super.init must called first (init skspritenode) self.physicsbody? = skphysicsbody(circleofradius: self.size.width/2) self.physicsbody?.dynamic = true self.physicsbody?.categorybitmask = physicscategory.alien //physicsbody?. optional chaining? self.physicsbody?.collisionbitmask = 0 //do need this? or jsut use in laser class self.physicsbody?.contacttestbitmask = physicscategory.laser self.physicsbody?.usesprecisecollisiondetection = true //motion self.physicsbody?.velocity.dx = velocityvector.dx * movespeed self.physicsbody?.velocity.dy = velocityvector.dy * movespeed //self.velocityvector = normalizevector(velocityvector) self.position = startpos } //alien.motion(self.velocityvector, movespeed) required init?(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") } }
as as:
class normalien:alien{ static let alienimage = sktexture(imagenamed:"sprites/alien.png") //alienimage.setscale(0.2) init(startpos:cgpoint,speed: cgfloat){ super.init(texture:normalien.alienimage, startposition: startpos, movespeed:speed,velocityvector:cgvector(dx: 1,dy: 0)) } required init?(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") } }
i have function:
func buildalien(){ let aalien = normalien(startpos:cgpoint(x:0,y:size.height/2), speed:100) addchild(aalien) }
that instantiates normalien
within didmovetoview
(this function called within skaction.repeatactionforever
). reason each sprite created fails move. i'm confused because move if use skaction.moveto
must not understand applying velocity.
on top of this, i'm confused why receive error: expected declaration
when attempt manipulate normaliens
texture alienimage.setscale(0.2)
, when try call internal method on class: alien.motion(self.velocityvector, movespeed)
? think calling method inside class cause every instance make have called- making them potentially move.
any appreciated.
Comments
Post a Comment