Let's create a Grabber
Updated: May 15, 2021
Grabber's are fun, right, like in a seaside amusement arcade, hurl coins into the grabber and leave disappointed. But a Grabber might be fun in your game, perhaps use it to move items around, or grab an enemy and drop him into a fiery pit of doom.
I created this script for Episode 3 of Cocoa2: 24 Hour Parsley People, but I've simplified it here so that you should be able to easily adapt it for your own games.
Once you are up and running, it should look something like this...
Here's what we'll need to create:
A 2 frame Sprite for the Grabber itself
A Sprite for the Grabber Controls
Some Script for the Player
Script for the Grabber
Script for the sprited that can be Grabbed.
Here's the one I used, we need two frames, Frame 0 shows the grabber closed:
And Frame 1 shows the Grabber Open:
2. Grabber Controls
We're also going to need a Sprite for the Controls of the Grabbing machine, when our player collides with the controls they will then be able to control the grabber...I went for a joystick kinda thing...
3. The Player Script.
I'm assuming you are writing a side-on standard platform game or adventure here, so you probably have a load of script for your player already. What we need to do is use a variable which will determine which mode the player is in, either Normal Mode, or Grabber Mode.
In my example I'm going to use C to store the value of the Mode.
So when C=1 the player is in Normal Mode and he can run and jump as normal, and when C = 2, the player is in Grabber mode, the player no longer moves, but the Grabber does instead.
So my player Script will, at a high-level, look something like:
IF C = 1 ; normal mode
....do all the normal stuff
IF C = 2
...do all the grabber stuff
The next thing I need is a variable to store the different things that the Grabber can do, I've called them Grabber Statuses, and I'm using the variable M.
There are quite a few statuses!
M = 0 ; Resets the Grabber so it has returned to its starting position
M = 10 ; move the Grabber to the left
M = 11 ; move the grabber to the right
M = 12 ; the grabber goes up but has not grabbed anything
M = 13 ; the Grabber goes down but has not grabbed anything
M = 15 ; the Grabber has grabbed something and is going up
M = 19 ; the Grabber has reached the top and has something attached
M = 20 ; the grabber goes left and has something grabbed
M= 21 ; the grabber goes right and and has something grabbed
M = 23 ; the grabber goes down and has something attached
So, in your player script, wrap everything you've done so far inside:
IF C = 1 ; normal mode
Then beneath that we'll add our Grabber script (brace yourself....it's a big one!):
4. The Grabber Script
OK, so that will control the Grabber. But we need to add some code for Grabber too. In effect the Grabber needs to be visible on the screen, but not doing anything, until the player touches the Grabber controls. So, we'll use a different sprite type and initial set the Grabber to this one, then when the player collides with the controls, we'll trigger the Grabber to change its Sprite Type to O (player), thereby allowing the player to control it.
I'm going to use Type 8
Pretty simple, if C = 2, the player is in Grabber mode, so change the grabbers sprite type to 0.
5. The Grabber Controls Script
This is also straightforward, the Grabber Controls Sprite is stationary, it's basically a switch, touch it and stuff happens, the player cant move, instead the player now controls the grabber:
6. Some script for Sprites that can be Grabbed
In Cocoa2:24 Hour Parsley People I made a bunch of sprites 'Grabable' with different consequences, in this simplified version we'll stick to just one.
It will need a variable ( a local one is fine....I'm going to use DIRECTION) that will tell us whether or not the item has been grabbed. Then, based on the current value of M (remember that from the beginning?) we'll know which way the Grabber is going so that the Grabbed Item will move in tandem with it....magic!
So, when DIRECTION = 1, the sprite has been grabbed, in your sprite event add:
...obviously you'll need to tweak this for the correct IMAGE numbers for your game!
Finally, we need to add some code to the player script that will enable the player to return to Normal Mode, by exiting the Grabber Controls, so we'll add this to the bottom of the Player Event, then, and only when the Grabber is nit in action, the player can press the Fire button to release himself from the Grabber Controls:
Finally, add your Grabber to your screen using the SPRITEPOSITIONS editor, remember to initially set your Grabber to the inactive TYPE (in my example above the Grabber is a TYPE 8 until it is made active, at which point it becomes a TYPE 0 (Player event)
And that's it!.
So, the rules of this mechanic are:
Player touches controls
Player becomes inactive
Grabber becomes active
Press left the grabber moves across the crane to the left, to keep it interesting, like a grabber machine it will keep moving left until we press either right or down (even if I release left!). Same with the Right and Down movements
The Grabber needs to hit a grabbable item within a hit box, not to much to the left or right....to keep it challenging!
The grabber and grabbed item must return to the top of the crane to then be able to move right or left
If the grabber reaches the left edge or right edge or is otherwise impeded it will drop whatever it had grabbed!
Player presses down to drop the grabbed item in the desired location.
Player presses Fire when the Grabber is not in motion, to exit the Grabber Controls and return to Normal Mode
Once you've got it up and running, try and adapt it to add more fun stuff...here's some examples from Cocoa 2: 24 Hour Parsley People