A
Awesmazing
Guest
Hey everyone, new to the forums but I've been tinkering with GM for a while. This may be old news to some people, but if you're like me, and have googled this exact topic only to get answers that are excellent and work well, but involve using scripts in your game. This can be great, because the GM scripts language is comparatively easy to understand, but if you're very fresh and expected more of a drag and drop experience in making games (as I do) this might help you understand more about both the inner-workings of the game engine and the drag and drop functions as well.
So basically... The attached image shows an object that can move freely up, down, left, right, and any combination of two directional keys. The movement is smooth and works as expected. The only con I've come across for this is that pressing any three keys simultaneously will stop movement. This can be fixed with only the drag and drop features as well but gets a bit more complicated. The method that is working here is based on the principle that a few lines of code can use with the control of a variable to essentially know what keys are pressed, and what direction that should move you in.
The create event simply sets the self variable "move" to 0
The step event in the image gives directions to the object based on the keys pressed. Each key is assigned a "value" and that value is either added to the "move" variable when the key is pressed, or subtracted from the "move" variable when released. Also note the alarm between the "move" variable check and the "move to position" action. The "move to position" action is set to move the object to the center of itself (stand still) after a short timer (set to 5) whenever "move" is set to 0. Without that timer, I found this setup to not work properly. I'm not sure as to why, since pressing keys should increase the variable more quickly than the set position action should take place but obviously not. Either way, that small delay allows this to work. If someone knows more about that I'd be interested.
Each other "move" variable check has 8 total numbers it looks for to determine whether to move up, down, left, right, up/left, up/right, down/left, or down/right. Each arrow key is assigned a value in the key press events, which are all relative to the current value of "move" I used up as +1, left to +3, right +5, and down +7. Releasing any of those keys triggers the key release event which removes the same value the key added from the "move" variable. So, if up and left are pressed, "move" will = 4 and cause the appropriate directional movement. The reason 1, 3, 5, and 7 were used was so that no combination of keys can have an overlapping value. Any numbers will work, just make sure the sums are all different for the 8 directions involving any 2 pressed keys.
Obviously this is not limited to the arrow keys and can be duplicated or substituted with WASD as well. I just figured after searching to find a completely drag and drop solution for this, I'd hammer it out and write one myself. Let me know if you have any questions or improvements!
So basically... The attached image shows an object that can move freely up, down, left, right, and any combination of two directional keys. The movement is smooth and works as expected. The only con I've come across for this is that pressing any three keys simultaneously will stop movement. This can be fixed with only the drag and drop features as well but gets a bit more complicated. The method that is working here is based on the principle that a few lines of code can use with the control of a variable to essentially know what keys are pressed, and what direction that should move you in.
The create event simply sets the self variable "move" to 0
The step event in the image gives directions to the object based on the keys pressed. Each key is assigned a "value" and that value is either added to the "move" variable when the key is pressed, or subtracted from the "move" variable when released. Also note the alarm between the "move" variable check and the "move to position" action. The "move to position" action is set to move the object to the center of itself (stand still) after a short timer (set to 5) whenever "move" is set to 0. Without that timer, I found this setup to not work properly. I'm not sure as to why, since pressing keys should increase the variable more quickly than the set position action should take place but obviously not. Either way, that small delay allows this to work. If someone knows more about that I'd be interested.
Each other "move" variable check has 8 total numbers it looks for to determine whether to move up, down, left, right, up/left, up/right, down/left, or down/right. Each arrow key is assigned a value in the key press events, which are all relative to the current value of "move" I used up as +1, left to +3, right +5, and down +7. Releasing any of those keys triggers the key release event which removes the same value the key added from the "move" variable. So, if up and left are pressed, "move" will = 4 and cause the appropriate directional movement. The reason 1, 3, 5, and 7 were used was so that no combination of keys can have an overlapping value. Any numbers will work, just make sure the sums are all different for the 8 directions involving any 2 pressed keys.
Obviously this is not limited to the arrow keys and can be duplicated or substituted with WASD as well. I just figured after searching to find a completely drag and drop solution for this, I'd hammer it out and write one myself. Let me know if you have any questions or improvements!
Attachments
-
38.2 KB Views: 51