faboon

Is there a better way to get an object from a tile map?

Recommended Posts

This is how I'm getting an object (Ball) from a tile map and finding a reference of it:

// Game's create
map.createFromObjects("objects", 3, "ball", 0, true, false, undefined, Ball);

this.game.world.forEach(element => {
    if(element instanceof Ball) {
        this.ball = element;
    }
});

// Ball's contructor
this.game.add.existing(this);

I've saw the examples and they are getting objects from adding them to a group.

I only have one Ball and that will be the player so I thought putting it in a group was a bad idea?

This way I have a class that directly controls the Ball.

Is there a better way than doing this?

Share this post


Link to post
Share on other sites
map.createFromObjects("objects", 3, "ball", 0, true, false, undefined, Ball);

this.ball = this.game.world.getTop();

I would remove the game.add.existing in the constructor, because createFromObjects is already inserting it in game.world.

Edited by samme
getTop()

Share this post


Link to post
Share on other sites
23 hours ago, samme said:

map.createFromObjects("objects", 3, "ball", 0, true, false, undefined, Ball)
this.ball = this.game.world.getLast();

I would remove the game.add.existing in the constructor, because createFromObjects is already inserting it in game.world.

Thanks. I can't find this.game.world.getLast(). Is that a custom function I should make up?

Share this post


Link to post
Share on other sites
On 27/02/2018 at 5:09 PM, samme said:

Oops, that should be getTop.

Cheers.

I'm using Typescript and getTop returns any. How can I cast that to the ball script? this.game.world.getTop as Ball isn't working for me.

Edit:

I was a fool. It should be this.game.world.getTop()

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.