mouse control coding

D

danne249

Guest
so this will be a bit tricky explaining on what i need help with that im have been stuck on for a long while (im not a good ex plainer).
its a topdown shoot im making

so basically i have succeeded with making the mouse to have having full control over where my character is looking based on the position of the mouse and with that have a standard idle animation when im not moving.
This is the code that im currently using in an step event.

//mouse controll
var dir = point_direction(x,y,mouse_x,mouse_y);

if(dir > 45 && dir <= 135){
// Up
}
else if(dir > 135 && dir <= 225){
sprite_index = character_idle_left_spr;
image_speed = 0.03;

// Left
}
else if(dir > 225 && dir <= 315){
// Down
}
else if(dir > 315 || dir <= 45){
sprite_index = character_idle_right;
image_speed = 0.03;
// Right
}

And right now im trying to ad 2 other commands to both directions, run animation and "aim" animation.
But the thing is i dont want the character to start to look in the right direction if im walking right and have my mouse to the left, so the mouse is still in full control over the animation.

i think i need to ad some sort of code on each dir saying for example on the right dir "if D key pressed, then right sprint animation will happen, else if right mouse button pressed, right aim animation happen" but this string of code can only happen if i have my mouse in the right dir "technically have the right dir code active".

But if i have the left dir code active, it will do like "if D key pressed, then left sprint animation will happen, else if right mouse button pressed, left aim animation will happen"

I hope this make sense for you guys, i have no idea on how to code this 💩💩💩💩, and i cant find a clear answer on google or im just stupid.
 
G

Gillen82

Guest
First of all, never feel "stupid" about not knowing something. We all get stuck at times. We all need help at times.

Are you looking your character to always face in the direction of the mouse, regardless of the direction you are moving?

if so, just use the following:

Code:
image_angle = point_direction( x, y, mouse_x, mouse_y );
As for the animations, you can now control these separately:

Code:
if ( image_angle < 45 ) || ( image_angle > 315 ) {

    //Facing Left Animation code to go here

}else if ( image_angle > 135 ) || ( image_angle < 225 ) {

    //Facing Left Animation code to go here

}else {

    //Idle Animation to go here

}
Sorry if this doesn't help!!
 
D

danne249

Guest
First of all, never feel "stupid" about not knowing something. We all get stuck at times. We all need help at times.

Are you looking your character to always face in the direction of the mouse, regardless of the direction you are moving?

if so, just use the following:

Code:
image_angle = point_direction( x, y, mouse_x, mouse_y );
As for the animations, you can now control these separately:

Code:
if ( image_angle < 45 ) || ( image_angle > 315 ) {

    //Facing Left Animation code to go here

}else if ( image_angle > 135 ) || ( image_angle < 225 ) {

    //Facing Left Animation code to go here

}else {

    //Idle Animation to go here

}
Sorry if this doesn't help!!
ah thanks :p

that i have done, so the character will "flip" from the right to the left "depends on what value is greater of my mouse position and at the same time switch thru two diffrent idle animations "left and right".
but the thing is when i move around it still does the idle animations, and what im trying to accomplish is that when i move i want the character to switch from idle animation to a running animation but here comes the tricky part.
I want the mouse to still have controll on where the character is facing when he is doing the run animation so for an example, my character will look like its running backwards if i run left and have my mouse positioned the the right of the character.

i think im over complicate this but yes, i want it to do as you wrote but i want it to be able to do more animations in each direction depending on what action is triggered.

this is how the game looks right now

im not good at explaining :p
 

Attachments

G

Gillen82

Guest
I think I'm with you here. Would this work?

Code:
//Check to see if you are facing right
if ( image_angle < 45 ) || ( image_angle > 315 ) {

    //Check to see if you are moving
    if ( speed != 0 ) {

    //Put running animation here

    } else {

    //Put idle animation here

    }

}

//Check to see if you are facing left
if ( image_angle > 135 ) || ( image_angle < 225 ) {

   //Check to see if you are moving
    if ( speed != 0 ) {

        //Put running animation here

    } else {

        //Put idle animation here

    }

}
There's probably an easier way to do it...but like yourself...I'm still learning :)
 
G

Gillen82

Guest
Another quick question...How are you controlling your character's movement?
 
D

danne249

Guest
I think I'm with you here. Would this work?

Code:
//Check to see if you are facing right
if ( image_angle < 45 ) || ( image_angle > 315 ) {

    //Check to see if you are moving
    if ( speed != 0 ) {

    //Put running animation here

    } else {

    //Put idle animation here

    }

}

