0

我在我的程序中创建事件时遇到问题。当我尝试使用以下系统设置运行它时,我收到错误“尝试调用方法'addEventListener(一个零值)”我正在寻找的效果是OtherObject文件向Object文件发送一个信号,事件,告诉特定例如,它已经完成了 OtherObject 文件中的一些任务。任何让我走上正确道路的帮助或指导将不胜感激。

目标文件

Object = {};

ObjectMeta = {__index = Object};

function Object:onTrigger()
   --Event Triggered
end

function Object.new(args)
    Obj = {};
    Obj.sprite = display.newImage("Picture.png");
    Object.someObject = OtherObject.new(args);
    Object.someObject.owner = Object;
    Object:addEventListener("onTriggered", Obj);
    return setmetatable(Obj,ObjectMeta );
end





return Object;

其他对象文件

OtherObject = {};

OtherObjectMeta = {__index = OtherObject};

function OtherObject.new(args)
    Obj = {};
    Obj.sprite = display.newRect(0,0,3,7);
    Obj.ObjectImAttachedTo =nil;
    return setmetatable(Obj,OtherObjectMeta );
end
function OtherObject:doSomething()
    self.ObjectImAttachedTo:dispatchEvent( {name = "triggered"} );
end
return OtherObject;
4

1 回答 1

1
local Object = {}

local ObjectMeta = {__index = Object}

function Object:triggered(event)
   --Event Triggered
end

function Object.new(args)
    local Obj = {}
    Obj.someObject = OtherObject.new(args)
    Obj.someObject.ObjectImAttachedTo = Obj
    setmetatable(Obj, ObjectMeta)
    Runtime:addEventListener("triggered", Obj)
    return Obj
end

return Object
于 2013-05-09T08:00:13.513 回答