Jump to content

Spawn X/Y Question


Heppell08
 Share

Recommended Posts

Was wondering if there was a way to stop my collectible items in-game from being spawned in certain places.

I want a random spread every time per level and I'm using randomX and randomY.

What i'm looking for is a way to have it so that it doesn't spawn inside the floor of a collide tiles and also so it isn't outside the bounds of where the player can go.

Not sure if this is even possible at all but would be nice to get any tips if possible.

Here my group code so far:

 

setGravs: function()        {            collectGroup = this.add.group();            for(var i = 0; i < collectables; i++)            {                gravitoken = collectGroup.create(this.world.randomX, this.world.randomY, 'gravitoke');                gravitoken.health = 1;            }        },

Its super basic because it has other stuff running in the back of whats really going on but i think the spawns could definitely be better.

 

Link to comment
Share on other sites

First off: cool, didn't know about world.randomX/randomY.  :)

 

Second off: I think this depends on what else is going in your game. If I were doing something like this and I were using Tiled to make a tilemap I'd define an object layer in Tiled that defined rectangles of legal places for collectibles to spawn. In other words, be very explicit and do it manually when designing your levels.

 

Failing that, I believe you can check if the collectible would collide with something by checking against the physics manager: http://docs.phaser.io/Phaser.Physics.Arcade.html#toc35 If it does, pick another place. But I'd go the manual route first.

Link to comment
Share on other sites

The manual route was questionable and I had thought about it but I figure to make it a bit more random I wanted it to be different each time. I did think about using TILED as an option for a layer. My idea was:

Make new layer and fill the whole thing with the collectible, then in some magical way I will probably never know how, remove the collectables and have a random amount of them still there. I dunno I haven't a clue really. Might be that I need to look into some crazy algo or need to write my own.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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