Well, I fixed most of the problems, but when the program starts, I am unable to draw lines. Everything else seems to be fine, though.
[code]
local physics = require(“physics”)
physics.start()
physics.setScale( 60 )
physics.setGravity(0, 10)
display.setStatusBar( display.HiddenStatusBar )
local background = display.newImage( “images/clouds.png”, true )
– Create player and set location
local player = display.newImage(“images/fred.png”)
player.x = display.contentWidth / 2
player.y = display.contentHeight / 3
– Turn player into physics body
physics.addBody(player)
– Create walls on the left and right
local leftWall = display.newRect (0, 0, 1, display.contentHeight);
local rightWall = display.newRect (display.contentWidth, 0, 1, display.contentHeight);
– Add physics to the walls. (static)
physics.addBody (leftWall, “static”, { bounce = 0.1 } );
physics.addBody (rightWall, “static”, { bounce = 0.1 } );
local cw = display.contentWidth
local ch = display.contentHeight
local lineLengthConstraint = 128
local function checkLineLength(line)
local x = (line.contentBounds.xMax-2) - (line.contentBounds.xMin+2)
local y = (line.contentBounds.yMax-2) - (line.contentBounds.yMin+2)
local length = math.sqrt(x*x + y*y)
return length
end
local function drawLine(event)
Runtime:addEventListener(“touch”, drawLine)
if event.phase == “began” then
– inTouch state.
inTouch = true
– X/Y Coordinates of initial touch.
initialX = event.x
initialY = event.y
– Create Line #1.
line1 = display.newLine(initialX, initialY, event.x, event.y)
line1.width = 1
line1.alpha = 0.25
–line1.isVisible = false
– Create Line #2, using the length and rotation of Line #1.
line2 = display.newLine(initialX, initialY, initialX, initialY+checkLineLength(line1))
line2:setColor(0,255,0)
line2.width = 8
line2.rotation = math.deg(math.atan2(event.y - initialY, event.x - initialX)) - 90
elseif event.phase == “moved” and inTouch then
– Remove old Line #1.
display.remove(line1)
line1 = nil
– Create new Line #1.
line1 = display.newLine(initialX, initialY, event.x, event.y)
line1.width = 1
line1.alpha = 0.25
–line1.isVisible = false
– Recreate Line #2, in the case that Line #1 is shorter than the length constraint.
if checkLineLength(line1) <= lineLengthConstraint then
– Remove old Line #2.
display.remove(line2)
line2 = nil
– Create new Line #2, using the length of Line #1.
line2 = display.newLine(initialX, initialY, initialX, initialY+checkLineLength(line1))
line2:setColor(0,255,0)
line2.width = 8
end
– Set rotation of Line #2.
line2.rotation = math.deg(math.atan2(event.y - initialY, event.x - initialX)) - 90
elseif event.phase == “ended” and inTouch then
– inTouch state.
inTouch = false
– Remove all lines.
display.remove(line1)
line1 = nil
display.remove(line2)
line2 = nil
end
end [import]uid: 188277 topic_id: 34907 reply_id: 138925[/import]
[import]uid: 12217 topic_id: 34907 reply_id: 138936[/import]