Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Making a Square Grid — Gideros Forum

Making a Square Grid

QuasarCreatorQuasarCreator Member
edited July 2012 in General questions
Hello,

I am making a 20x20 square grid with squares that are 40x40 pixels.
Here is my code right now:
 
local a
local b = -1
local x
local y = -1
 
for h = 0, 20 do
	b = b + 1
	a = b * 40
 
	for i = 0, 20 do
		y = y + 1
		x = y * 40
		local square = Bitmap.new(Texture.new("images/square.png"))
		square:setPosition(x,a)
		self:addChild(square)
 
	end
end
However, I am only getting one row of squares. Here is my reasoning behind the code. The first loop let's call h and the second loop lets call i. So, I thought that once h has looped once i has looped 20 times, thus making 20 squares at the y coordinate of 0. Then when h loops again the y coordinate or a is added 40 pixels. Then i would loop another 20 times , thus making 20 squares at the y coordinate of 40. Then it keeps going until h has looped twenty times and i has looped 400 times. Since I am only getting one row of 20 squares, I don't get why it isn't looping more times and adding 40 to the y coordinate each time h loops.

So, please help me understand why my code isn't working the way I intended. Thank you very much!!

P.S. I didn't get any errors.
I am a programming noobie, so thanks in advance for helping me out and answering my questions!

Comments

  • ar2rsawseenar2rsawseen Maintainer
    edited July 2012
    Well your problem is that, you need to reset x coordinate after each 20 loops, or else your squares are positioned like that:
    0 0 0 0
    _ _ _ _ 0 0 0 0
    _ _ _ _ _ _ _ _ 0 0 0 0
    etc.
    They are probably getting outside the screen or something.

    So I guess something like that:
    local a
    local b = -1
    local x
    local y = -1
     
    for h = 0, 20 do
    	b = b + 1
    	a = b * 40
     
    	for i = 0, 20 do
    		y = y + 1
    		x = y * 40
    		local square = Bitmap.new(Texture.new("images/square.png"))
    		square:setPosition(x,a)
    		self:addChild(square)
     
    	end
    	y = -1
    end
    Should do the trick.
    Funny that you mentioned it, while I was just working on one of components called GridView for new GameTemplate :D

    Likes: QuasarCreator

    +1 -1 (+1 / -0 )Share on Facebook
  • @ar2rsawseen Thank you so much it works perfectly! I never thought that my squares were outside my screen since.

    I also wanted to say that your GameTemplate has helped me so much and probably every other beginner out there, so thanks. :)>-
    I am a programming noobie, so thanks in advance for helping me out and answering my questions!
Sign In or Register to comment.