Jump to content

2 events at once


gerit99
 Share

Recommended Posts

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

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

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

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 by thescientist
Link to comment
Share on other sites

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

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

Do you want to always call right after forward, or do you want a special keycode/keypress combo that will call them together?

Edited by thescientist
Link to comment
Share on other sites

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...