Jump to content

What is the correct way to create map with interactive objects?


sendmenas
 Share

Recommended Posts


Hi,

I have created a Tiled map with object layer in it.

this.objectLayer = this.map.createFromObjects('Object Layer 1', 1, {key: 'brick'} );

This creates an array of sprites, as expected. But when I check these sprites, I see that body is null. I want to add collider for the objects and I believe due to the reason that they have no body, this does not work.

What I aim to do is to create walls, that after hit will shrink by 25% based on bullet direction. I successfully managed to do it on one brick without using Tiled map:

// Created brick
this.bricks = this.physics.add.staticGroup();
this.bricks.create(400, 300, 'brick');

// Added callback on hit
this.physics.add.overlap(this.bricks, this.bullet, this.bulletBrickHit, null, this);

// Changing brick size on hit
bulletBrickHit(bullet, brick) {	    
    switch(bullet.direction) {
	    case 'left':
	        brick.setCrop(0, 0, brick.body.width - 25, brick.body.height);
	        brick.setSize(brick.body.width - 25, brick.body.height);
	        break;
    }
}

But I was thinking of using Tiled for map creation as it would be much faster to do it using it.

So my questions are: Can I use Tiled objects for this? Or should I add all bricks manually, as Tiled is not for that? Maybe there is some other way how to do it "right"? Or maybe I am just missing something?

I would really appreciate your help.

Link to comment
Share on other sites

bulletBrickHit(bullet, brick) {	    
    switch(bullet.direction) {
	    case 'left':
	        brick.setCrop(0, 0, brick.body.width - 25, brick.body.height);
	        brick.setSize(brick.body.width - 25, brick.body.height);
brick.refresh();
	        break;
    }
}

for create static layer see example:

https://labs.phaser.io/edit.html?src=src\game objects\tilemap\static\create from objects.js

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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