Quantcast
Channel: Pixi.js Latest Topics
Viewing all articles
Browse latest Browse all 3978

Adding on screen buttons to allow player to move left right up

$
0
0

Hi I'm trying to add  onscreen buttons to move my sprite around my website. 

https://alfielytorres.github.io/hackercastle/

 

right now my code is as follows 

 

//load keyboard
class Keyboard {
	constructor() {
		this.pressed = {};
	}
	watch(el) {
		el.addEventListener('keydown', (e) => {
			this.pressed[e.key] = true;
		});
		el.addEventListener('keyup', (e) => {
			this.pressed[e.key] = false;
		});

	}



app.loader.load((loader, resources) => {
	// create a new keyboard instance
	let kb = new Keyboard();
	kb.watch(app.view);

.....

});



...

	//Jumping
		if (characterPosition.vy < 0) {
			characterPosition.y += characterPosition.vy;
		}
		if (!kb.pressed.ArrowUp && touchingGround && characterPosition.jumped) {
			characterPosition.jumped = false;
		}
		if (kb.pressed.ArrowUp && touchingGround && !characterPosition.jumped) {

			characterPosition.vy = -19;
			characterPosition.jumped = true;
		}

		//Right
		if (kb.pressed.ArrowRight) {
			characterPosition.direction = 0;
			characterPosition.vx = Math.min(8, characterPosition.vx + 2);
		}
		if (characterPosition.vx > 0) {
			characterPosition.vx -= 1;
		}
		//Left
		if (kb.pressed.ArrowLeft) {
			characterPosition.direction = 1;
			characterPosition.vx = Math.max(-8, characterPosition.vx - 2);
		}
		if (characterPosition.vx < 0) {
			characterPosition.vx += 1;
		}

 


Viewing all articles
Browse latest Browse all 3978

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>