Jump to content

Does PixiJSv4 Do Input? Need An Example!


JeZxLee
 Share

Recommended Posts

I talk of keyboard input...

What would be the best way to handle keyboard input in a PixiJSv4 game?

	document.addEventListener("keypress", CheckForKeyPress, true);
	document.addEventListener("keydown", CheckForKeyDown, true);
	document.addEventListener("keyup", CheckForKeyRelease, true);

Is the above OK, or I need to do something different?

function CheckForKeyDown(evt)
{
    if (DelayAllUserInput === 0)
    {
		if (evt.keyCode === 8)  KeyboardCharacterPressed = "=";
		else if (evt.keyCode === 13)  KeyboardCharacterPressed = "/";
		else if (evt.keyCode === 27)  KeyboardCharacterPressed = "~";
		else if (evt.keyCode === 32)  KeyboardCharacterPressed = " ";
		
		if (evt.keyCode === 38)  JoystickDirection/*.Vertical*/ = UP;
		else if (evt.keyCode === 39)  JoystickDirection/*.Horizontal*/ = RIGHT;
		else if (evt.keyCode === 40)  JoystickDirection/*.Vertical*/ = DOWN;
		else if (evt.keyCode === 37)  JoystickDirection/*.Horizontal*/ = LEFT;

		if (evt.keyCode === 90)  JoystickButtonOne = ON;
		else if (evt.keyCode === 88)  JoystickButtonTwo = ON;
    }
}

 

Link to comment
Share on other sites

That's one way yes, and it would certainly work, although it gives a precedence to keypresses, which might be unacceptable. For example, if you hit right and up at the same time (which can happen if you're trying to change direction quickly) then up will always win as you've introduced precedence by using if..else clauses.

This might be fine for you, otherwise you'll have to be a bit smarter about how you handle your inputs.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...