• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Creating object instance at direction I'm facing

chefdez

Member
Hello everyone,

What I'm trying to do here is to create an attack sprite in front of my character depending on the direction he's facing. The game is top down and I can move with WASD, I have sprites/objects for attacking in these directions.
I was trying something along these lines,

if mouse_check_button_pressed(mb_left) && sprite_run_left or sprite_idle_left {
attacking=true;
image_speed = 1;
image_index = 0;
instance_create(x,y-10,object_slash_left);

}

Obviously this is way off and I'm sure most of you are have your head tilted like whaaaaaaaaaat? I'm very new so if you would be so kind and help that'd be great.

I was wondering if there was a way to resolve this using a case statement? Like if I'm facing left then I will attack left, and If I'm facing right I will attack right.

Thanks :)

PS: I need to guidance on making the best movement on top down thats 8 directional, I can only move 4 right now, any tips?
 
T

The5thElement

Guest
As for the melee combat check out this video. It's for a side view type game but should be easy
to convert to what you need.

As for the 8 directional movement try something like this. This is bare bones so you can adjust
it for your needs.
Code:
//This system uses Q,W,E,A,D,Z,X,C as the
//movement input.

if(keyboard_check(ord('A')){
     x -= 5;
} else if(keyboard_check(ord('Q')){
     x -= 5;
     y -= 5;
} else if(keyboard_check(ord('W')){
     y -= 5;
} else if(keyboard_check(ord('E')){
     x += 5;
     y -= 5;
} else if(keyboard_check(ord('D')){
     x += 5;
} else if(keyboard_check(ord('C')){
     x += 5;
     y += 5;
} else if(keyboard_check(ord('X')){
     y += 5;
} else if(keyboard_check(ord('Z')){
     x -= 5;
     y += 5;
}
 
H

Hossein

Guest
Hello, direction is a big problem for everyone, this is why i use math to write a code for finding direction, everyone can use this code as a function with, this function input is "dx" and "dy" variables and output is "d" mean the direction number, dx is different between 2 objects X coordinate, and dy is different of Y coordinate between 2 objects Y coordinates, and "di" and "dst" are objects, i write a hand function because people can make their own changes inside it if they want, this is version 1 of this object and i dont make it optimize and tidy enough, but it is working very good, you can use it, but you need to read my instruction carefully, because you may not understand the code easy.


// in the name of god
// write the bellow code in your step event
if di.x > dst.x
{
dx = di.x - dst.x;
} else { dx = dst.x - di.x; }
if di.y > dst.y
{ dy = di.y - dst.y; } else {
dy = dst.y - di.y; }

if dy < dx and dst.x > di.x and dst.y < di.y
{
d = dy/dx * 45;
}
if dy > dx and dst.x > di.x and dst.y < di.y
{
d = -(dx/dy * 45) + (90);
}
if dy > dx and dst.x < di.x and dst.y < di.y
{
d = (dx/dy * 45) + (90);
}
if dx > dy and dst.x < di.x and dst.y < di.y
{
d = -(dy/dx * 45) + 180;
}
if dx > dy and dst.x < di.x and dst.y > di.y
{
d = (dy/dx * 45) + 180;
}
if dy > dx and dst.x < di.x and dst.y > di.y
{
d = -(dx/dy * 45) + 270;
}
if dy > dx and dst.x > di.x and dst.y > di.y
{
d = (dx/dy * 45) + 270;
}
if dy < dx and dst.x > di.x and dst.y > di.y
{
d = -(dy/dx * 45) + 360;
}
if dy == dx
{
if dst.x > di.x and dst.y < di.y { d = 45; }
if dst.x < di.x and dst.y > di.y { d = 270 - 45; }
if dst.x < di.x and dst.y < di.y { d = 45 + 90; }
if dst.x > di.x and dst.y > di.y { d = 360 - 45; }
}
if dy == 0 and dst.x > di.x { d = 360; } if dy == 0 and dst.x < di.x { d = 180; }
if dx == 0 and dst.y > di.y { d = 270; } if dx == 0 and dst.y < di.y { d = 90; }
// with this code your objects can create in each direction of DDD.DD (this is the direction number)
// you dont need too much accuracy, a three number integer, like 154, can be enough for finding direction
as you see, you need to declaration "dx, dy, d, di, dst" variables in your create event and set a 0 value for initializing them, dst and di are your objects you can rename them if you want. dst will get the di position and then calculate it and then tell the own posiotion (dst position) to di, it mean if di get the direction of dst, then di can rotate the sprite to it, this code return the direction value as "d" variable, if you want to create an object in front of your character then you need use your direction degree in "sin" or "cos" or "tan" or "cot" or ..., if you decide to do that message me here, then i will write the code and instruction for you
 

KhMaIBQ

Member
Directional movement is much easier to do if you use the OR bitwise operator. Here is how you do this...

Code:
{
    // Using the OR bitwise operator we can get the direction of input as a numerical value
    // With 4-directional input, the outcome is 16 different states the directional input can be in
    // Right = 1 ( Bit 1 0001 )
    //  Left = 2 ( Bit 2 0010 )
    //    Up = 4 ( Bit 3 0100 )
    //  Down = 8 ( Bit 4 1000 )
    
    var tmp_rad, tmp_speed;
    tmp_rad = degtorad( 45 ); // Get radian value for 45 degress for diagonal movement speed calculation
    tmp_speed = 5; // Overall movement speed of the character
    
    // Tracks status of directional inputs ( right, left, up, down )
    var tmp_r, tmp_l, tmp_u, tmp_d;
    tmp_r = 0;
    tmp_l = 0;
    tmp_u = 0;
    tmp_d = 0;
    
    // Check input status for each key
    if ( keyboard_check( ord( "D" ) ) ) { tmp_r = 1; }
    if ( keyboard_check( ord( "A" ) ) ) { tmp_l = 2; }
    if ( keyboard_check( ord( "W" ) ) ) { tmp_u = 4; }
    if ( keyboard_check( ord( "S" ) ) ) { tmp_d = 8; }
    
    // Get current state of all inputs using OR bitwise operator with all four status variables
    var tmp_dir = tmp_r | tmp_l | tmp_u | tmp_d;
    
    // Handle current input state using a switch statement
    // All other values not handled are considered invalid input states ( ex. Left + Right )
    switch ( tmp_dir )
    {
        case 1: // Right
        {
            x += tmp_speed;
            break;
        }
        case 2: // Left
        {
            x -= tmp_speed;
            break;
        }
        case 4: // Up
        {
            y -= tmp_speed;
            break;
        }
        case 8: // Down
        {
            y += tmp_speed;
            break;
        }
        case 5: // Up-Right
        {
            x += tmp_speed * sin( tmp_rad );
            y -= tmp_speed * cos( tmp_rad );
            break;
        }
        case 6: // Up-Left
        {
            x -= tmp_speed * sin( tmp_rad );
            y -= tmp_speed * cos( tmp_rad );
            break;
        }
        case 9: // Down-Right
        {
            x += tmp_speed * sin( tmp_rad );
            y += tmp_speed * cos( tmp_rad );
            break;
        }
        case 10: // Down-Left
        {
            x -= tmp_speed * sin( tmp_rad );
            y += tmp_speed * cos( tmp_rad );
            break;
        }
    }
}
Before I give advice about your attack object issue, I have a few questions about it to clarify what it is you are trying to do.

Are you only able to attack in 8 directions?
Does the mouse influence the attack direction at all?
 
D

Dave Martinez

Guest
lengthdir_x(len, dir);
lengthdir_y(len, dir);
 
Last edited by a moderator:

chefdez

Member
lengthdir_x(len, dir);
lengthdir_y(len, dir);
Hello, thanks for your reply that information was very useful. In the video however, it shows him using the mouse direction to shoot the bullet. What I'm trying to do is make the image angle do a specific sprite. In essence, if I'm facing left and I press my left mouse button, then I will create sprite_slash_left.
 

chefdez

Member
Directional movement is much easier to do if you use the OR bitwise operator. Here is how you do this...

Code:
{
    // Using the OR bitwise operator we can get the direction of input as a numerical value
    // With 4-directional input, the outcome is 16 different states the directional input can be in
    // Right = 1 ( Bit 1 0001 )
    //  Left = 2 ( Bit 2 0010 )
    //    Up = 4 ( Bit 3 0100 )
    //  Down = 8 ( Bit 4 1000 )
   
    var tmp_rad, tmp_speed;
    tmp_rad = degtorad( 45 ); // Get radian value for 45 degress for diagonal movement speed calculation
    tmp_speed = 5; // Overall movement speed of the character
   
    // Tracks status of directional inputs ( right, left, up, down )
    var tmp_r, tmp_l, tmp_u, tmp_d;
    tmp_r = 0;
    tmp_l = 0;
    tmp_u = 0;
    tmp_d = 0;
   
    // Check input status for each key
    if ( keyboard_check( ord( "D" ) ) ) { tmp_r = 1; }
    if ( keyboard_check( ord( "A" ) ) ) { tmp_l = 2; }
    if ( keyboard_check( ord( "W" ) ) ) { tmp_u = 4; }
    if ( keyboard_check( ord( "S" ) ) ) { tmp_d = 8; }
   
    // Get current state of all inputs using OR bitwise operator with all four status variables
    var tmp_dir = tmp_r | tmp_l | tmp_u | tmp_d;
   
    // Handle current input state using a switch statement
    // All other values not handled are considered invalid input states ( ex. Left + Right )
    switch ( tmp_dir )
    {
        case 1: // Right
        {
            x += tmp_speed;
            break;
        }
        case 2: // Left
        {
            x -= tmp_speed;
            break;
        }
        case 4: // Up
        {
            y -= tmp_speed;
            break;
        }
        case 8: // Down
        {
            y += tmp_speed;
            break;
        }
        case 5: // Up-Right
        {
            x += tmp_speed * sin( tmp_rad );
            y -= tmp_speed * cos( tmp_rad );
            break;
        }
        case 6: // Up-Left
        {
            x -= tmp_speed * sin( tmp_rad );
            y -= tmp_speed * cos( tmp_rad );
            break;
        }
        case 9: // Down-Right
        {
            x += tmp_speed * sin( tmp_rad );
            y += tmp_speed * cos( tmp_rad );
            break;
        }
        case 10: // Down-Left
        {
            x -= tmp_speed * sin( tmp_rad );
            y += tmp_speed * cos( tmp_rad );
            break;
        }
    }
}
Before I give advice about your attack object issue, I have a few questions about it to clarify what it is you are trying to do.

Are you only able to attack in 8 directions?
Does the mouse influence the attack direction at all?
Wow what an awesome code, I'll take some time to study that. Thank you.
To answer your question, it is a top down game with a simple sword slash, it would be cool if I could attack in 8 directions. I have 2 attacks, throwing a ninja star which I would like affected by the mouse direction when I press space bar, and slash attack with my left mouse button.

What I'm trying to accomplish with my left mouse button is to use a certain sprite depending on the angle my player sprite is facing. So if I'm facing left and press left mb then sprite_slash_left will play and destroy itself after the animation is completed.

Also, a question for you... How do I assign my directional sprites to the code you provided? Meaning if I hold W and D, then sprite will change to sprite_run_right.

Thanks a ton
Code:
if keyboard_check(ord('W')) && sprite_index != sprite_run_forward
        {       
            image_index = 1;
            sprite_index = sprite_run_forward; //run animation
            image_speed = 1
           
            }

if !keyboard_check(ord('W')) && sprite_index = sprite_run_forward
        {
        image_index = 0;
        sprite_index = sprite_ninja_idle_forward; //standing
        image_speed = 1;               
        }
       
if keyboard_check(ord('A')) && sprite_index != sprite_run_left
        {       
            image_index = 1;
            sprite_index = sprite_run_left; //run animation
            image_speed = 1
        }
if !keyboard_check(ord('A')) && sprite_index = sprite_run_left
        {
        image_index = 0;
        sprite_index = sprite_ninja_idle_left; //standing
        image_speed = 1;               
        }
       
if keyboard_check(ord('S')) && sprite_index != sprite_run_down
        {       
            image_index = 1;
            sprite_index = sprite_run_down; //run animation
            image_speed = 1
        }
if !keyboard_check(ord('S')) && sprite_index = sprite_run_down
        {
        image_index = 0;
        sprite_index = sprite_ninja_idle_down; //standing
        image_speed = 1;               
        }
       
if keyboard_check(ord('D')) && sprite_index != sprite_run_right
        {       
            image_index = 1;
            sprite_index = sprite_run_right; //run animation
            image_speed = 1;
            }
       
if !keyboard_check(ord('D')) && sprite_index = sprite_run_right
        {
        image_index = 0;
        sprite_index = sprite_ninja_idle_right; //standing
        image_speed = 1;               
        }
 

KhMaIBQ

Member
If we assume you are using Game Maker's built-in variables, we can modify the switch statement from before to this...

Code:
// Handle current input state using a switch statement
    // All other values not handled are considered invalid input states ( ex. Left + Right )
    switch ( tmp_dir )
    {
        case 1: // Right
        {
            direction = 0;
            speed = 5;
            sprite_index = spr_run_right;
            break;
        }
        case 2: // Left
        {
            direction = 180;
            speed = 5;
            sprite_index = spr_run_left;
            break;
        }
        case 4: // Up
        {
            direction = 90;
            speed = 5;
            sprite_index = spr_run_up;
            break;
        }
        case 8: // Down
        {
            direction = 270;
            speed = 5;
            sprite_index = spr_run_down;
            break;
        }
        case 5: // Up-Right
        {
            direction = 45;
            speed = 5;
            sprite_index = spr_run_up_right;
            break;
        }
        case 6: // Up-Left
        {
            direction = 135;
            speed = 5;
            sprite_index = spr_run_up_left;
            break;
        }
        case 9: // Down-Right
        {
            direction = 225;
            speed = 5;
            sprite_index = spr_run_down_right;
            break;
        }
        case 10: // Down-Left
        {
            direction = 315;
            speed = 5;
            sprite_index = spr_run_down_left;
            break;
        }
        default: // Invalid input or idle
        {
            speed = 0;
            switch ( direction )
            {
                case 0: sprite_index = spr_idle_right; break;
                case 45: sprite_index = spr_idle_up_right; break;
                case 90: sprite_index = spr_idle_up; break;
                case 135: sprite_index = spr_idle_up_left; break;
                case 180: sprite_index = spr_idle_left; break;
                case 225: sprite_index = spr_idle_down_left; break;
                case 270: sprite_index = spr_idle_down; break;
                case 315: sprite_index = spr_idle_down_right; break;
            }
            break;
        }
    }
Direction in Game Maker starts with right being 0 and the value increases as you go counter-clockwise. 0 is right, 90 is up, 180 is left, and 270 is down.

You can then use that direction value to determine how to spawn your slash attack when you press the left mouse button.

Code:
{
    // Spawn the slash attack and set the offset
    var tmp_slash = instance_create( x + lengthdir_x( offset, direction ), y + lengthdir_y( offset, direction ), obj_slash );
    
    // Set the slash sprite based on the player's direction
    with ( direction )
    {
        case 0: tmp_slash.sprite_index = spr_slash_right; break;
        case 45: tmp_slash.sprite_index = spr_slash_up_right; break;
        case 90: tmp_slash.sprite_index = spr_slash_up; break;
        case 135: tmp_slash.sprite_index = spr_slash_up_left; break;
        case 180: tmp_slash.sprite_index = spr_slash_left; break;
        case 225: tmp_slash.sprite_index = spr_slash_down_left; break;
        case 270: tmp_slash.sprite_index = spr_slash_down; break;
        case 315: tmp_slash.sprite_index = spr_slash_down_right; break;
    }
}
That video that Dave Martinez posted should work for your ninja star attack.
 

chefdez

Member
If we assume you are using Game Maker's built-in variables, we can modify the switch statement from before to this...

Code:
// Handle current input state using a switch statement
    // All other values not handled are considered invalid input states ( ex. Left + Right )
    switch ( tmp_dir )
    {
        case 1: // Right
        {
            direction = 0;
            speed = 5;
            sprite_index = spr_run_right;
            break;
        }
        case 2: // Left
        {
            direction = 180;
            speed = 5;
            sprite_index = spr_run_left;
            break;
        }
        case 4: // Up
        {
            direction = 90;
            speed = 5;
            sprite_index = spr_run_up;
            break;
        }
        case 8: // Down
        {
            direction = 270;
            speed = 5;
            sprite_index = spr_run_down;
            break;
        }
        case 5: // Up-Right
        {
            direction = 45;
            speed = 5;
            sprite_index = spr_run_up_right;
            break;
        }
        case 6: // Up-Left
        {
            direction = 135;
            speed = 5;
            sprite_index = spr_run_up_left;
            break;
        }
        case 9: // Down-Right
        {
            direction = 225;
            speed = 5;
            sprite_index = spr_run_down_right;
            break;
        }
        case 10: // Down-Left
        {
            direction = 315;
            speed = 5;
            sprite_index = spr_run_down_left;
            break;
        }
        default: // Invalid input or idle
        {
            speed = 0;
            switch ( direction )
            {
                case 0: sprite_index = spr_idle_right; break;
                case 45: sprite_index = spr_idle_up_right; break;
                case 90: sprite_index = spr_idle_up; break;
                case 135: sprite_index = spr_idle_up_left; break;
                case 180: sprite_index = spr_idle_left; break;
                case 225: sprite_index = spr_idle_down_left; break;
                case 270: sprite_index = spr_idle_down; break;
                case 315: sprite_index = spr_idle_down_right; break;
            }
            break;
        }
    }
Direction in Game Maker starts with right being 0 and the value increases as you go counter-clockwise. 0 is right, 90 is up, 180 is left, and 270 is down.

You can then use that direction value to determine how to spawn your slash attack when you press the left mouse button.

Code:
{
    // Spawn the slash attack and set the offset
    var tmp_slash = instance_create( x + lengthdir_x( offset, direction ), y + lengthdir_y( offset, direction ), obj_slash );
   
    // Set the slash sprite based on the player's direction
    with ( direction )
    {
        case 0: tmp_slash.sprite_index = spr_slash_right; break;
        case 45: tmp_slash.sprite_index = spr_slash_up_right; break;
        case 90: tmp_slash.sprite_index = spr_slash_up; break;
        case 135: tmp_slash.sprite_index = spr_slash_up_left; break;
        case 180: tmp_slash.sprite_index = spr_slash_left; break;
        case 225: tmp_slash.sprite_index = spr_slash_down_left; break;
        case 270: tmp_slash.sprite_index = spr_slash_down; break;
        case 315: tmp_slash.sprite_index = spr_slash_down_right; break;
    }
}
That video that Dave Martinez posted should work for your ninja star attack.
Awesome, any reason why that second code box returns a token is undefined error?
 

KhMaIBQ

Member
The word "offset" in the code is not defined. You just need to replace it with your actual offset value.
 

chefdez

Member
// Spawn the slash attack and set the offset
var tmp_slash = instance_create( x + lengthdir_x(0, direction ), y + lengthdir_y(-15, direction ), object_slash );

// Set the slash sprite based on the player's direction
with ( direction )
{
case 0: tmp_slash.sprite_index = sprite_slash_right; break;
case 90: tmp_slash.sprite_index = sprite_slash_forward; break;
case 180: tmp_slash.sprite_index = sprite_slash_left; break;
case 270: tmp_slash.sprite_index = sprite_slash_down; break;
}

Since I don't have the other sprites yet I deleted some of those cases. It gives me a token error here. If I change this to a switch statement the game starts but it keeps running the attack sprite infinitely
I got a lot of studying to do lol, this is harder than it looks
Also, I haven't set a mouse check
 

KhMaIBQ

Member
Please read the manual about the lengthdir_x/y functions because you are using them incorrectly in the code I gave you above. Always read the manual about functions you are unfamiliar with because it helps solve many typical problems people come across.

Please post the error you are getting because I have no clue how to solve it if I don't know what the actual error is.

With any code given to you on the forum, always make sure to adapt it to your needs and don't copy-and-paste it blindly. The people providing you the code are giving you something to build upon, and it is up to you to make it work within your project.
 

chefdez

Member
Yes and I thank you for your patience as I am 100% new to programming. I do make some slight changes that seem to make sense to me but as someone on the outside looking in, some forms of the language aren't always readable. My understanding of lengthdir is that it starts at the origin point of your sprite and adds lengthdir to it as an x and y value.
Code:
   if mouse_check_button_pressed(mb_left) {
    var tmp_slash = instance_create(x + lengthdir_x(-32, direction ), y + lengthdir_y(32, direction ), object_slash );
 
    // Set the slash sprite based on the player's direction
    with (direction)
    {
        case 0: tmp_slash.sprite_index = sprite_slash_right; break;
        case 90: tmp_slash.sprite_index = sprite_slash_forward; break;
        case 180: tmp_slash.sprite_index = sprite_slash_left; break;
        case 270: tmp_slash.sprite_index = sprite_slash_down; break;
    }
 }
I added a mouse check so that slash is created at lengthdir whenever the left mb is pressed (to me that makes sense anyway... o_o)
My lengthdir looks very similiar to the one in the manual.
Code:
instance_create(x + lengthdir_x(64, image_angle), y + lengthdir_y(64, image_angle), obj_bullet);
Except in this case we are using the direction variable to determine which angle we are facing, right?

This is my error: In object object_ninja in Event StepnormalEvent action number 1 at line 116: Token is undefined at CompileStatement. It starts are case 0 and ends at case 270.

Thanks again
 
Last edited:

KhMaIBQ

Member
Sorry about that. I accidentally put "with" instead of "switch" in that last part. *face palm*

Code:
{
    // Perform slash attack if left mouse button is triggered
    if ( mouse_check_button_pressed( mb_left ) )
    {
        // Create the slash object and store its ID in a temp variable to allow access to it to finish initializing
        // The 32 in lengthdir_x and lengthdir_y is the offset value from the origin point
        // Both values are the same and positive because it is the length of the offset from the origin
        // The value returned by both functions will be positive or negative depending on the direction
        var tmp_slash = instance_create( x + lengthdir_x( 32, direction ), y + lengthdir_y( 32, direction ), object_slash );
        
        // Set the slash sprite based on the player's direction
        // This switch currently does not handle the diagonal directions of the slash
        switch ( direction )
        {
            case 0: tmp_slash.sprite_index = sprite_slash_right; break;
            case 90: tmp_slash.sprite_index = sprite_slash_forward; break;
            case 180: tmp_slash.sprite_index = sprite_slash_left; break;
            case 270: tmp_slash.sprite_index = sprite_slash_down; break;
        }
    }
}
 

chefdez

Member
Ah yeah I figured, I changed that to switch, set it to mouse left and have it destroy itself when the animation ends. You've been a tremendous help, thank you very much! Could you tell me how this code determines which angle to face by default when speed is at 0? By default all animations look to the right after I finish running
 

KhMaIBQ

Member
If an invalid directional input is detected or no keys are being held, the default from the switch statement from before should choose the correct animation.

Code:
default: // Invalid input or idle
        {
            speed = 0;
            switch ( direction )
            {
                case 0: sprite_index = spr_idle_right; break;
                case 45: sprite_index = spr_idle_up_right; break;
                case 90: sprite_index = spr_idle_up; break;
                case 135: sprite_index = spr_idle_up_left; break;
                case 180: sprite_index = spr_idle_left; break;
                case 225: sprite_index = spr_idle_down_left; break;
                case 270: sprite_index = spr_idle_down; break;
                case 315: sprite_index = spr_idle_down_right; break;
            }
            break;
        }
EDIT: If you are using your code from post #7, that is why you are defaulting to facing right while idle. Specifically this part is causing it...

Code:
if !keyboard_check(ord('D')) && sprite_index = sprite_run_right
        {
        image_index = 0;
        sprite_index = sprite_ninja_idle_right; //standing
        image_speed = 1;               
        }
 

chefdez

Member
Heh I got rid of that and the problem persisted... in the end I had a no key event that was interrupting the code, deleted that and now my animations go idle the direction im facing. Thanks for all the help and patience!
 

chefdez

Member
If an invalid directional input is detected or no keys are being held, the default from the switch statement from before should choose the correct animation.

Code:
default: // Invalid input or idle
        {
            speed = 0;
            switch ( direction )
            {
                case 0: sprite_index = spr_idle_right; break;
                case 45: sprite_index = spr_idle_up_right; break;
                case 90: sprite_index = spr_idle_up; break;
                case 135: sprite_index = spr_idle_up_left; break;
                case 180: sprite_index = spr_idle_left; break;
                case 225: sprite_index = spr_idle_down_left; break;
                case 270: sprite_index = spr_idle_down; break;
                case 315: sprite_index = spr_idle_down_right; break;
            }
            break;
        }
EDIT: If you are using your code from post #7, that is why you are defaulting to facing right while idle. Specifically this part is causing it...

Code:
if !keyboard_check(ord('D')) && sprite_index = sprite_run_right
        {
        image_index = 0;
        sprite_index = sprite_ninja_idle_right; //standing
        image_speed = 1;              
        }
Argh, so I decided to try to add the remaining 4 directions and I encountered a problem... when I'm walking down with S and I press A or D the return values are reversed. For example S + A is 315 and S + D is 225, however this looks like it makes sense

case 9: // Down-Left
{
direction = 225;
speed = 5;
sprite_index = sprite_run_down_left;
break;
}
case 10: // Down-Right
{
direction = 315;
speed = 5;
sprite_index = sprite_run_down_right;
break;
}
 

KhMaIBQ

Member
My mistake. When down and left are pressed, the bitwise operator returns 10. When down and right are pressed, the bitwise operator returns 9. Direction 225 goes down-left and direction 315 goes down-right.

case 9: // Down-Right
{
direction = 315;
speed = 5;
sprite_index = sprite_run_down_right;
break;
}
case 10: // Down-Left
{
direction = 225;
speed = 5;
sprite_index = sprite_run_down_left;
break;
}
 
Top