Jump to content

Detect when near the world bounds limit


Heppell08
 Share

Recommended Posts

Pretty simple i think but i kind dont know where to start with it. I know i have my world width and world height.

I know i have an idea in my head how to do this but i just cant get it out my head and working.

 

My idea was to check the player x position in comparison to the world width and height but its the calculations leading up to that bit where im a bit lost.

 

do i have 

 

player.x > this.world.widh - (some amount) || player.x this.world.width + (some amount)

 

Its so i can create an ingame warning about the game edge being a killer and kills the player.

 

Any help/ideas are great thanks.

 

Sidenote: world width = 800 world height 600.

 

Sidenote2:this.world.setBounds(0,0,1600,1200);

 

not 800/600 thats my screen size...

Link to comment
Share on other sites

I'd do this with invisible sprites which trigger the warning:

var warningTriggers = game.add.group();warningTriggers.enableBody = true;warningTriggers.physicsBodyType = Phaser.Physics.ARCADE;// sets how 'deep' the trigger is from the sides of the worldvar triggerSize = 200;var topTrigger = game.add.sprite(0, 0, null, 0, warningTriggers);topTrigger.body.setSize(game.world.width, triggerSize)var bottomTrigger = game.add.sprite(0, game.world.height - triggerSize, null, 0, warningTriggers);bottomTrigger.body.setSize(game.world.width, triggerSize);var leftTrigger = game.add.sprite(0, 0, null, 0, warningTriggers);leftTrigger.body.setSize(triggerSize, game.world.height);var rightTrigger = game.add.sprite(game.world.width - triggerSize, 0, null, 0, warningTriggers);rightTrigger.body.setSize(triggerSize, game.world.height);function update() {  game.physics.arcade.overlap(player, warningTriggers, function() {    // do warning here  });}
Link to comment
Share on other sites

I guess if you want a mathematical approach:

var triggerDepth = 200;function update() {  if (player.x < triggerDepth || player.x > game.world.width - triggerDepth || player.y < triggerDepth || player.y > game.world.height - triggerDepth ) {    // do warning here  }}

You could also do this with the Rectangle.containsRect function, and use the body as the second comparison rectangle:

var triggerDepth = 200;var triggerRect = new Phaser.Rectangle(triggerDepth, triggerDepth, game.world.width - (triggerDepth * 2), game.world.height - (triggerDepth * 2))function update() {  if (!Phaser.Rectangle.containsRect(player.body, triggerRect)) {    // do warning here  }}
Link to comment
Share on other sites

Invisible sprites will work, but I think you are on the right track and a simple if statement should suffice.

if (player.x > (world.width - warningWidth) ||    player.x < warningWidth ||    player.y > (world.height - warningWidth) ||    player.y < warningWidth){  warnPlayer();}
Link to comment
Share on other sites

Scrap this i eventually woke up and realised to do what i would like was EXTREMELY simple:

 

if(player.x > 1300 && player.x < 1599 || player.x >0 && player.x < 200)    {      var edgeWarn = this.add.bitmapText(200,100,'tron','Edge Warning', 72);      edgeWarn.fixedToCamera = true;    }    if(player.y > 1000 && player.y < 1199 || player.y >0 && player.y < 200)    {      var edgeWarn = this.add.bitmapText(200,100,'tron','Edge Warning', 72);      edgeWarn.fixedToCamera = true;    }
 
just coding a buffer zone on my world bounds and got text to tell. Sorry for wasting forum space though.
Link to comment
Share on other sites

yeah i added in @lewsters approach and yeah my net lagged but i quickly wrote it on testing but have removed the ability to spam texts haha. looks like this now:

if(edgeWarn.visible === true)    {      edgeWarn.alpha -= 0.1;    }    if(edgeWarn.alpha < 0.01)    {      edgeWarn.destroy();      this.createEdgeWarn();    }    if (!Phaser.Rectangle.containsRect(player.body, triggerRect))     {      edgeWarn.visible = true;    }

Got 1 text created but visible false then have this running to alpha out then destroy then recreate and set to visible false. just for a nice faded effect ingame :D

Link to comment
Share on other sites

I would also remove those magic numbers, what happens when you want to change the world size. You will have to go through and change them all into new magic numbers.

 

Yeah they have been removed also, i looked at the rect approach but my head aint been with it recently haha. Glad you guys got that in for me because i knew there was a much simpler approach to it.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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