使用Gideros中的Lua编程:用触摸绘制直线遇到问题。

在我使用 Gideros 和 Lua 创建的游戏中,我希望玩家能够从屏幕上触摸的点画一条直线,直到他们释放。然而,当我尝试运行这个代码时,我总是收到一个错误消息。这是代码:

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

这下一行是我的代码中的第 66 行:

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

这是我设置“场景”的行:

scene = gideros.class(Sprite)

这是我的错误消息:

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

有人知道我为什么会收到这个消息吗?

点赞
用户2274511
用户2274511

如果你这样写

scene = gideros.class(Sprite)

它意味着 scene 是一个类,但你只能给该类的实例添加事件监听器,而不能给类本身添加。

因此,你可以这样做:

Scene = gideros.class(Sprite)
local scene = Scene.new()
stage:addChild(scene)
2014-10-01 17:49:15