简体   繁体   中英

Corona SDK. Connect two physics bodies by drawing a straight line

I am trying to make a game like the famous Dots: A game about connecting, where you have to connect the dots that are the same colours. The problem I have is with drawing the line. It has to be straight and to start from one dot(physics object) and to snap to another dot which is the same colour. It would be much appreciated if you help me understand how to draw a straight line that snaps to another object.

You don't need the line to snap, you just need it to look like it is snapping.

CoronaSDK has a method to draw a line and another one to draw a rectangular.

display.newLine( [parentGroup,], x1, y1, x2, y2 )
display.newRect( left, top, width, height )

You can use whichever you like the most.

local originX     -- the X coordinate where the line starts
local originY     -- the Y coordinate where the line starts
local finalX      -- the X coordinate where the line ends
local finalY      -- the Y coordinate where the line ends
local parentGroup -- the line's parent group
local rectWidth   -- the rect's width (this is the distance between originX and finalX)
local rectHeight  -- the rect's height

--with newLine
local line = display.newLine(parentGroup, originX, originY , finalX, finalY )

--with newRect
local line = display.newRect(parentGroup, originX, originY , rectWidth, rectHeight )
line:setReferencePoint( display.CenterLeftReferencePoint ) -- draws the line from the left

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM