Top Down Adventure Style Movement and Attacks

Heres some very early GML code for movement and attacks in a top down adventure game.  I suppose that I could have used some sort of constructor function for assigning attributes to obj_sword, but thats not always easy to read and can get very messy if you change the function to add paramaters so, as doing things the less professional way only required me writing a few lines 4 times, I decided to use poor, but easy to read code (I may add a constructor once I’m sure the script is the way I like it).

if keyboard_check_pressed(vk_space) and not instance_exists(obj_sword){
var temp_id, temp_x, temp_y;
switch face{
case ‘left’:
temp_x = 0
temp_y = 8
temp_id = instance_create(x+temp_x,y+temp_y,obj_sword)
temp_id.count = 0
temp_id.count_max = -120
temp_id.depth = depth + 1
case ‘down’:
temp_x = 8
temp_y = 28
temp_id = instance_create(x+temp_x,y+temp_y,obj_sword)
temp_id.count = 90
temp_id.count_max = -30
temp_id.depth = depth – 1
case ‘right’:
temp_x = 30
temp_y = 20
temp_id = instance_create(x+temp_x,y+temp_y,obj_sword)
temp_id.count = 180
temp_id.count_max = 60
temp_id.depth = depth – 1
case ‘up’:
temp_x = 28
temp_y = -4
temp_id = instance_create(x+temp_x,y+temp_y,obj_sword)
temp_id.count = 270
temp_id.count_max = 150
temp_id.depth = depth + 1
temp_id.image_angle = temp_id.count
temp_id.x_offset = temp_x
temp_id.y_offset = temp_y
temp_id.count_speed = -argument0

To control the animation for obj_sword, I used the following code in my step event…

x = obj_player.x + x_offset
y = obj_player.y + y_offset
count += count_speed
image_angle = count
if count < count_max then instance_destroy()

So far, this solution seems robust enough to handle any situation without hickup, but will likely require an update later on if I decide to add more sword type weapons, but for now it works great!


Leave a Reply

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

You are commenting using your 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