Shooting Flashcards
Having the weapon follow the player and aim using mouse
(step event)
x = obj_player.x; y = obj_player.y;
image_angle = point_direction(x, y, mouse_x, mouse_y);
use +/- # after obj_player.x/y to allign where the weapon will be placed
place this code in a Begin Step Event to have a slight delay in the weapon trailing the player; can give a feeling of weightiness
adjusting the origin point can help with the way the object follows the mouse cursor
Firing the weapon
In the weapon’s (create event)
firingdelay = 0; recoil = 0;
In the weapon’s (step event)
firingdelay = firingdelay - 1; recoil = max(0, recoil - 1);
if (mouse_check_button(mb_left)) && (firingdelay < 0)
{
recoil = 1.5;
firingdelay = 7;
with (instance_create_layer(x, y, “Bullets”, obj_bullet))
{
speed = 5;
direction = other.image_angle + random_range(-3, 3);
image_angle = direction;
}
}
x = x - lengthdir_x(recoil, image_angle); y = y - lengthdir_y(recoil, image_angle);
a “with” statement is used to apply the code within to a different designated object: (x, y, instance layer name, object name)
using “other.[]” within a “with” statement will refer to the original object you were coding (in this case the weapon)
setting the recoil’s max range as max(0, recoil - 1); is so that if the value ever becomes 0 - 1 = -1, it will instead take the 0, ensuring the recoil value never drops below 0
x/y = x/y - lengthdir_x/y(recoil, image_angle); is used to actually set the recoil movement by moving the weapon by the number of pixels designated in the “recoil = #” statement in the x and y directions
Bullet Animation
To utilize a muzzle flash frame, use this code in the bullet’s (animation end event):
image_speed = 0; image_index = 1;
To have the bullet be destroyed on collision with another object, use this code in the bullet’s (post-draw event):
if (place_meeting(x, y, obj_wall)) instance_destroy();
if using muzzle flash, adjust the origin point of the bullet to better line it up with the front of the weapon
If weapon sprite should be flipping at certain angles
In weapon’s (step event):
if (image_angle > 90) && (image_angle < 270) { image_yscale = -1; } else { image_yscale = 1; }
Restarting
(Add event -> Key Pressed -> Letters -> R)
game_restart();
good for testing
Bullet collision with Enemies
In obj_enemy:
(create event)
hp = [#];
flash = 0;
*flash is the # of frames an enemy will flash white when hit
In obj_bullet
(Add event -> collision -> obj_enemy)
with (other) { hp--; flash = 3; }
instance_destroy():
“with (other)” in a collision event returns the ID of the object hit. Using “obj_enemy” would apply to EVERY enemy, while “other” retreives only the ID of the specific enemy hit.
Adding damage flash
Create shader
within .fsh, under “ gl_FragColor = v_vColour * texture2D( gm_BaseTexture, v_vTexcoord );”:
gl_FragColor = vec4(1.0, 1.0, 1.0, gl_FragColor.a);
*this sets R,G, & B to max (255), which makes a white pixel. gl_FragColor.a sets the alpha (the transparency) the the value already set in the previous lines.
within obj_enemy:
(draw event)
draw_self();
if (flash>0) { flash--; shader_set(shdr_White); draw_self(); shader_reset(); }
- draw_self(); is necessary because a draw event revokes all automatic process, so the all draw actions must be input manually.
- we earlier set the “collsion obj_enemy” event within obj_bullet to flash = 3; which allows this “if (flash>0)” statement to work upon bullet collision. flash–; will subtract 1 from the flash value each frame until reaching 0, meaning the enemy will be white for 3 frames.
Enemy Death
In obj_enemy
(begin step event)
if (hp <= 0) { instance_create_layer(x, y, "Enemies", obj_dead); instance_destroy(); }
In obj_dead
(animation end event)
image_speed = 0; image_index = #;
(post-draw event)
if (image_index = #) instance_destroy();