Lua Gideros:触摸线2

时间:2014-10-14 21:07:04

标签: graphics lua gideros

在我使用Lua和Gideros工作室的游戏中,我希望有人从鼠标向下画直线到鼠标。这是我的代码不起作用:

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)

有人知道为什么这不起作用?谢谢!

这是我另一个问题的第二部分。

1 个答案:

答案 0 :(得分: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)