//Check to see if you are facing left
if ( image_angle > 135 ) || ( image_angle < 225 ) {

   //Check to see if you are moving
    if ( speed != 0 ) {

        //Put running animation here

    } else {

        //Put idle animation here

    }

}
There's probably an easier way to do it...but like yourself...I'm still learning :)
hmm something happen anyway, it wont do exactly what i want but you understand what i want to do anyway :D there is some problem with the code, im trying to figure out what is making it.
the thing is happening is that if i have the speed at 0 then there will be only the ide animation even if i move, but when i turn the speed to 1 or higher then the character will only use the run animation instead and wont stop using it :I i think i know what the problem is, im working on it right now so i give you an update if i fix it :)
 
D

danne249

Guest
Another quick question...How are you controlling your character's movement?
oh 💩💩💩💩, didnt se you reply, i use a 8x movement code that look like this, i have it in the "any key" event

Code:
//character movment dir

if (keyboard_check(ord('W')) && keyboard_check(ord('D')))
// dir and speed
{
    x+=3; y-=3;
}
     
else if (keyboard_check(ord('W')) && keyboard_check(ord('A')))
// dir and speed
{x-=3; y-=3;}

else if (keyboard_check(ord('S')) && keyboard_check(ord('D')))
// dir and speed
{x+=3; y+=3;}

else if (keyboard_check(ord('S')) && keyboard_check(ord('A')))
// dir and speed
{x-=3; y+=3;}

else if keyboard_check(ord('W'))
// dir and speed
{x+=0; y-=4;}

else if keyboard_check(ord('A'))
// dir and speed
{x-=4; y-=0;}

else if keyboard_check(ord('S'))
// dir and speed
{x+=0; y+=4;}

else if keyboard_check(ord('D'))
// dir and speed
{x+=4; y-=0;}
 
A

Ampersand

Guest
I think I understand.

So your xscale is set like
if sign(mouse_x - x) != 0
{
image_xscale = sign(mouse_x - x) ;
}
and he looks like he's running the wrong way if he's moving left but the mouse is on the right?

Simply play the running animation backwards if he is backstepping. So

var mousedir = point_direction(x, y, mouse_x, mouse_y) ;
var walkdir = point_direction(x, y, xprevious, yprevious) ;
if abs(angle_difference(mouse_dir, walkdir)) < 90
{
image_speed += 1 ;
}
else
{
image_speed -= 1 ;
}

Let me know if this does not work for you!

Edit: Doh, now I see the movement code. Fixed. Also if you let me know what exactly your end goal gfx wise is I can flesh that out a little more.
 
Last edited by a moderator:
G

Gillen82

Guest
Why not try this:

Code:
//Set Controls at top of step event
left = keyboard_check(vk_left);
right = keyboard_check(vk_right);
up = keyboard_check(vk_up);
down = keyboard_check(vk_down);

//Move left
if ( left ) {
    hspeed = -4;

    //Put run animation code in here

}

//Move right
if ( right ) {
    hspeed = 4;

    //Put run animation code in here

}

//Move up
if ( up ) {
    vspeed = -4;
}

//Move down
if ( down ) {
    vspeed = 4;
}

//Stop moving
if ( !left && !right ) || ( left && right ){
    hspeed = 0

    //Put idle animation code in here
}

if ( !up && !down ) || ( up && down ){
    vspeed = 0

    //Put idle animation code in here
}
 
D

danne249

Guest
Why not try this:

Code:
//Set Controls at top of step event
left = keyboard_check(vk_left);
right = keyboard_check(vk_right);
up = keyboard_check(vk_up);
down = keyboard_check(vk_down);

//Move left
if ( left ) {
    hspeed = -4;

    //Put run animation code in here

}

//Move right
if ( right ) {
    hspeed = 4;

    //Put run animation code in here

}

//Move up
if ( up ) {
    vspeed = -4;
}

//Move down
if ( down ) {
    vspeed = 4;
}

//Stop moving
if ( !left && !right ) || ( left && right ){
    hspeed = 0

    //Put idle animation code in here
}

if ( !up && !down ) || ( up && down ){
    vspeed = 0

    //Put idle animation code in here
}
i can try that to se if it works, but i dont want to use the VK buttons :p want the WASD controll scheme :p but i think i have tried that once before and ended up in a mess
 
G

Gillen82

Guest
i can try that to se if it works, but i dont want to use the VK buttons :p want the WASD controll scheme :p but i think i have tried that once before and ended up in a mess
Sorry....Just replace vk buttons with appropriate WASD :)
 
Top