Jump to content

Help with logic for stealth game


heisenthurg
 Share

Recommended Posts

I am building a simple 2D platform stealth game, with the idea being that the player will move through the level and avoid being spotted by enemies. I have created a map with "shadow" objects, the idea being that when the player overlaps with the shadow object, they become invisible (not literally, just the enemies wont detect them). 

 

I am having trouble figuring out the best approach to this. I have created a global variable "hidden = false", and was going to add a callback function on overlap between player and shadow to set this to "true". The issue is that as the overlap is in the update function, the console shows this variable being made "true" many times a second, instead of once. How can I make it so this variable only changes one time to true, and further to this, changes back to false once the player is not overlapping the shadow?

 

Sorry, I haven't provided code as I am still trying to understand the best approach for this!

 

Thanks :)

Link to comment
Share on other sites

I wouldn't worry about setting the variable multiple times. Or, maybe I should ask: why are you worried about setting that value multiple times? update is called every frame, so that's how often it'll check for overlap.

 

If you're using arcade physics the "overlap" method returns a boolean if there was overlap. It'll change to false when the shadow objects and the player are no longer overlapping.

Link to comment
Share on other sites

I just assumed this was an issue, as I saw the console.log showing this value was constantly being set, it didn't seem the correct/most efficient way for it to be done. I hadn't considered this was actually normal behaviour for the update function! 

 

Thanks

Link to comment
Share on other sites

If they become visible but aren't detected, how do they get detected then? Is it the shadow of the enemy's back or the front? I am trying to understand the gameplay mechanic behind the detection system.

Link to comment
Share on other sites

The shadows are not shadows of the player. Imagine the shadows as just darker areas on the background of the map (at the moment the are just black squares as I'm still designing the map). So it looks as if the character is standing in a "shadow" of a building or whatever. These shadows are separate objects, so the player can overlap them, and when this happens the variable is set to hidden, so I can incorporate that in an if statement so enemies can't see them.

 

For detection, I am using a simple idea of drawing an invisible line from the center of the player, extending a certain distance, say 200px, in front and behind them. If this line overlaps with an enemy sprite, they are "detected" as they are within the distance of view of the enemy. So if the player is not "hidden" and an overlap occurs between the player's line and the enemy, they are detected and lose a life. However, if they are "hidden", nothing will happen, the enemy will pass by the player, and they can move on to the next area.

Link to comment
Share on other sites

Perhaps you can just check if the player is hidden during your `detection` overlap. It depends on whether your "shadows" are tiles or Sprites. 

 

For tiles, use getTileWorldXY and check if the tile at the player's coordinates is a shadow (either through it's index or through it's properties object).

 

For Sprites or objects, use: Phaser.Physics.Arcade.intersects which is self-explanatory.

 

Hope this helps! =)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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