Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Please Help me Understand This Code — Gideros Forum

Please Help me Understand This Code

QuasarCreatorQuasarCreator Member
edited July 2012 in General questions
I made a post a couple days ago asking how I could set an objects position upon release. @AlexRu amazed me. He wrote all this code to help some stranger out, so thank you. However, I am having a hard time understanding the code and why certain things were present. So, I decided to ask a few of my questions on the forums so I can better understand the code and be able to know what it means when I am debugging.

So here is the code (all credit goes to @AlexRu) :
I will have the questions embedded in the code as comments.
Also, in the code I am trying to put my sprites in my grid.
 
--///////////////////////////////////////////////////////////////////////////////////////////////	
--make your grid 2d table
grid = {}
local gridWidth = application:getContentWidth() / 40
local gridHeight = (application:getContentHeight() / 40) - 1 --so there is room for my menu
 
 
for i = 1, gridWidth do
	table.insert(grid,{})
	for j = 1, gridHeight do
		table.insert(grid[i],j)                               -- Why can't I put brackets around the [j] too?
		grid[i][j] = Sprite.new()
		grid[i][j]:addChild(Bitmap.new(Texture.new("images/square.png")))
		grid[i][j]:setPosition((i-1) * 40, (j-1) * 40)
		self:addChild(grid[i][j])
	end
end
--/////////////////////////////////////////////////////////////////////////////////////////////////
 
--x and y coordinates when the object is released
local function onMouseUp(self, event)
	print(event.x, event.y)
	local break_for_flag = false                                                 -- Why is there break_for_flag?
	for i = 1, gridWidth do
		for j = 1, gridHeight do
			if grid[i][j]:hitTestPoint(event.x, event.y) then              --Why do I have use hitTestPoint? For some reason 
				self:setPosition(grid[i][j]:getPosition())                  --if I do not use hitTestPoint my sprites go
                                                                                                           -- to coordinate (0,0).
				break_for_flag = true                                         
				break
			end                                                                  -- Are the for statements there so each square in the grid
		end                                                                     -- follows or is exposed (can't think of the right word) to the                       
                                                                                            -- if statement? 
 
 
		if break_for_flag then                                                 -- Why is this if statement here?
			break
		end
	end
	if self.isFocus then
		self.isFocus = false
		event:stopPropagation()
 	end
 end

I hope no one minds me asking all these questions, so thanks in advance!
I am a programming noobie, so thanks in advance for helping me out and answering my questions!

Comments

  • Okay I am going to go ahead and add my comments to the code so you can see why :), a lot of these are actually either programming specific--which as you mentioned you are somewhat new, you will pick them up at some point I am sure :) Some of these are the logic that makes it work for tiling as well.

    Hopefully this helps and isn't too crazy confusing--I haven't had enough coffee today lol--but these forums are pretty friendly as long as you don't expect someone to code up the whole app we are glad to help make things easier :)

    On with the comments!:
    --///////////////////////////////////////////////////////////////////////////////////////////////	
    --make your grid 2d table
    grid = {}
    local gridWidth = application:getContentWidth() / 40
    local gridHeight = (application:getContentHeight() / 40) - 1 --so there is room for my menu
     
     
    for i = 1, gridWidth do
    	table.insert(grid,{})
    	for j = 1, gridHeight do
    		table.insert(grid[i],j)                               -- Why can't I put brackets around the [j] too? 
    --[[Eli: You can't put brackets around j because it is a number, where using grid[i] means to access the table entry at i position, brackets are used to indicate that you are accessing a entry inside the table at the index point in this case i is the index, and grid is the table.]]
    		grid[i][j] = Sprite.new()
    		grid[i][j]:addChild(Bitmap.new(Texture.new("images/square.png")))
    		grid[i][j]:setPosition((i-1) * 40, (j-1) * 40)
    		self:addChild(grid[i][j])
    	end
    end
    --/////////////////////////////////////////////////////////////////////////////////////////////////
     
    --x and y coordinates when the object is released
    local function onMouseUp(self, event)
    	print(event.x, event.y)
    	local break_for_flag = false                                                 -- Why is there break_for_flag?
    --[[Eli: This is the flag that tells the loop that you should move down one 40x40 tile on the y axis, this creating a new row each time it is set.]]
    	for i = 1, gridWidth do
    		for j = 1, gridHeight do
    			if grid[i][j]:hitTestPoint(event.x, event.y) then              --Why do I have use hitTestPoint? For some reason 
    --[[Eli: Gideros Studio uses a rather global on click event, if you do not check which tile the click event is over with hitTestPoint the first object to get the event will likely be the one that processes it, and in this cause since you are drawing a grid the first object is the one in the upper corner as you noted.]]
    				self:setPosition(grid[i][j]:getPosition())                  --if I do not use hitTestPoint my sprites go
                                                                                                               -- to coordinate (0,0).
    				break_for_flag = true                                         
    				break
    			end                                                                  -- Are the for statements there so each square in the grid
    --[[
    The two loops are the same as your tiles in rows of columns, a lot like the following below.
    ########### (break flag, now increase y and start back at x = 0)
    ########### (break flag, now increase y and start back at x = 0)
    ########### (break flag, now increase y and start back at x = 0)
     
    I think you get the idea, it basically goes through and tests each tile until it finds the one that was clicked on.
    ]]
    		end                                                                     -- follows or is exposed (can't think of the right word) to the                       
                                                                                                -- if statement? 
     
     
    		if break_for_flag then                                                 -- Why is this if statement here?
    --[[Eli: This is the statement that makes the loop end--or break--once the clicked tile is found and thus the flag is set to true in the above code]]
    			break
    		end
    	end
    	if self.isFocus then
    		self.isFocus = false
    		event:stopPropagation()
     	end
     end
    ThumbHurt Games / FB: ThumbHurt Games / FB: Eli/Teranth | Skype: teranth37
    +1 -1 (+2 / -0 )Share on Facebook
  • Wow, thank you so much @Teranth you really helped me understand the code! Now I can look at my code and know what is going on and why it is there. ^:)^
    I am a programming noobie, so thanks in advance for helping me out and answering my questions!
  • @QuasarCreator anytime, I'm glad I was able to help. The more coding you do the more you will just sort of understand some of it second nature :)
    ThumbHurt Games / FB: ThumbHurt Games / FB: Eli/Teranth | Skype: teranth37
  • AlexRuAlexRu Member
    @Teranth, thanks for commenting. You're absolutely right. :)
  • @AlexRu anytime I like to try and help out where I can. :)
    ThumbHurt Games / FB: ThumbHurt Games / FB: Eli/Teranth | Skype: teranth37
Sign In or Register to comment.