Assigning an object to several groups or behavioural groups

Hi all,

I am having difficulty assigning a particular object to several behavioral groups.

My first example:

I am creating an app with multiple scenes (using storyboard) and with a moving camera in one of the scenes. It is a rule that an object cannot be assigned to more than one group. Therefore, I am using an unorthodox way to assign a display object to the scene.view group for my scene as well as to move only certain objects in that scene with the movecamera function.

  1. Defining objects Coding (not all - main points only):

local storyboard = require(“storyboard”)

local scene = storyboard.newScene()

function scene:createScene(event)

   

     local screenGroup = self.view

     local sky = display.newImage(“sky.png”)

     screenGroup:insert(sky)

     

     local tree = display.newImage(“tree.png”)

     screenGroup:insert(tree)

      

     local man = display.newImage(“man.png”)

     screenGroup:insert(man)

     local forward_button = display.newImage(“button.png”)

     screenGroup:insert(forward_button)

end

  1. Lets say if i have a movecamera function that follows the man forward, when I press the forward button.

local background = display.newgroup()

background : insert (sky)

background : insert (tree)

local function moveCamera()

           if (man.x > 10) then

                   background.x = -man.x+10

           end

end

  1. I cannot assign the sky and tree objects to both the scene.view and background groups. At the same time, I want the forward_button to stay where it is, so I need to call the sky and tree separately. 

I could do this:

local function moveCamera()

           if (man.x > 10) then         // 10 is the initial x distance of man

                   scene.view[1].x = (original sky’s position) - man.x +10

                   scene.view[2].x = (original tree’s position) - man.x +10

           end

end

This works fine assuming the number of objects are small, and no objects in the middle are destroyed in the game.

Is there a way I can create or define a separate sub-group or behavioral category that I can control certain objects within the scene.view group without relying on index?

Thank you! Any help will be greatly appreciated!

Jon

Hi Jon,

This should be possible but have you tried inserting the sky and tree objects into background rather the sceneGroup?

e.g.

function scene:createScene(event)          local screenGroup = self.view local background = display.newGroup()      local sky = display.newImage("sky.png")      background:insert(sky)            local tree = display.newImage("tree.png")      background:insert(tree)      sceneGroup:insert(background)        local man = display.newImage("man.png")      screenGroup:insert(man)      local forward\_button = display.newImage("button.png")      screenGroup:insert(forward\_button) end  

In theory background should now contain sky and tree and be able to move as one group.

Luckily for my projects I have been using Level Director (http://www.retrofitproductions.com/level-director) which makes all this really easy.

Also be aware that if you are using physics, moving a group will not work, you have to move individual objects, again Level Director does this for you and can even wrap objects and specify different speeds for each layer to create parallax scrolling.

That said, the above code should work though.

Hi Jon,

This should be possible but have you tried inserting the sky and tree objects into background rather the sceneGroup?

e.g.

function scene:createScene(event)          local screenGroup = self.view local background = display.newGroup()      local sky = display.newImage("sky.png")      background:insert(sky)            local tree = display.newImage("tree.png")      background:insert(tree)      sceneGroup:insert(background)        local man = display.newImage("man.png")      screenGroup:insert(man)      local forward\_button = display.newImage("button.png")      screenGroup:insert(forward\_button) end  

In theory background should now contain sky and tree and be able to move as one group.

Luckily for my projects I have been using Level Director (http://www.retrofitproductions.com/level-director) which makes all this really easy.

Also be aware that if you are using physics, moving a group will not work, you have to move individual objects, again Level Director does this for you and can even wrap objects and specify different speeds for each layer to create parallax scrolling.

That said, the above code should work though.