Jump to content

Object Layer problems


owen
 Share

Recommended Posts

Hi

I've added 4 objects (tiles) to the Object Layer in my Tiled map which are to be sprites in my game. I have checked that my object layer name matches my code and yes the sprites appear on the game --- but they dont' do anything yet.

 

Now I want to apply some physics to those objects, for which I am first going to have to create the sprites from these objects.

 

I'm doing this using createFromObjects as follows:

 
    map.createFromObjects('Object Layer 1', RED_APPLE, 'redApple', 0, true, false, redApples);    map.createFromObjects('Object Layer 1', GREEN_APPLE, 'greenApple', 0, true, false, greenApples);
Does that look right to you?  I am expecting this to have created 4 sprites across 2 groups (redApples and greenApples).   And when I run the game, yes, I'm seeing the expected sprites.  But I cannot figure how to take it further and apply my physics eg. gravity to these groups.
 
What is the best/easiest way of applying gravity and bounce to both groups?  Can I apply it somehow to a whole group at a time?  If so, how do I first even refer to the groups?
 
Thanks,
Owen
Link to comment
Share on other sites

I think I have solved this by using forEach on the groups, as below.

 

 map.createFromObjects('Object Layer 1', RED_APPLE, 'redApple', 0, true, false, redApples);    map.createFromObjects('Object Layer 1', GREEN_APPLE, 'greenApple', 0, true, false, greenApples);    // apply physics to object layer    redApples.forEach(function (apple) {        game.physics.arcade.enable(apple);        apple.body.bounce.y = RED_APPLE_BOUNCE;        apple.body.gravity.y = RED_APPLE_GRAVITY;        apple.body.collideWorldBounds = true;    }, this, false);    greenApples.forEach(function (apple) {        game.physics.arcade.enable(apple);        apple.body.bounce.y = GREEN_APPLE_BOUNCE;        apple.body.gravity.y = GREEN_APPLE_GRAVITY;        apple.body.collideWorldBounds = true;    }, this, false);     
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...