Once again apologies for the ‘noobness’ of this question but it has me tearing my hair out.
I have a number of objects (aliens) that are generated using a loop. I would like to attach a listener to them so that they automatically move by themselves.
However, for some bizarre reason, I just cannot get it to work.
This is the code for generating the wave of aliens…
local function newWave () totalEnemies=0 local row, col local sx, sy = 30, 50 local d='r' for col=1, 10 do sx=102 for row=1, 21 do enemy = newRect( layers.aliens, sx, sy, { cornerRadius = 8, collision = onCollision, myType = "invader", fill = \_W\_, w=30, h=30}, {calculator = myCC, colliderName = "invader" } ) -- Remember to change 100 to -100 if d =='r' then enemy.dir='right' else enemy.dir='left' end enemy.alpha=0.5; enemy.live=true; enemy.change=0 totalEnemies = totalEnemies + 1 sx = sx + 40 Runtime:addEventListener ( "enterFrame", enemy ) end sy=sy+(40) -- 20 if d=="r" then d="l" else d="r" end end end
and this is the code for moving them…
function enemy.enterFrame(self) if game\_status == "playing" then local ew=self.contentWidth/2 if self.dir=="right" and self.change==0 then self.x=self.x+speed if self.x\>=(w-ew) and self.change==0 then self.down=self.y self.dir="down" self.change=1 end elseif self.dir=="left" and self.change==0 then self.x=self.x-speed if self.x\<=(ew) then self.down=self.y self.dir="down" self.change=1 end elseif self.dir=="down" and self.change==0 then self.y=self.y+speed if self.y\>=(self.down+(40)) then self.down=self.y self.change=1 if self.x\>w/2 then self.dir="left" elseif self.x\<w/2 then self.dir="right" end end end self.change=0 end end
When they move alternate rows move in different directions, which then move down when the reach the edge of the screen, so you effectively get two lots of aliens zigzagging down the screen (if that makes sense).
I know that this isn’t exactly a question directly related to SSK2 but as I have used it in my code.
I can’t for the life of me figure this out.