2012-07-01 3 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) 

그것은 이미지를 표시하지만 터치시 (인쇄 "안녕하세요") 작동하지 않습니다.

답변

2

문제의 몇 :

변경 if event.phase =="began" then

+0

내가, Royi 당신을 사랑합니까해야 function Fish:listen(event)

if phase =="began" thenfunction Fish.listen(event)에. =) –

+1

앗, 인터넷에서 낯선 사람. – royi

관련 문제