HELP - Problem adding multiple parts to physics.addBody when using OOP framework.

Hi all,

I’m using dmccuskey’s Ghost VS Monsters OOP as a framework and am having a couple of issues. 

The problem I’m having is that I want to add multiple shapes to an object but am having some issues doing so. I think I understand where the problem lies, but need some help to solve it. 

In Ghost VS Monsters OOP, Objects are handled with the following code:

– Game Object Factory

–====================================================================–

local GameObjectFactory = {}

function GameObjectFactory.create( objType, gameEngine )

    print( "GameObjectFactory.create : " … objType )

    local groundShape = { -240,-18, 240,-18, 240,18, -240,18 }

– GROUND ONE

    if objType == “ground-one” then

        o = display.newImageRect( “assets/game_objects/ground1.png”, 480, 76 )

        o.myName = "ground"

        o.physicsType = "static"

        o.physicsProperties = { density=1.0, bounce=0, friction=0.5, shape=groundShape }

else

        print( “\n\nERROR: Game Objects Factory, unknown object '” … tostring( objType ) … “’\n\n”)

    end

    return o

end

If I wanted to add a more complex shape to the “ground-one” then I’d expect to add something like the following to the above code:

local groundShape2 = { -100,-18, 100,-18, 100,18, -100,18 }

&

o.physicsProperties = { density=1.0, bounce=0, friction=0.5, shape=groundShape },

                                       ** { density=1.0, bounce=0, friction=0.5, shape=groundShape2 }**

This however does not seem to work.

In the game engine, the objects are created by the following code:

function GameEngine:_addDataItems( data, group, params )

    print( “GameEngine:_addDataItems” )

    local params = params or {}

    local isPhysics = params.isPhysics or false

    local o, d

    for _, item in ipairs( data ) do

        – item is one of the entries in our data file

        – most of the creation magic happens in this line

        – game objects are created from level data entries

        o = GameObjectFactory.create( item.name, self )

        – sanity check - if we have something, then process it

        if o then

            – process attributes found in the level data

            if item.reference then

                o:setReferencePoint( DisplayReferenceFactory( item.reference ) )

            end

            – TODO: process special properties and layer the rest

            if item.rotation then o.rotation = item.rotation end

            if item.alpha then o.alpha = item.alpha end

            if item.x then o.x = item.x end

            if item.y then o.y = item.y end

            – add new object to the display group and physics engine

            d = o

            if o.isa ~= nil and o:isa( CoronaBase ) then

                – type is of dmc_object

                d = o.display

            end

            if isPhysics then

                physics.addBody( d, o.physicsType, o.physicsProperties )

            end

            group:insert( d )

            – count enemies being place on screen

            if o.myName == self.game_data.info.enemyName then

                self._enemyCount = self._enemyCount + 1

                o:addEventListener( o.UPDATE_EVENT, self )

            end

        end

    end

end

I think the problem lies with:

if isPhysics then

                physics.addBody( d, o.physicsType, o.physicsProperties )

end

I’m assuming because of the way the code is handling physics.addBody for the objects, it’s not allowing for more complex body constructions. 

I hope that all makes sense. Does anyone know how to solve this?

Any help would be much appreciated. 

Thanks

Issue solved. 

I had actually already added a fix to add complex bodies using this method but was getting Director errors in builds. I assumed it was down to my fix but further digging shows it was a .png file name in upper-case causing the error. 

Issue solved. 

I had actually already added a fix to add complex bodies using this method but was getting Director errors in builds. I assumed it was down to my fix but further digging shows it was a .png file name in upper-case causing the error.