[SOLVED] angle_difference and keyboard control

Repix

Member
I've done this before.. like ages ago. But I forgot how I worked my way around this..

I know why it doesn't work, but I can't come up with a good work around other than putting in way too much unnecessary code. Any suggestions?

Code:
///angle system

var dd = angle_difference(image_angle, global.pd);

image_angle -= min(abs(dd), 10) * sign(dd);

//setting those variables to its keys

if keyboard_check(ord('D'))
    {
        global.pd = 360
    }
    
else if keyboard_check(ord('W'))
    {
         global.pd = 90
    }
    
else if keyboard_check(ord('A'))
    {
         global.pd = 180
    }
    
else if keyboard_check(ord('S'))
    {
         global.pd = 270
    }
else if keyboard_check(ord('S')) && keyboard_check(ord('W'))
    {
        global.pd = 315
    }
else if keyboard_check(ord('W')) && keyboard_check(ord('D'))
    {
        global.pd = 45
    }
else if keyboard_check(ord('D')) && keyboard_check(ord('S'))
    {
        global.pd = 135
    }
else if keyboard_check(ord('S')) && keyboard_check(ord('A'))
    {
        global.pd = 225
    }
 

jo-thijs

Member
I've done this before.. like ages ago. But I forgot how I worked my way around this..

I know why it doesn't work, but I can't come up with a good work around other than putting in way too much unnecessary code. Any suggestions?

Code:
///angle system

var dd = angle_difference(image_angle, global.pd);

image_angle -= min(abs(dd), 10) * sign(dd);

//setting those variables to its keys

if keyboard_check(ord('D'))
    {
        global.pd = 360
    }
   
else if keyboard_check(ord('W'))
    {
         global.pd = 90
    }
   
else if keyboard_check(ord('A'))
    {
         global.pd = 180
    }
   
else if keyboard_check(ord('S'))
    {
         global.pd = 270
    }
else if keyboard_check(ord('S')) && keyboard_check(ord('W'))
    {
        global.pd = 315
    }
else if keyboard_check(ord('W')) && keyboard_check(ord('D'))
    {
        global.pd = 45
    }
else if keyboard_check(ord('D')) && keyboard_check(ord('S'))
    {
        global.pd = 135
    }
else if keyboard_check(ord('S')) && keyboard_check(ord('A'))
    {
        global.pd = 225
    }
Shorter version:
Code:
///angle system

image_angle += clamp(angle_difference(global.pd, image_angle), -10, 10);

//setting those variables to its keys

if keyboard_check(ord('A')) != keyboard_check(ord('D')) ||keyboard_check(ord('W')) != keyboard_check(ord('S'))
    global.pd = point_direction(keyboard_check(ord('A')), keyboard_check(ord('W')), keyboard_check(ord('D')), keyboard_check(ord('S')));
Looks like it should work to me though.
 

Repix

Member
Maybe it's just the order the code is, since I do press one of those keys at first, so it will probably just use one of the first 4 blocks, not sure.

But thanks for the short version! That looks a lot cleaner.
 

jo-thijs

Member
Maybe it's just the order the code is, since I do press one of those keys at first, so it will probably just use one of the first 4 blocks, not sure.

But thanks for the short version! That looks a lot cleaner.
Oh yeah, silly me.
I was looking for a mistake at a different location.
Because of ow you use else, the if-statements checking for 2 buttons should come before those checking for 1 button only.
 
Top