[Solved] Tutorial, i'm stuck with "for i = ....... do ....."

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Tchey
Prole
Posts: 25
Joined: Sun Jan 10, 2016 10:44 am
Contact:

[Solved] Tutorial, i'm stuck with "for i = ....... do ....."

Post by Tchey »

Hello,

I'm still at the very first steps of learning coding and lua and Love and all.

From this : http://www.youtube.com/watch?v=iT2hmjvgj-E

I'm making several changes, step by steps, to have a "horizontal" Invaders, instead of "vertical" one. My enemies are coming from the right, and i'm on the left, moving up and down. I didn't use images, but i have sounds and music, and i made the code so the player can move everywhere inside the left green rectangle without going out with a 3px margin. I didn't do the collision yet.

My bullets are laser beam, so they are small rectangles instead of small dots. My enemies are red, 30x30 px.

My troubles are with the part to spawn enemies. I don't understand what does this, step by step. I got now the program spawn several enemies instead of one, but how ?

Code: Select all

  -- for i=0, 10 do
  --   enemiesController:spawnEnemy(i * 15, 0)
  -- end
When i try, i have a long red rectangle on my upper part, moving to the left. When i change things to see what they do, i have for example this : several red rectangles on the upper parts, moving to the left, not where i want them.

Code: Select all

  for i=0, 10 do
    enemiesController:spawnEnemy(i * 50, 0)
  end
This is what i get. Could you explain me this part please ?
160115_212958.jpg
160115_212958.jpg (24.25 KiB) Viewed 2056 times
Last edited by Tchey on Fri Jan 15, 2016 10:44 pm, edited 2 times in total.
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Tutorial, i'm stuck with "for i = ....... do ....."

Post by Davidobot »

I didn't look at the video, but that is a basic "for" loop in Lua. It's syntax is

Code: Select all

for variable = initialValue, EndValue, Step do
Basically, it's spawning 10 enemies, and placing them at a distance of 50 px apart.
If that didn't answer your question, then I misunderstood.
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
User avatar
Tchey
Prole
Posts: 25
Joined: Sun Jan 10, 2016 10:44 am
Contact:

Re: Tutorial, i'm stuck with "for i = ....... do ....."

Post by Tchey »

Thanks,

I can't find out how to place my enemies on the right side of the screen, in column, so they come from right to left.
I can get them moving like i want, and spawn them one by one with for example :

Code: Select all

enemiesController:spawnEnemy(700, 200)
enemiesController:spawnEnemy(700, 300)
enemiesController:spawnEnemy(700, 400)
But i don't get it with this "for i = ....... do ....."

With more tries, i managed to get 5 enemies with space between them, but they still spawn on a line, no a column.

Code: Select all

  for i=10, 14 do
    enemiesController:spawnEnemy(i * 70, 30)
  end
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Tutorial, i'm stuck with "for i = ....... do ....."

Post by Davidobot »

Try doing (700, 50*i) instead of what you have now.
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
pedrosgali
Party member
Posts: 107
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

Re: Tutorial, i'm stuck with "for i = ....... do ....."

Post by pedrosgali »

Try switching

enemiesController:spawnEnemy(i*70, 30)
to
enemiesController:spawnEnemy(370, i*30)

That should spawn them at x = 370, y = i * 30 which is the far right in a column.
The for loop works like this, you give it 2 numbers and it runs the code between that many times with i being set as the value the loop counter is currently on. In your case you provide the numbers 10 to 14 so the first loop round i = 10, then 11, then 12 etc.
You can provide a third number after the end value as a step value, this defaults to 1 if you don't provide one. So you could write

for i = 14, 10, -1 do...

and it would start at 14 counting backwards to 10. Hope this helps.

Code: Select all

if not wearTheseGlasses() then
  chewing_on_trashcan = true
end
bobbyjones
Party member
Posts: 730
Joined: Sat Apr 26, 2014 7:46 pm

Re: Tutorial, i'm stuck with "for i = ....... do ....."

Post by bobbyjones »

Ok so "i" will tell you what iteration you are on. You can use that and some math to position your enemies. If you want enemies in columns then you can use I to position your enemy along the y.

Code: Select all

for i=1,10 do --i=1 not 0. 0 would get you plus 1 enemies.
    spawnEnemy(10,(i-1)*50) --this will spawn enemies at x 10 and y starting from 0 with spaces of 50.
end

--if you want multiple columns to can use two for loops
for i=1,5 do --5 columns
    for j=1,5 do -- 5 rows
         spawnEnemy(50*(i-1),50*(j-1))
     end
end
       
I also recommend (if you have time for detour) to take a look at this Khan academy course for computer programming. It will teach you a lot of the basics needed. It will teach you in JavaScript but the lessons are transferable to lua and love. IMO some one should translate those tutorials to love. Would be neat. Link below.

https://www.khanacademy.org/computing/c ... rogramming
User avatar
Tchey
Prole
Posts: 25
Joined: Sun Jan 10, 2016 10:44 am
Contact:

Re: Tutorial, i'm stuck with "for i = ....... do ....."

Post by Tchey »

Thanks all,

i don't understand fully everything but i managed to have one column of enemies moving at the player, and collision, and gameover screen. I'll build more over this to understand different things.

Code: Select all

  for i=2, 11 do
    enemiesController:spawnEnemy(780, (i-1)*50)
  end
  
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 3 guests