makotok/Hanappe

Add a condition around setAttrLink with MOAIColor

Closed this issue · 4 comments

In DisplayObject:setParent, if you parent to a Box2D body you get an error, because it doesn't have MOAIColor.

It doesn't seem like there is a function in Moai to test if an attribute exists without raising errors.

I could not understand the problem.
You can set the MOAIBox2DBody to "DisplayObject:setParent".
It inherits the Transform, the other properties are ignored.

Do you have a sample?

I thought that a condition could solve this, but Moai spouts errors regardless of what condition I try to put.
The code still works, it just shows Moai errors.

module(..., package.seeall)

local layer = flower.Layer()
local world = MOAIBox2DWorld.new()
local image = flower.Image("cathead.png")
local body = world:addBody(MOAIBox2DBody.KINEMATIC, x, y)

function onCreate(e)
  scene:addChild(layer)

  layer:setBox2DWorld(world)

  image:setParent(body)
end
----------------------------------------------------------------
No such attribute
stack traceback:
        [C] in function 'setAttrLink'
        .\flower.lua:1600 in function 'setParent'
        .\scene.lua:13 in function 'callback'
        .\flower.lua:892 in function 'call'
        .\flower.lua:1008 in function 'dispatchEvent'
        .\flower.lua:1916 in function 'init'
        .\flower.lua:250 in function <.\flower.lua:239>
        (tail call) ?
        .\flower.lua:778 in function <.\flower.lua:777>
        (tail call) ?
        .\flower.lua:1293 in function <.\flower.lua:1275>
        (tail call) ?
        (tail call) ?
        main.lua:5 in main chunk

Thank you. I have some understanding.
In my environment seems to be no problem.
However, there is an implementation that is output MOAILog.

It is also similar code setParent: MOAIProp original.
For now, you can be avoided in the following code.

module(..., package.seeall)

function onCreate(e)
    layer = flower.Layer()
    layer:setScene(scene)

    image1 = flower.Image("cathead.png")
    image1:setLayer(layer)
    image1:setPos(0, 0)

    world = MOAIBox2DWorld.new()
    world:setGravity(0, 10)
    world:setUnitsToMeters(1/30)

    body = world:addBody(MOAIBox2DBody.DYNAMIC)
    body:addRect(0, 0, 128, 128)
    body:resetMassData()

    --image1:setParent(body)
    image1:setAttrLink(MOAITransform.INHERIT_TRANSFORM, body, MOAITransform.TRANSFORM_TRAIT)

    world:start()
end

Maybe they can not check the setParent.
For any object, I correspond with setAttrLink.