Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: 8 Directional Movement GM 7 GML  (Read 930 times)

0 Members and 1 Guest are viewing this topic.
8 Directional Movement GM 7 GML
« on: May 11, 2009, 10:14:47 pm »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
Just looking for feedback:
Code: [Select]
// Movement - 8 directional

if movable = 1
{
    // Upwards 90 Degrees first
    if keyboard_check(vk_up) && !keyboard_check(vk_down) && !keyboard_check(vk_left) && !keyboard_check(vk_right)
    {
        speed = 2;
        sprite_index = cloudup;
        image_speed = .45;
        direction = 90;
    }
   
    // Upwards-Right 45 Degrees
    if keyboard_check(vk_up) && keyboard_check(vk_right) && !keyboard_check(vk_left) && !keyboard_check(vk_down)
    {
        speed = 2;
        sprite_index = cloudright;
        image_speed = .45;
        direction = 45;
    } 
   
    // Upwards-Left 135 Degrees
    if keyboard_check(vk_up) && keyboard_check(vk_left) && !keyboard_check(vk_down) && !keyboard_check(vk_right)
    {
        speed = 2;
        sprite_index = cloudleft;
        image_speed = .45;
        direction = 135;
    }
   
    // Right 0 Degrees
    if keyboard_check(vk_right) && !keyboard_check(vk_left) && !keyboard_check(vk_down) && !keyboard_check(vk_up)
    {
        speed= 2;
        sprite_index = cloudright;
        image_speed = .45;
        direction = 0;
    }
   
    // Left 180 Degrees
    if keyboard_check(vk_left) && !keyboard_check(vk_right) && !keyboard_check(vk_down) && !keyboard_check(vk_up)
    {
        speed= 2;
        sprite_index = cloudleft;
        image_speed = .45;
        direction = 180;
    }

    // Downwards 270 Degrees
    if keyboard_check(vk_down) && !keyboard_check(vk_up) && !keyboard_check(vk_left) && !keyboard_check(vk_right)
    {
        speed = 2;
        sprite_index = clouddown;
        image_speed = .45;
        direction = 270;
    }

    // Downwards-Left 225 Degrees
    if keyboard_check(vk_down) && keyboard_check(vk_left) && !keyboard_check(vk_up) && !keyboard_check(vk_right)
    {
        speed = 2;
        sprite_index = cloudleft;
        image_speed = .45;
        direction = 225;
    }
   
    // Downwards-Right 315 Degrees
    if keyboard_check(vk_down) && keyboard_check(vk_right) && !keyboard_check(vk_left) && !keyboard_check(vk_up)
    {
        speed = 2;
        sprite_index = cloudright;
        image_speed = .45;
        direction = 315;
    } 

}

No stopping implemented yet, but easy to do.
Logged
  • Super Fan Gamers!
Re: 8 Directional Movement GM 7 GML
« Reply #1 on: May 11, 2009, 10:22:52 pm »
  • Oh.
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 579
Seems really redundant.  Can't you just add together the angles of each arrow key?
Logged
Hm.

Mirby

Drifter
Re: 8 Directional Movement GM 7 GML
« Reply #2 on: May 11, 2009, 10:25:39 pm »
  • To bomb or not to bomb...
  • *
  • Reputation: +6/-0
  • Offline Offline
  • Gender: Female
  • Posts: 4162
I did a code like this for enemy characters for seemingly random movement.

Code: [Select]
if direction = 0
{sprite_index = E;}
if direction = 45
{sprite_index = NE;}
if direction = 90
{sprite_index = N;}
if direction = 135
{sprite_index = NW;}
if direction = 180
{sprite_index = W;}
if direction = 225
{sprite_index = SW;}
if direction = 270
{sprite_index = S;}
if direction = 315
{sprite_index = SE;}

Simple, really. However, it's all intertwined with the events. For the object, you'll need to set an alarm for the Create event (and set movement to all directions in the Create event). Then in the Alarm event, run this handy piece of code (and reset the alarm and movement too!). Make sure you have an 8-directional sprite set up, with the names listed here for the sprites. Maybe you could implement this if it's not too simple, TFS. Personally, I'm proud of myself for figuring it out. Yours is nicer, though.
Logged

Mirby Studios | Share & Enjoy now available! (Links above!) | Games I've Beaten
Quote from: Mamoruanime
I like-like it :D
  • Mirby Studios
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.319 seconds with 43 queries.