Creating proximity triggered falling nasties in MPAGD
Updated: Nov 22, 2020
...well I couldn't think of a better name for them. But these are hazards that fall from the ceiling just as the player is about to walk under them. This is another mechanic that I used in Cocoa and the Time Machine and improved in 24 Hour Parsley People.
They are pretty easy to implement, and in the code below I'll show you how you can adapt them to suit your own games.
First lets take a look at them in action. Here's an example in a demo screen....
So, a couple of things to note here.
1. I only want to trigger the fall when the player is just beneath them
2. The player must be within a specific vertical range of the falling nasty to trigger
In other words, we need to be careful not trigger them if the player walks above the nasty. And in my example I don't want the player on the ground level to trigger the fall in the upstairs level.
Obviously, your use case may need different rules - but the code below should be easily adaptable.
Firstly, lets think about how MPAGD sprites work, and specifically how their co-ordinates work. Each sprite is a 16px by 16px square. And each instance of a Sprite has an X and Y value, this is the x & y coordinate of the top left hand corner of the sprite as it appears on the screen. So if your sprite is in the very top left corner of the screen, it's X would be 0 and its Y would be 0.
Now, the problem is, with a falling nasty, we'll need to know the X and Y of both the nasty AND the player. And our code is going to need to live in one of the Sprite events, it probably makes sense to use one of our EVENTSPRITETYPE's for the falling nasty. So we are going to need a couple of variables that will store the X & Y values of the player. I tend to use F & G for these, purely out of habit...you can use what you like, either way open up your player event and lets add some cod ( I tend to drop it in at the bottom of the Player script):
LET F = X LET G = Y
OK, now we'll be able to use the players X & Y values when we are in the falling nasty script...
In this example I was using IMAGE 12, obviously remove that IF/ENDIF as required, or change it to reference your falling nasty sprite image.
What we are doing here is effectively checking the position of the player relative to the falling nasty - first is the player beneath the falling nasty, and, if so, is it within our given height range ?
In the falling nasties SETTINGA I am actually storing the the X value of the Player minus 18 - in other words 18 pixels to the left of the player
I'm doing a similar thing with variable M, this time I am storing the X value of the players current position PLUS 18 - or 18 pixels top the right of the player
You can change to any value you like, rather than use 18 - depending on how early or late you want the fall to be triggered.
So, as soon as M or SETTINGA match the x value of the falling nasty, we trigger the fall.
NOTE: In this script I am using multiple 'SPRITEDOWNS' to dictate the speed of the fall, you can use as many (or few) as your game requires to make it easier or more difficult to avoid. As an alternative you could replace the SPRITEDOWNS with FALL (thanks to Alessandro Grussu for the suggestion). With FALL the sprite will accelerate as it drops.
What you do within your collision IF statement is up to you, I've kept it simple for the purpose of the tutorial. I've then used the falling nasties SETTINGB parameter to act as a timer (how long it remains on the floor before being removed)...again, you can adapt this however you like, maybe having an explosion sprite, or something else entirely!
It would also be easy to adapt this code, so that, for example, you could trigger from above, perhaps as a fun way to kill an enemy!
Hope this helps!