What we'll do is have a player1 (who's a purple block). Player1 gets all collisions, keydown events, etc.
We have another player that i like to call VISUAL. Visual is a child of player1, so he goes wherever player one goes.
And VISUAL contains all the animations. Now we can simply use animation commands in player1 as normal, only instead of 'event actor' we say 'visual'.
However, i prefer to use a draw actor for visual that sets his animation based on whatever player1 is doing. This draw actor script can get complex, but at the same time it is simpler because ALL of the animation commands are in ONE script editor.
This makes it sooooo much easier to change things and hunt down errors.
But first, we need player1 to have good collisions!
Here is the script involved in my
collision demo: its not really that complicated. It consists of four collisions, two draw actor events, three keydowns, and one keyup.
The first things to do for player 1 are to add variables to him. Do NOT use solid values in your events, and i'll show you why.
On player1>create actor i set a number of variables (global integers) for player1.
- Code: Select all
canjump=0;
jump=16;
speed=4;
gravity=1;
canright=1;
canleft=1;
Now on player>keydown>keyright we do this:(Repeat YES)
- Code: Select all
x+=speed*canright;
canleft=1;
and on player>keydown>keyleft we do the opposite:
- Code: Select all
x-=speed*canleft;
canright=1;
Now on player1>
collision>left side of block: (Repeat YES)
- Code: Select all
int j=yvelocity;
PhysicalResponse(MOVE_EVENT_ACTOR_ONLY, USE_CALCULATED_MASS, 1, 1, 0, 1);
yvelocity=j;
Now we used speed for a reason! what happens when you hold down the B button? Mario RUNS! so all we have to do to change his speed is to change the speed variable when the run key is held. We can also easily adjust his speed througout the game, for instance if he gets wings or is underwater.
Now for player1 gravity, its also very simple:
player1>draw actor:
- Code: Select all
yvelocity+=gravity;
And on keydown (jumpkey):(Repeat NO)
- Code: Select all
yvelocity-=jump*canjump;
canjump=0;
Now player 1 will take off, and canjump becoming 0 happens AFTER his yvelocity becomes -. This prevents him from moonwalking. On player 1>
collision with top side of block, or ground: (Repeat YES)
- Code: Select all
if (yvelocity>0){
PhysicalResponse(MOVE_EVENT_ACTOR_ONLY, USE_CALCULATED_MASS, 1, 1, 0, 1);
canjump=1;
}
This is good because it means player 1 will only have a response with the top of a block if he is moving downward. When moving upward nothing happens with the top of the block.
So notice too, we can easily change his jump amount whenever we want; Like when he gets boots or something. We can also change his gravity easily too, once again, in case he is underwater or in space.
Player 1>draw actor
- Code: Select all
if (yvelocity<14){yvelocity+=gravity;} //limiting gravity helps avoid errors.
One final
collision and we've got him moving pretty well;
player1>
collision>bottom side of block (repeat NO)
- Code: Select all
if (canjump==0){
yvelocity-=yvelocity; }
Once again we've avoided using physical response; this is important because once again, we don't want him to stick to the block, and if he bounces off the block, the bounce can push him thru other objects, which is bad. We simply make him fall when hitting a block with his head.