Created
April 10, 2020 02:23
-
-
Save zardilior/b27a185fc17c8aba0d9fb6552b2e0a4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser-arcade-physics.min.js"></script> | |
<script> | |
// Shooter part 1 | |
/* | |
Load an image | |
insert it | |
Move our ship with arrow keys | |
Change position first | |
Use velocity next | |
*/ | |
var config = { | |
// is it canvas or webgl? (advanced) | |
type: Phaser.AUTO, | |
// the dimensions of our game | |
width: 800, | |
height: 600, | |
// Phaser divides things by scenes | |
// A scene could be a level, a room, a menu, different things, we will work with single scenes for now. To understand the basics. | |
scene: { | |
// our game functions | |
preload: preload, | |
create: create, | |
update: update | |
} | |
}; | |
var game = new Phaser.Game(config); | |
var moving = { | |
up: false, | |
down: false, | |
left: false, | |
down: false | |
} | |
// before the game loads, we load assets here like images and such | |
function preload() { | |
// load an image | |
} | |
// which helps us create the game, here we create the hero we place the different things, and we set timers. | |
function create() { | |
// insert hero ship | |
// setup an event listener | |
document.addEventListener('keydown',function(key){ | |
// check what key is pressed | |
// change the value of the related property | |
console.log('keydown',key.code,key.keyCode); | |
switch(key.code) { | |
case 'ArrowUp': | |
moving.up = true | |
case 'ArrowDown': | |
moving.down = true | |
case 'ArrowRight': | |
moving.right = true | |
case 'Arrowleft': | |
moving.left = true | |
} | |
}) | |
document.addEventListener('keyup',function(key){ | |
// check what key is released | |
// change the value of the related property | |
switch(key.code) { | |
case 'ArrowUp': | |
moving.up = false | |
break; | |
case 'ArrowDown': | |
moving.down = false | |
break; | |
case 'ArrowRight': | |
moving.right = false | |
break; | |
case 'ArrowLeft': | |
moving.left = false | |
break; | |
} | |
console.log('keyup',key.code,key.keyCode); | |
}) | |
} | |
// runs every frame, each time the animation updates | |
function update(){ | |
// move our ship accordingly | |
if(moving.up) console.log('Moving Up!!') | |
if(moving.down) console.log('Moving Down!!') | |
if(moving.left) console.log('Moving Left!!') | |
if(moving.right) console.log('Moving Right!!') | |
} | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment