0

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.

4

1 回答 1

0

如果你不工作,你的意思是什么都没有发生,也没有在屏幕上绘制任何东西,那是因为你没有将你的形状添加到舞台层次结构中

它应该是这样的:

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)
于 2014-10-15T15:07:06.330 回答