Hello,
Does anyone know how i can add a physic body to this line? I want to draw the line and correspond to object hitting it, making it "static". Any help? Right now it is just a normal line that is drawn and is erased after releasing every other one. All i need is to add a physic body to it. I want to make the whole line static. Can someone please help, i really need this. Thank you SO MUCH .Help is very much APPRECIATED.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | local lines = {} local myLines = {} local prevX,prevY local function removeLine(myLine) for i=1,#myLine do myLine[i]:removeSelf() myLine[i] = nil end end local i = 1 local function drawLine(e) if "began" == e.phase then myLines[i] = {} prevX = e.x prevY = e.y elseif "moved" == e.phase then if prevX then myLines[i][#myLines[i] + 1] = display.newLine(prevX,prevY,e.x,e.y) myLines[i][#myLines[i]].width = 3 end prevX = e.x prevY = e.y elseif "ended" == e.phase then prevX = nil prevY = nil i = i + 1 --you can get to individual lines using this. removeLine(myLines[#myLines-1]) end end Runtime:addEventListener("touch",drawLine) |
How you would add physics to the lines begin created is like so (your code, slightly modified);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | local lines = {} local myLines = {} local prevX,prevY require ( "physics" ) physics.start() physics.setGravity( 0, 7.5 ) local function removeLine(myLine) for i=1,#myLine do myLine[i]:removeSelf() myLine[i] = nil end end local i = 1 local function drawLine(e) if "began" == e.phase then myLines[i] = {} prevX = e.x prevY = e.y elseif "moved" == e.phase then if prevX then myLines[i][#myLines[i] + 1] = display.newLine(prevX,prevY,e.x,e.y) myLines[i][#myLines[i]].width = 3 physics.addBody(myLines[i][#myLines[i]], "dynamic", {density = 1.0, friction = 0.3, bounce = 0.2}) end prevX = e.x prevY = e.y elseif "ended" == e.phase then prevX = nil prevY = nil i = i + 1 --you can get to individual lines using this. removeLine(myLines[#myLines-1]) end end Runtime:addEventListener("touch",drawLine) |
Thank you so much PEACH!!!!!!!!!!!!!!!!!!!!!!! I REALLY REALLY APPRECIATE IT! I really owe you a HUGE THANK YOU! Theres is only 1 problem that i'm seeing, do you know how we can change the size of the static/dynamic bodies that appear? To make them fit the line ? Thanks SO MUCH!
Haha, I didn't know how much you'd like the initial example as there's so many little pieces, but I'm glad you do :)
I'm not totally sure on your question; I could read it a few ways.
However, try changing "dynamic"
to "static"
and see what that does ;)
Peach