Jump to content

Random enemy spawn within a polygon


Recommended Posts


I'm trying to get a sprite to spawn within a polygon I've drawn in Tiled.

I've imported the polygon like so:

this.bunyipSpawnZone = new Phaser.Polygon(polygonSpawn[0][0],polygonSpawn[0][1],polygonSpawn[1][0],

polygonSpawn is the corresponding Tiled map object. Which is an array where [0] is the x coord and [1] is the y.

this.bunyipSpawnZone returns a Phaser.Polygon with seemingly appropriate x and y coordinates.

Phaser.Polygon_points: Array[6]0: Phaser.Pointtype: 25x: 158y: -6__proto__: Object1: Phaser.Pointtype: 25x: 20y: -852__proto__: Object2: Phaser.Pointtype: 25x: 902y: -852__proto__: Object3: Phaser.Pointtype: 25x: 892y: -996__proto__: Object4: Phaser.Pointtype: 25x: -96y: -978__proto__: Object5: Phaser.Pointtype: 25x: -114y: 0__proto__: Objectlength: 6__proto__: Array[0]

area: -300120

closed: true

flattened: false

points: (...)type: 12


But the area is negative??? Why is that?

And I can't seem fill in the area with graphics.beginFill() to verify that the polygon is in the right place. I tried using .contains for debugging as well, but that's also not giving me a true when I expect it to.

Again the end goal here is to have an enemy spawn somewhere randomly in the polygon. But it seems I can't get a polygon working right.

the github is https://github.com/nodes777. Any help would be appreciated. Thanks!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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