2012-07-03 4 views
1

내가 만든 객체에 attirubute를 추가하려고합니다. 새 객체를 만들었지 만 typeOfBird와 같은 새 attiribute를 추가하고 bird.typeOfBird와 같은 attiributes에 연결하려고합니다. 내가 어떻게 할 수 있니? 그것은 새 개체처럼 보이는루아에서 객체에 속성을 추가하려면 어떻게해야합니까?

module(...,package.seeall) 

    function new(params) 
    local bird=display.newImage(params.img,params.x,params.y) 

    function bird:touch(event) 
local t = event.target 
local phase = event.phase 

if "began" == phase then 
    -- Make target the top-most object 
    local parent = t.parent 
    parent:insert(t) 
    display.getCurrentStage():setFocus(t) 

    t.isFocus = true 
elseif t.isFocus then 
    if "moved" == phase then 
     t.x = event.x 
     t.y = event.y 
    elseif "ended" == phase or "cancelled" == phase then 
     display.getCurrentStage():setFocus(nil) 
     t.isFocus = false 
    end 
    end 

return true 
    end 

답변

2

는 그래서 당신은 그냥 얻을 정상적으로 값을 설정할 수 있습니다, 이미 간단한 루아 테이블입니다. 예를 들어 당신이 추가 할 수 있도록 라인이 좋아 : 당신의 bird:touch 기능

if self.typeOfBird == "gull" then ... end 

self.typeOfBird = "parrot" 

합니다.

아니면 new 기능에

bird.typeOfBird = "gull" 

.

관련 문제