GameMaker Studio: Player Attack in a 2D platformer

 

GameMaker-Studio-Logo

In 2D platformers, players should be able to take care of enemies with more than just jumping on them. Today I will show you a method for a player attack. It creates a projectile that flies in front and behind the player and damages foes it hits.

Sprites (This tutorial assumes your sprites are basic coloured squares and your game has functional platform collision and player/enemy movement )

spr_player 32×32

spr_attack 32×32

spr_enemy 32×32

Objects

obj_player

Create Event

canfire = true

Alarm 0 Event

canfire = true

Press A Key Event

if canfire = true

{
if (keyboard_check (vk_left) = false) && (keyboard_check (vk_right) = false)
{
action_create_object_motion (obj_attack, obj_player.x,obj_player.y,10,180)

action_create_object_motion (obj_attack, obj_player.x,obj_player.y,10,360)
}
if keyboard_check (vk_left)
{
action_create_object_motion (obj_attack, obj_player.x,obj_player.y,10,360)

action_create_object_motion (obj_attack, obj_player.x,obj_player.y,10,180)
}
if keyboard_check (vk_right)
{
action_create_object_motion (obj_attack, obj_player.x,obj_player.y,10,180)

action_create_object_motion (obj_attack, obj_player.x,obj_player.y,10,360)
}
canfire = false
alarm[0] = 0.5 * room_speed
}

obj_attack

Collision Event with obj_wall

instance_destroy()

Collision Event with obj_enemy

instance_destroy()

obj_enemy

Create Event

global.Ehp = 10
Ehurt = false

Alarm 0

Ehurt = false

Step Event

if Ehurt = true
{
image_alpha = 0.5
}
else
{
image_alpha = 1
}

if global.Ehp <=0
{
instance_destroy()
}

Collision Event with obj_attack

if !Ehurt
{
global.Ehp-=5
Ehurt = true
alarm[0] = 0.3 * room_speed
}

When done, the player’s attack should produce moving projectiles. These projectiles disappear when hitting walls, are not produced every time the player mashes the A key, do damage to foes when they are hit by them, enemies flash invincibility frames when hit by the attack and disappear if they take enough damage.

I hope this tutorial has helped you in your project’s development.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s