Object Angle Help

Bulldrome

Member
Hello, I'm new to the forums and GMS2 in general so forgive my inexperience.
So I'm currently having a problem with getting the angle of an object to work correctly. I'm trying to have this object, a shield, move around the player by using the directional keys. Up would set its angle to 90 degrees, Right would set it to 0 degrees, Down and Left would set it to 225 degrees, etc.

Right now, the way I have the object coded, the object reacts the way it's supposed to with the Up and Right key but when pressing Up and Right, the object simple does nothing, its unresponsive. Is there just some simple thing I'm missing?
Any help would be appreciated.

(Also I know using a block of If statements isnt the best practice, I'm just testing things out now)
 

Attachments

SoVes

Member
you have an if statement that is triggering after checking for both keys, so basically you're setting the angle to 45 and back to 0 after it.
 

Bulldrome

Member
you have an if statement that is triggering after checking for both keys, so basically you're setting the angle to 45 and back to 0 after it.
So then how would I go about getting it to just stick at the 45? would I need to do an else statement after each if statement?
 

FrostyCat

Member
A lot of rookies start out trying to handle every input permutation the way you did, and in almost every case they fail the way you also did. You basically have later checks undoing the work of the ones that come before it, for example the one for right undoing the one for right+up.

When it comes to directional input, experienced users don't act on permutations, they act on net results. Each key is an influence to be added to others instead of an absolute reference, and only the final sum of influences is acted upon. The 0-1 form is particularly prevalent, and in this case is all you need:
Code:
var dx, dy;
dx = keyboard_check(vk_right)-keyboard_check(vk_left);
dy = keyboard_check(vk_down)-keyboard_check(vk_up);
if (dx != 0 || dy != 0) {
  image_angle = point_direction(0, 0, dx, dy);
}
 

Bulldrome

Member
Thank you both for the advice.
Seeing it laid out in this way makes a lot of sense and I'm kicking myself for not realizing this was the direction I should have taken my code in.
I'll definitely remember to have the results acted on instead of just the key press alone
 
Top