Jump to content

Sprite follow doesn't work correctly


eduardonunesp
 Share

Recommended Posts

I have started a platform game very simple, just to teaching myself, following examples and tutorials. Already searched too much the annoying problem of camera follows, because my camera doesn't follow the player correctly. The problem is the sprite player move left to right and the camera start moving but not in the same speed, so the camera doesn't center and the sprite moving far from center to right, also the cameras still moving, but can't follow the sprite speed.

function GameState() {  Phaser.State.call(this);}GameState.prototype = Object.create(Phaser.State.prototype);GameState.prototype.constructor = GameState;GameState.prototype.preload = function() {  'use strict';  this.game.load.tilemap('map', 'static/map1.json', null, Phaser.Tilemap.TILED_JSON);  this.game.load.image('tiles', 'static/tiles.png');  this.game.load.spritesheet('hero', 'static/hero.png', 34, 38, 14);};GameState.prototype.create = function () {  'use strict';  console.log('create');  // start physics system  this.game.physics.startSystem(Phaser.Physics.ARCADE);  this.game.world.setBounds(0, 0, 6400, 640);  this.game.physics.arcade.gravity.y = 1000;  this.player = new Player(this, 40, 4);  this.game.stage.addChild(this.player);  this.game.camera.follow(this.player);  var map = this.game.add.tilemap('map');  map.addTilesetImage('tiles1', 'tiles');  this.layer1 = map.createLayer('layer1');  this.layer1.debug = true;  this.layer1.resizeWorld();  this.layer2 = map.createLayer('layer2');  this.layer2.resizeWorld();  map.setCollision(130, true, "layer1", true);};GameState.prototype.update = function() {  'use strict';};GameState.prototype.render = function() {  'use strict';};
function Player(game_state, x, y) {    Phaser.Sprite.call(this, game_state.game, x, y, 'hero');    this.game_state = game_state;    this.states = {      WALKING : 'WALKING',      IDLE : 'IDLE'    };    this.facing = 'left';    this.life = 0;    this.isAlive = false;    this.name = 'Player';    this.currState = this.states.IDLE;    console.log('Add player');    this.animations.add('idle', [1], 0);    this.animations.add('walk', [0, 1], 6, true);    this.animations.play('idle');    this.game_state.game.physics.enable(this, Phaser.Physics.ARCADE);    this.body.collideWorldBounds = true;    this.anchor.setTo(1);    this.cursors = this.game_state.game.input.keyboard.createCursorKeys();}Player.prototype = Object.create(Phaser.Sprite.prototype);Player.prototype.constructor = Player;Player.prototype.changeState = function(newState) {  if (newState === this.currState) {    return;  }  this.currState = newState;  console.log('Change state', newState);  switch(newState) {    case 'IDLE':      this.animations.play('idle');      break;    case 'WALKING':      this.animations.play('walk');      break;  }};Player.prototype.update = function() {  //this.game_state.game.physics.arcade.collide(this.player, this.layer1);  if (this.cursors.left.isDown) {      this.scale.x = 1;      this.body.velocity.x = -200;  }  if (this.cursors.right.isDown) {      this.scale.x = -1;      this.body.velocity.x = 200;  }  if (this.cursors.right.isDown || this.cursors.left.isDown) {    this.changeState('WALKING');  } else {    this.body.velocity.x = 0;    this.changeState('IDLE');  }};

post-17272-0-72820400-1446984087.png

post-17272-0-77668500-1446984095.png

post-17272-0-85913400-1446984106.png

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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