From a9ba8a68d61110cca347e2a4823b25880bc94923 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Mon, 21 Apr 2014 17:14:01 +0200 Subject: Added player with movement Unfortunately, can currently go through walls. re #3 --- game/prefabs/player.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ game/prefabs/wall.js | 2 -- game/states/play.js | 9 +++++++ game/states/preload.js | 1 + 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 game/prefabs/player.js diff --git a/game/prefabs/player.js b/game/prefabs/player.js new file mode 100644 index 0000000..8e3ac3d --- /dev/null +++ b/game/prefabs/player.js @@ -0,0 +1,66 @@ +'use strict'; + +var Player = function(game, x, y, key, frame, controls) { + Phaser.Sprite.call(this, game, x, y, key, frame); + + this.moving = false; + + this.game.input.keyboard.addKeyCapture([ + controls.up, + controls.down, + controls.left, + controls.right + ]); + + this.game.input.keyboard.addKey(controls.up).onDown.add(this.moveUp, this); + this.game.input.keyboard.addKey(controls.down).onDown.add(this.moveDown, this); + this.game.input.keyboard.addKey(controls.left).onDown.add(this.moveLeft, this); + this.game.input.keyboard.addKey(controls.right).onDown.add(this.moveRight, this); +}; + +Player.prototype = Object.create(Phaser.Sprite.prototype); +Player.prototype.constructor = Player; + +Player.prototype.update = function() { +}; + +Player.prototype.moveUp = function() { + this.move(0, -100); +}; +Player.prototype.moveDown = function() { + this.move(0, 100); +}; +Player.prototype.moveLeft = function() { + this.move(-100, 0); +}; +Player.prototype.moveRight = function() { + this.move(100, 0); +}; + +Player.prototype.move = function(deltaX, deltaY) { + if (this.moving) { + return; + } + + var newX = this.x + deltaX; + var newY = this.y + deltaY; + + if (!this.canMoveToNewLocation(newX, newY)) { + return; + } + + this.moving = true; + var tween = this.game.add.tween(this).to({x: newX, y: newY}, 500); + tween.onComplete.add(this.finishMovement, this); + tween.start(); +}; + +Player.prototype.finishMovement = function() { + this.moving = false; +}; + +Player.prototype.canMoveToNewLocation = function(newX, newY) { + return true; +}; + +module.exports = Player; diff --git a/game/prefabs/wall.js b/game/prefabs/wall.js index 56c7e90..4b4b42a 100644 --- a/game/prefabs/wall.js +++ b/game/prefabs/wall.js @@ -2,8 +2,6 @@ var Wall = function(game, x, y, frame) { Phaser.Sprite.call(this, game, x, y, 'wall', frame); - - // initialize your prefab here }; Wall.prototype = Object.create(Phaser.Sprite.prototype); diff --git a/game/states/play.js b/game/states/play.js index ef55d1f..6d542ca 100644 --- a/game/states/play.js +++ b/game/states/play.js @@ -1,6 +1,7 @@ 'use strict'; var Wall = require('../prefabs/wall'); +var Player = require('../prefabs/player'); function Play() {} @@ -10,11 +11,19 @@ Play.prototype = { create: function() { this.createWalls(); // this.world.scale = {x:0.5, y:0.5}; + this.playerA = new Player(this.game, 100, 200, 'player-a', 0, { + up: Phaser.Keyboard.UP, + down: Phaser.Keyboard.DOWN, + left: Phaser.Keyboard.LEFT, + right: Phaser.Keyboard.RIGHT + }); + this.game.add.existing(this.playerA); }, update: function() { }, createWalls: function() { this.walls = this.game.add.group(); + this.walls.add(new Wall(this.game, 0,0)); this.walls.add(new Wall(this.game, 100,0)); this.walls.add(new Wall(this.game, 200,0)); diff --git a/game/states/preload.js b/game/states/preload.js index c8b22ad..7154c8f 100644 --- a/game/states/preload.js +++ b/game/states/preload.js @@ -13,6 +13,7 @@ Preload.prototype = { this.load.onLoadComplete.addOnce(this.onLoadComplete, this); this.load.setPreloadSprite(this.asset); this.load.image('wall', 'assets/images/wall.svg'); + this.load.image('player-a', 'assets/images/player-a.svg'); }, create: function() { this.asset.cropEnabled = false; -- cgit v1.2.3