ducntq Posted July 4, 2014 Share Posted July 4, 2014 I'm trying to do a small example, with AMD loading, but somehow the platforms move along with the player, even I set their immovable property to false. How do I make the platforms stay still? The example url: http://128.199.251.170:1337/ The code Entry script:var requireConfig = { paths: { phaser: '/vendors/phaser/phaser.min', jquery: '/vendors/jquery/jquery-1.11.1.min', socketio: '/js/socket.io', sailsio: '/js/sails.io', game: '/models/Game', player: '/models/Player' }, shim: { 'phaser': { exports: 'Phaser' } }};requirejs.config(requireConfig);require(['jquery', 'phaser', 'game', 'player'], function($, Phaser, Game, Player) { var game = new Game({ created: function () { var player = new Player(game, 'blue', 100, 350); game.addLocalPlayer(player); }});});Game.jsdefine([ 'phaser'], function (Phaser) { 'use strict'; var localPlayer, remotePlayers, options, platforms, cursors, GAME_WIDTH = 800, GAME_HEIGHT = 400; function Game(opts) { options = opts; Phaser.Game.apply(this, [GAME_WIDTH, GAME_HEIGHT, Phaser.CANVAS, "game", {preload: preload, create: create}]); } function preload() { this.load.image('ground', 'assets/tiles/grassHalfMid.png'); this.load.atlasJSONHash('blue', 'assets/players/blue/p1_walk.png', 'assets/players/blue/p1_walk.json'); } function create() { this.stage.backgroundColor = '#ffffff'; this.physics.startSystem(Phaser.Physics.ARCADE); platforms = this.add.group(); platforms.enableBody = true; for (var i = 0; i < 12; i++) { var ground = platforms.create(70 * i, 360, 'ground'); ground.body.immovable = true; } cursors = this.input.keyboard.createCursorKeys(); if (options.created) { options.created(this); } } Game.prototype = Phaser.Utils.extend(true, Phaser.Game.prototype); Game.prototype.constructor = Game; Game.prototype.getPlatforms = function () { return platforms; }; Game.prototype.getCursors = function () { return cursors; }; Game.prototype.addLocalPlayer = function(p) { localPlayer = p; this.add.existing(p); this.physics.arcade.collide(p, platforms); }; return Game;});Player.jsdefine(['phaser'], function (Phaser) { 'use strict'; var game, cursors; function Player(theGame, color, x, y) { game = theGame; Phaser.Sprite.apply(this, [game, x, y, color]); game.physics.arcade.enable(this); this.body.bounce.y = 0.2; this.body.gravity.y = 300; this.body.collideWorldBounds = true; this.anchor.setTo(.5, 1); this.animations.add('run'); cursors = game.getCursors(); } Player.prototype = Phaser.Utils.extend(true, Phaser.Sprite.prototype, PIXI.Sprite.prototype); Player.prototype.constructor = Player; Player.prototype.update = function() { var platforms = game.getPlatforms(); game.physics.arcade.collide(this, platforms); this.body.velocity.x = 0; if (cursors.left.isDown) { // Move to the left this.body.velocity.x = -150; this.scale.x = -1; this.animations.play('run'); } else if (cursors.right.isDown) { // Move to the right this.body.velocity.x = 150; this.scale.x = 1; this.animations.play('run'); } else { // Stand still this.animations.stop(); this.frame = 0; } // Allow the player to jump if they are touching the ground. if (cursors.up.isDown && this.body.touching.down) { this.body.velocity.y = -150; } }; return Player;}); Link to comment Share on other sites More sharing options...
Recommended Posts