Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Trying to tell if a sprite is next to another — Gideros Forum

Trying to tell if a sprite is next to another

QuasarCreatorQuasarCreator Member
edited July 2012 in General questions
So, I am trying to write some code where there will be multiple shapes. The user can drag a shape and place it in a grid and then the user can "connect" another shape to the one just placed in the grid. I am having a hard time figuring out how I can tell when a shape is next to another in the grid. I was wondering if anyone could give me some ideas or hints to help me out.
[-O<

Here is some code I have came up with so far:
 
	local startFlow = false
	local moveFlow = false	
	local shapeX = self:getX()--x coordinate of the shape
	local shapeY = self:getY()--y coordinate of the shape
	local flow = 0
 
	--the starting shape which starts the flow
	if shapeX == 80 and shapeY == 40 then
		local startFlow = true
		local moveFlow = true
		print("flow has began")
		flow = flow + 1
		local controlShapeX = shapeX
		local controlShapeY = shapeY
 
	end
I know that piece of code is very ugly haha!

Okay, in this piece of code I am trying to make see if a shape is next to another shape. In the if statement I am trying to tell if the first shape is "connected" to the starting point which means the flow (of water) begins and the flow moves 1 shape. I am trying to make the last shape the control shape to see if the next shape is next to the last shape. So, I am trying to make the first shape my control shape. Then, once the next shape is connected to the first shape; the second shape becomes the control shape to see if the next shape is next to it. So, how can I make the last shape the control shape? Also, how should I compare the two shapes x and y coordinates to tell if they are next to each other in the grid?

If there is another way to tell if multiple shapes are next to each other please tell me!

So, if you have any hint, tips, or ideas I could try out I would be very thankful!! :D ^:)^
I am a programming noobie, so thanks in advance for helping me out and answering my questions!

Comments

  • What you need to do is have a field in your object that contains a reference to the objects on the left and on the right. These would start of as nil (meaning they weren't connected) and then when you place an object down you can can scan through the list of existing objects to see which one collides with or shares an edge with the current object - you then set the "right" reference in the existing object to the current one and the "left" reference in the current object to the existing one, then as you do this you'll build up a simple linked list type of structure.

    Sorry if this sounds confusing (it's late here) - try sketching it on a bit of paper and you should get an idea of how it works or ask again if you get struck. :)

    Likes: QuasarCreator

    WhiteTree Games - Home, home on the web, where the bits and bytes they do play!
    #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
    +1 -1 (+1 / -0 )Share on Facebook
  • @techdojo Thank you! I think I understand what your saying. I will try to draw it and code it.
    I am a programming noobie, so thanks in advance for helping me out and answering my questions!
Sign In or Register to comment.