Function To Create Multiple Squares

Hey guys, i’m new in corona just created one simple game to test, now i’m trying to do an aplication with multiple squares but make one by one isn’t practical so i tried this, but doesn’t works. What whould be the solution if have one?

[lua]function createSquare(name, w, h, x ,y)

  name = display.newRect(w, h , x, y)

  name:setFillColor(0,0,0)

  return name

end

createSquare(button, halfw, halfh, 40, 40)

button.touch = createSquare

button:addEventListener(“touch”, button)

[/lua]

Close but not quite…

local function createSquare( w, h, x ,y) local obj = display.newRect(w, h , x, y) obj:setFillColor(0,0,0) return obj end 

Then later use it like this:

local width = 100 local height = 30 local x = 50 for i = 1, 5 do local tmp = createSquare( x, 100, width, height ) end

As far as adding listeners, etc.  I don’t know what you’re trying to achieve there, but the code as I read it didn’t make sense to me, so I ignored it.

For the future, once you get your feet under you (not appropriate for very beginners), be aware that SSK2 really simplifies this for you.

See some of the examples here of one-line builders, including visual params, physics bodies, etc. all in a single call:

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/display_samples/

sskversuspure.gif

colorswitch.gif

chomp.gif

Thank you man
You helped a lot, i wanna see the ssk2
Now you can understand (or not) my problem
I was trying to make some buttons but i don’t like to create every button i need, then i was thinking in a function to do it for me but didn’t work.
Its was just a test to see if works, and now it’s working ¯_(ツ)_/¯

[lua]

halfw = display.contentWidth/2

halfh = display.contentHeight/2

display.newImage(“images/alo.png”, halfw , halfh) --bkg

as = display.newText(“alo”, halfw , halfh - 100) --text that appears and disappears

as:setFillColor(1,0,0)

as.alpha = 0

function show(self, event)

  if event.phase == “began” then

    self:removeEventListener(“touch”, show)

    if self == square then

      as.alpha = 1

    else

      as.alpha = 0

    end

  end

end

function createSquare(w, h, x ,y)

  sq = display.newRect(w, h , x, y)

  sq:setFillColor(0,0,0)

  return sq

end

square = createSquare(halfw, halfh, 40, 40)

square2 = createSquare(halfw/2, halfh/2, 40, 40)

square.touch = show

square:addEventListener(“touch”, square)
square2.touch = show
square2:addEventListener(“touch”, square2)[/lua]

obs: if have some error in the code is cuz i’m translating manually

Just a tip - look into the difference between local and global scope.

In your code, all of your variables and functions are global, because they do not have the local keyword before them.

This means they will not be automatically cleaned up when the scene/function in which they were defined has finished.

For example, this code is potentially troublesome:

[lua]

display.newImage(“images/alo.png”, halfw , halfh)

  – no reference kept, so what happens when you want to delete /change the background?

 – this image is now staying in memory until the app is killed…

[/lua]

[lua]

  square = createSquare(halfw, halfh, 40, 40)

   – square is global. If you accidentally assign ‘square’ somewhere else in your code, this will be overwritten and you’ll lose reference to the object.

  – you’ll have to remember to manually remove and nil square (and everything else defined in the scene) when you no longer want it.

[/lua]

So you can see if you had tens or hundreds of functions, all using global variables for even temporary calculations, those will stay in memory until you delete them by hand.

The bkg don’t change in the code, it is just a white static image but i’ll change this and put display.setDefault. But the square i need to make some operations, In this code square do nothing but in the original will do
i just did 

Square = … exactly to remember: “Ok, this Square is what i cant change”

Well, the point I’m trying to make is to generally put local in front of every variable/reference you create. The exception would be something like:

[lua]

local myBackground  – define background at top of lua file, but we don’t want to draw it just yet.

— later on in the file

local createScene = function ()

  myBackground = display.newImage(“images/alo.png”, halfw , halfh)

  

   – assign background image to the local variable myBackground.

end

createScene()

[/lua]

If we put local myBackground inside createScene(), we would lose the reference as soon as that function completed.

Close but not quite…

local function createSquare( w, h, x ,y) local obj = display.newRect(w, h , x, y) obj:setFillColor(0,0,0) return obj end 

Then later use it like this:

local width = 100 local height = 30 local x = 50 for i = 1, 5 do local tmp = createSquare( x, 100, width, height ) end

As far as adding listeners, etc.  I don’t know what you’re trying to achieve there, but the code as I read it didn’t make sense to me, so I ignored it.

For the future, once you get your feet under you (not appropriate for very beginners), be aware that SSK2 really simplifies this for you.

See some of the examples here of one-line builders, including visual params, physics bodies, etc. all in a single call:

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/display_samples/

sskversuspure.gif

colorswitch.gif

chomp.gif

Thank you man
You helped a lot, i wanna see the ssk2
Now you can understand (or not) my problem
I was trying to make some buttons but i don’t like to create every button i need, then i was thinking in a function to do it for me but didn’t work.
Its was just a test to see if works, and now it’s working ¯_(ツ)_/¯

[lua]

halfw = display.contentWidth/2

halfh = display.contentHeight/2

display.newImage(“images/alo.png”, halfw , halfh) --bkg

as = display.newText(“alo”, halfw , halfh - 100) --text that appears and disappears

as:setFillColor(1,0,0)

as.alpha = 0

function show(self, event)

  if event.phase == “began” then

    self:removeEventListener(“touch”, show)

    if self == square then

      as.alpha = 1

    else

      as.alpha = 0

    end

  end

end

function createSquare(w, h, x ,y)

  sq = display.newRect(w, h , x, y)

  sq:setFillColor(0,0,0)

  return sq

end

square = createSquare(halfw, halfh, 40, 40)

square2 = createSquare(halfw/2, halfh/2, 40, 40)

square.touch = show

square:addEventListener(“touch”, square)
square2.touch = show
square2:addEventListener(“touch”, square2)[/lua]

obs: if have some error in the code is cuz i’m translating manually

Just a tip - look into the difference between local and global scope.

In your code, all of your variables and functions are global, because they do not have the local keyword before them.

This means they will not be automatically cleaned up when the scene/function in which they were defined has finished.

For example, this code is potentially troublesome:

[lua]

display.newImage(“images/alo.png”, halfw , halfh)

  – no reference kept, so what happens when you want to delete /change the background?

 – this image is now staying in memory until the app is killed…

[/lua]

[lua]

  square = createSquare(halfw, halfh, 40, 40)

   – square is global. If you accidentally assign ‘square’ somewhere else in your code, this will be overwritten and you’ll lose reference to the object.

  – you’ll have to remember to manually remove and nil square (and everything else defined in the scene) when you no longer want it.

[/lua]

So you can see if you had tens or hundreds of functions, all using global variables for even temporary calculations, those will stay in memory until you delete them by hand.

The bkg don’t change in the code, it is just a white static image but i’ll change this and put display.setDefault. But the square i need to make some operations, In this code square do nothing but in the original will do
i just did 

Square = … exactly to remember: “Ok, this Square is what i cant change”

Well, the point I’m trying to make is to generally put local in front of every variable/reference you create. The exception would be something like:

[lua]

local myBackground  – define background at top of lua file, but we don’t want to draw it just yet.

— later on in the file

local createScene = function ()

  myBackground = display.newImage(“images/alo.png”, halfw , halfh)

  

   – assign background image to the local variable myBackground.

end

createScene()

[/lua]

If we put local myBackground inside createScene(), we would lose the reference as soon as that function completed.