2012-07-01 30 views
0

我想在我的main.lua中使用另一個類的函數。根據我的研究,我編寫了一些代碼,但無法正常工作。你能幫我嗎?謝謝。如何使用main.lua中的其他類的函數?

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代碼:

require "fish" 

    originalImage="fish.small.red.png" 
    differentImage="fish.small.blue.png" 

    local fishImage=Fish(originalImage) 

它顯示的圖像,但是當它碰到不工作(打印 「HI」)。

回答

2

兩個問題:

變化function Fish:listen(event)function Fish.listen(event)

if phase =="began" thenif event.phase =="began" then

+0

我不愛你,Royi。 =) –

+1

Aww和我你,陌生人在互聯網上。 – royi

相關問題