Jump to content

createFromObjects, objects sizes


olgzich
 Share

Recommended Posts

I use the createFromObjects function to create sprites. And everything works well. But, when I change the size of the object in the json file, the image size changes, but the size of the sprite is not. The size of the sprite remains equal to the size of the original picture. And my game object is in the air.
How can I fix this?

And one more question. How can I get my properties that I added to the object?

My code:

var map = this.make.tilemap({ key: 'map' });   
var tiles = map.addTilesetImage('layer1', 'tiles');

layer = map.createDynamicLayer(0, tiles, 0, 0);

var mushroomsArray = map.createFromObjects('object layer', 5, { key: 'benefits' });
    
var mushroomsGroup = this.physics.add.group();
    
for (var i = 0; i < mushroomsArray.length; i++)
{       
    mushroomsGroup.add(mushroomsArray[i]);
    mushroomsArray[i].body.collideWorldBounds=true;    
}

 

Link to comment
Share on other sites

4 hours ago, samme said:

Explain?

For my map, I want to use a tileset with many different objects of different sizes.
For example, it can have a large mushroom and a small nut.
At this point, I have an image of 200 * 200 pixels and 4 objects on it.

When I don’t change the size of the object all is well.

mushroomBig.png.cc27e4be976e6465c01b5de499acf4b8.png

The mushroom is located at the top. And it begins to fall down to the "floor". The boundaries of the image are equal to the size of the sprite.

But, if I reduce the size of an object, the image is reduced, but not the sprite boundaries

mushroomSmall.png.88b1b8170eca7652b71120faa5dbccdf.png

The mushroom falls, but does not reach the "floor".

My .json file:

...
"objects":[
                {
                 "gid":5,
                 "height":66,
                 "id":1,
                 "name":"",
                 "properties":
                    {
                     "alpha":"1"
                    },
                 "propertytypes":
                    {
                     "alpha":"string"
                    },
                 "rotation":0,
                 "type":"",
                 "visible":true,
                 "width":70,
                 "x":211.62610985568,
                 "y":98.504325317887
                }
...
"tilesets":[
        {
         "columns":2,
         "firstgid":5,
         "image":"benefits.png",
         "imageheight":200,
         "imagewidth":200,
         "margin":0,
         "name":"benefits",
         "spacing":0,
         "tilecount":4,
         "tileheight":100,
         "tilewidth":100
        }]

From debugging data( console.log( mushroomsArray ) ) :

_scaleX: 0.7
_scaleY: 0.66
but:
height: 100
width: 100
Link to comment
Share on other sites

  • 2 weeks later...

Did so:

objectsArray[i].width = objectsArray[i].width * objectsArray[i]._scaleX;
objectsArray[i].height = objectsArray[i].height * objectsArray[i]._scaleY;
      
objectsArray[i].setOrigin(0);

objectsArray[i].x = objectsArray[i].x - (objectsArray[i].width/2);
objectsArray[i].y = objectsArray[i].y - (objectsArray[i].height/2);

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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