Is there a way to sense if a sprite is out of the screen or if a sprite is in ‘sight’ of another sprite? I’m trying to add in a battle mechanic where if the enemy sprite is out of the screen, the battle would end because you escaped the battle. Any type of help would mean a lot!
There is something like this! The sprite utils extension has a block : distance between sprite and other sprite! So it works with that you need an
if (((distance between (sprite) and (sprite2)) (>) (100) then
There you go!
I did this in my Decked Out:
- Fire a 1x1 pixel sprite towards the player’s direction.
- If it collides with the player, keep chasing/fighting him/her
- If it doesn’t collide with the player for the next ex. 5 seconds, you’ve escaped.
Maybe the distance formula?
When I programmed the behaviour of an enemy bat I had them wandering around until the player collided with an EnemyVision sprite which was essentially just a massive red circle set to invisible that had the bat’s X and Y position.
Just write a function that is called in the ‘game update’ that checks the following (you can program this in block or script)
// horizontal check
if (sprite.left > screen.width || sprite.right < 0)
// vertical check
if (sprite.top > screen.height || sprite.bottom < 0)
Also what @DahbixLP mentions, you could use the arcade-sprite-util extension to calculate the distance between the two sprites. This will take into account if the sprite is at an angle to the other sprite (i.e. using Pythagoras Theorem). If you just need to get the x or y distance, then subtracting those values will suffice:
I this example I set the score to be the distance between the two sprites (using the extension from Joey). The ‘sprite’ also says the absolute x/y distance from the second sprite. Absolute just means get the value as a positive number. I also added a function that checks separately if the ‘sprite’ has left the screen horizontally or vertically.
Big fan of sneaking games, thought would be nicer if players can hide behind the walls, try to make a small demo and,
Enemies stay where they are until a player is in sight and stopping chasing player if lost sight.
what is that extension?
This extension is amazing! Could you tell me the name? (Or the link)