SSK2: very basic question about moving stuff

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.

:frowning:

Answer Part 1
 
Thanks for the code post and your question is pretty good, but you are missing one key part.
 
You didn’t clarify what you mean by:

 I just cannot get it to work.

 
Tip: I wrote this post some time ago: “Ask A Better Question, Get A Better Answer”.  It talks about asking questions in a way that will help others to help you.  (Don’t read it now, but it might be worth a read later.)
 
One key part of that post was this list of info I love to see in posts:

  • What you did

  • What you saw

  • What you expected to see
    You’ve given me #1 and part of #3, please elaborate on #2 and maybe a bit more on #3.
     
    Answer Part 2
    I do notice you did not listen for the event.  This is what I would expect the listener code to look like (abbreviated)

    function enemy.enterFrame(self) … end listen(“enterFrame”, enemy) – << I don’t see this.

Answer Part 3 - Efficiency and SSK2 Auto-Listener feature
You can code the above a little more efficiently.  Also, ‘enterFrame’ is one of the listeners SSK2 watches for and auto-attaches if is detected in the visualParams list of the factory.
 
I would do this instead:

local function enterFrame(self) -- Efficient: \<\< Define just a single instance of the function ... body of function end local function newWave () ... enemy = newRect( layers.aliens, sx, sy, { cornerRadius = 8, collision = onCollision, myType = "invader", fill = \_W\_, w=30, h=30, enterFrame = enterFrame }, -- \<\<\<\< Auto attaches {calculator = myCC, colliderName = "invader" } ) ... end

Here is another example: https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/display_standard/#listener-example-2-enterframe

Tip:  Thanks again for the code post, but let me suggest one thing.  My guess is you are using Sublime or another app that auto-indents with TAB characters.
 
TABs mess up the legibility of code posts in the forums.
 
When pasting code please take these extra stesp:

  • Grab the code in your editor of choice.
  • Paste your (copied) code into a plain text editor like notepad.exe (or the equivalent on OS X),
  • Replace all tabs with 2 or 3 spaces (up to you)
  • Grab that in the copy buffer
  • Paste the modified code to the forums.
  • Manually correct wraparound lines and other legibility issues.

Ex:

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

Firstly, you are of course absolutely correct, I didn’t clarify the problem I was having.  Big slapped wrist for me as it annoys me when other people do the same. DOH!  

Secondly, thank you once again for the help.  What’s weird is that (at some point) I had had the listen statement in the constructor and the only thing I can think of is that I took it out while trying to fix something else.

Lastly, thanks for the pointer on the tabs in the code indentation.

Answer Part 1
 
Thanks for the code post and your question is pretty good, but you are missing one key part.
 
You didn’t clarify what you mean by:

 I just cannot get it to work.

 
Tip: I wrote this post some time ago: “Ask A Better Question, Get A Better Answer”.  It talks about asking questions in a way that will help others to help you.  (Don’t read it now, but it might be worth a read later.)
 
One key part of that post was this list of info I love to see in posts:

  • What you did

  • What you saw

  • What you expected to see
    You’ve given me #1 and part of #3, please elaborate on #2 and maybe a bit more on #3.
     
    Answer Part 2
    I do notice you did not listen for the event.  This is what I would expect the listener code to look like (abbreviated)

    function enemy.enterFrame(self) … end listen(“enterFrame”, enemy) – << I don’t see this.

Answer Part 3 - Efficiency and SSK2 Auto-Listener feature
You can code the above a little more efficiently.  Also, ‘enterFrame’ is one of the listeners SSK2 watches for and auto-attaches if is detected in the visualParams list of the factory.
 
I would do this instead:

local function enterFrame(self) -- Efficient: \<\< Define just a single instance of the function ... body of function end local function newWave () ... enemy = newRect( layers.aliens, sx, sy, { cornerRadius = 8, collision = onCollision, myType = "invader", fill = \_W\_, w=30, h=30, enterFrame = enterFrame }, -- \<\<\<\< Auto attaches {calculator = myCC, colliderName = "invader" } ) ... end

Here is another example: https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/display_standard/#listener-example-2-enterframe

Tip:  Thanks again for the code post, but let me suggest one thing.  My guess is you are using Sublime or another app that auto-indents with TAB characters.
 
TABs mess up the legibility of code posts in the forums.
 
When pasting code please take these extra stesp:

  • Grab the code in your editor of choice.
  • Paste your (copied) code into a plain text editor like notepad.exe (or the equivalent on OS X),
  • Replace all tabs with 2 or 3 spaces (up to you)
  • Grab that in the copy buffer
  • Paste the modified code to the forums.
  • Manually correct wraparound lines and other legibility issues.

Ex:

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

Firstly, you are of course absolutely correct, I didn’t clarify the problem I was having.  Big slapped wrist for me as it annoys me when other people do the same. DOH!  

Secondly, thank you once again for the help.  What’s weird is that (at some point) I had had the listen statement in the constructor and the only thing I can think of is that I took it out while trying to fix something else.

Lastly, thanks for the pointer on the tabs in the code indentation.