1

作成したオブジェクトに属性を追加しようとしています。ここでは鳥の表示オブジェクトを作成しましたが、これらの鳥にtypeOfBirdのような特定の属性を追加してから、bird.typeOfBirdのような属性に到達したいと思います。どうやってやるの?

 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
4

1 に答える 1

2

鳥のオブジェクトはすでに単純なluaテーブルのように見えるので、通常どおり値を取得して設定できます。したがって、たとえば、次のような行を追加できます。

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

self.typeOfBird = "parrot"

あなたのbird:touch機能に。

または

bird.typeOfBird = "gull"

あなたのnew関数で。

于 2012-07-03T07:01:56.020 に答える