[英]How to use functions from another class in main.lua?
I am trying to use functions of another class in my main.lua. 我想在main.lua中使用另一个类的函数。 I wrote some code according to my research but it's not working properly.
我根据我的研究编写了一些代码,但它没有正常工作。 Can you help me out?
你能帮我吗? Thanks.
谢谢。
fish.lua code: fish.lua代码:
function class()
local cls = {}
cls.__index = cls
return setmetatable(cls, {__call = function (c, ...)
instance = setmetatable({}, cls)
if cls.__init then
cls.__init(instance, ...)
end
return instance
end})
end
Fish= class()
function Fish:listen(event)
if phase =="began" then
print("hi")
end
end
function Fish:__init(image)
self.image=display.newImage(image,30,30)
self.image: addEventListener("touch",self.listen)
end
main.lua code: main.lua代码:
require "fish"
originalImage="fish.small.red.png"
differentImage="fish.small.blue.png"
local fishImage=Fish(originalImage)
It displays the image but does not work(prints "hi" ) when it is touched. 它显示图像但在触摸时不起作用(打印“hi”)。
A couple of problems: 一些问题:
Change function Fish:listen(event)
to function Fish.listen(event)
改变
function Fish:listen(event)
function Fish.listen(event)
and if phase =="began" then
should be if event.phase =="began" then
if phase =="began" then
应该是if event.phase =="began" then
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.