Lua Gideros: Line with touch 2

89 views Asked by At

In my game using Lua and Gideros studio, I want someone to draw a straight line from mouse down to mouse up. Here is my code that doesn't work:

local function onMouseDown(event)
    startx = event.x
    starty = event.y

    event:stopPropagation()
end

local function onMouseUp(event)
    endx = event.x
    endy = event.y
    local line = Shape.new()
    line:setLineStyle(5, 0x0000ff, 1)
    line:beginPath()
    line:moveTo(startx,starty)
    line:lineTo(endx,endy)
    line:endPath()
    event:stopPropagation()
end

place:addEventListener(Event.MOUSE_DOWN, onMouseDown)
place:addEventListener(Event.MOUSE_UP, onMouseUp)

Anybody know why this isn't working? Thanks!

This is part 2 of my other question.

1

There are 1 answers

1
Artūrs Sosins On BEST ANSWER

If you by not working, you mean that nothing is happening and nothing is drawn on the screen, then it is because you did not add your shape to the stage hiearchy

It should be like this:

local line = Shape.new()
line:setLineStyle(5, 0x0000ff, 1)
--can add to stage or maybe place, 
--if that's what you are using for scene
stage:addChild(line)

local function onMouseDown(event)
    startx = event.x
    starty = event.y

    event:stopPropagation()
end

local function onMouseUp(event)
    line:beginPath()
    line:moveTo(startx,starty)
    line:lineTo(event.x,event.y)
    line:endPath()
    event:stopPropagation()
end

place:addEventListener(Event.MOUSE_DOWN, onMouseDown)
place:addEventListener(Event.MOUSE_UP, onMouseUp)