gerit99 Posted May 9, 2012 Share Posted May 9, 2012 Is it possible to create that you can use 2 keycodes at the same moment? Like W to walk forward while turning left using another function? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 9, 2012 Share Posted May 9, 2012 The keydown event should fire any time you press a key down. Link to comment Share on other sites More sharing options...
gerit99 Posted May 9, 2012 Author Share Posted May 9, 2012 I mean on the same time Link to comment Share on other sites More sharing options...
justsomeguy Posted May 9, 2012 Share Posted May 9, 2012 The event will fire whenever you press a key. Create a test and see what happens. Link to comment Share on other sites More sharing options...
gerit99 Posted May 9, 2012 Author Share Posted May 9, 2012 If you create a game using javascript, and you want him to walk forward and walk to the right at the same time, you need to do W,D,W,D,W,D,W,D,W,D,W,D but i would like to create it so you could do them together at the same moment. That doesn't work for my script! Link to comment Share on other sites More sharing options...
justsomeguy Posted May 9, 2012 Share Posted May 9, 2012 What test code are you using? Link to comment Share on other sites More sharing options...
gerit99 Posted May 9, 2012 Author Share Posted May 9, 2012 function checkKeyPress(e){// Source from http://www.quirksmode.org/js/events_properties.html var targ; if (!e) var e=window.event; if (e.target) targ=e.target; // End source// alert(e.keyCode); switch(e.keyCode) {case 47: slowDown();break;case 119: playerFurther();break;case 115: playerBack();break; case 100: playerRight();break;case 97: playerLeft();break; case 87: playerFurther();break;case 83: playerBack();break; case 68: playerRight();break;case 65: playerLeft();break;case 37:playerFurther();break; case 38:playerLeft();break; case 39:playerBack();break; case 40:playerRight();break; default: //return false; } return true; }I already have the functions. Link to comment Share on other sites More sharing options...
thescientist Posted May 9, 2012 Share Posted May 9, 2012 If you create a game using javascript, and you want him to walk forward and walk to the right at the same time, you need to do W,D,W,D,W,D,W,D,W,D,W,D but i would like to create it so you could do them together at the same moment. That doesn't work for my script! where's your code? Is this something you are writing yourself or you found? The code will only do what you tell it to do, and as such, can also be re-written to make it do what you want to do. You're talking in vagueries, and without anything to go by, there's not much we can say. If you have a walk forward method, and a walk right method, you can either call walk right after walk forward, or within walk forward, or give the user a new key which will do them both (by calling walk forward then walk right). This way you keep two distinct methods for forward and right, and just have a special function to call them both at the same time. if you find bugs in one method, you just have to change that one method. And separate methods won't bind your functionality in such a way that a person will always move right when the move forward. What if the user really only wants to move forward? Link to comment Share on other sites More sharing options...
thescientist Posted May 9, 2012 Share Posted May 9, 2012 (edited) in that code then, why can't you just call the move right function after the move forward function? or better, yet, a separate case/keycode to specifically move forward/move right. Edited May 9, 2012 by thescientist Link to comment Share on other sites More sharing options...
gerit99 Posted May 9, 2012 Author Share Posted May 9, 2012 My uncle gave me that script so I could do thing with it. But i do not know how to create that script in any other way. Link to comment Share on other sites More sharing options...
Ingolme Posted May 9, 2012 Share Posted May 9, 2012 What I'd do is have a set of flags. Each keydown event activates a flag. Then one single "move()" function will check what flags are activated and choose a direction based on that. Link to comment Share on other sites More sharing options...
gerit99 Posted May 9, 2012 Author Share Posted May 9, 2012 How do you mean flagging? Link to comment Share on other sites More sharing options...
gerit99 Posted May 9, 2012 Author Share Posted May 9, 2012 you mean like move(1) or move(2)? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 9, 2012 Share Posted May 9, 2012 It sounds like you're using keypress, not keydown. The game should be built with a loop that constantly runs, and each time through it needs to check what keys are currently pressed and decide what to do based on that. You can use the keydown and keyup events to maintain a list of which keys are currently down. Link to comment Share on other sites More sharing options...
thescientist Posted May 9, 2012 Share Posted May 9, 2012 (edited) Do you want to always call right after forward, or do you want a special keycode/keypress combo that will call them together? Edited May 9, 2012 by thescientist Link to comment Share on other sites More sharing options...
gerit99 Posted May 9, 2012 Author Share Posted May 9, 2012 a special keycode combo Link to comment Share on other sites More sharing options...
Ingolme Posted May 9, 2012 Share Posted May 9, 2012 A flag is like a switch. A series of flags can be stored in a set of booleans or an integer.When you execute your move() function, you can check through all the flags to see which ones is activated. Let's assume that the flags are properties of a global object called "flags" // Vertical motionif(flags.up) { // Move up X pixels} else if(flags.down) { // Move down X pixels}// Horizontal motionif(flags.left) { // Move left X pixels} else if(flags.right) { // Move right X pixels} Of course there's a lot more to it, and this is probably not the optimal way to solve the problem, but you'll have to do some thinking and research on your own. Link to comment Share on other sites More sharing options...
gerit99 Posted May 9, 2012 Author Share Posted May 9, 2012 And how do i create the flags? Sorry i'm asking so much questions, it would be awesome if this finally worked! Link to comment Share on other sites More sharing options...
Ingolme Posted May 9, 2012 Share Posted May 9, 2012 I suggest you do some reasearch. Like I said, a flag is just a switch, which can be represented as a boolean.This is one way to create flags: var flags = {};flags.up = false;flags.down = false;flags.left = false;flags.right = false; Here's another: var flags = 0;var UP = 0x01;var DOWN = 0x02;var LEFT = 0x04;var RIGHT = 0x05 You will have to do some research because game programming is not something that anybody can just teach on a forum. Online resources will teach you how to use flags. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now