Lua Gideros: Trouble drawing line with touch

2019-06-12 15:54发布

问题:

In my Game using Gideros and Lua, I want players to be able to draw a straight line from the point they touch the screen until the point they release. However, when I try run this code, I always get an error message. Here is the code:

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

    event:stopPropagation()
end

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

end

This next line is line 66 in my code:

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

Here is the line where I set up "scene":

scene = gideros.class(Sprite)

Here is my error message:

main.lua:66: index '__userdata' cannot be found stack traceback: main.lua:66: in main chunk

Does anyone know why I am getting this message?

回答1:

If you do

scene = gideros.class(Sprite)

it means scene is a class, but you can add event listeners only to the instance of the class, not the class itself.

So something like this should work:

Scene = gideros.class(Sprite)
local scene = Scene.new()
stage:addChild(scene)