Jump to content

Move in sqm way


Recommended Posts



Im trying to learn Phaser but im stucked with a simple task... Im trying to move a sprite with size of 40x40px in a tilemap from 20 tiles width and 15 tiles height (each tile is 40x40px too), in a "sqm" way (like turn based rpgs), but I have strange results in all my tries...


Try 1: Tween.

It works on the first time, and dont execute again... ;(


Try 2: Physics.

This guy make my sprite run forever! Tried in diferent ways, but it ever runs forever when the movement start...


Try 3: Move with simple sprite.x+=40. (some use in last case... i want to avoid the "jump" movement...)

I dont understand why but it dont move some 1 tile, if each tile is 40px it should move 1 tile right?


Any tip to fix one of this? Or another way to move 1 sqm per time?


Sorry for bad english... =/


Resumed code:


var PP = PP || {};

PP.Game = function() {};

var largDes=800;
var altDes=600;
var largBds=200;
var altBds=altDes/2;
var largBdi=200;
var altBdi=altDes/2;
var dTween;
var down;
var map = [[0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0],
var upd=1;

PP.Game.prototype = {
create : function()

this.planet = this.game.make.bitmapData(largDes, altDes);
this.bmdSprite = this.planet.addToWorld(0, 0);
this.planet.ctx.fillStyle = (255,0,0);
this.planet.ctx.fillRect(0, 0, largDes, altDes);

for(var i=0;i<15;i++)
for(var j=0;j<20;j++)
this.game.add.sprite(j*40, i*40, 'cenario',mapa[i][j]);

//create player
this.player = this.game.add.sprite(160, 160, 'player');

dTween = this.game.add.tween(this.player).to({x:this.player.x+40,y:this.player.y},1000, Phaser.Easing.Linear.None, false);

this.playerSpeed = 0;
this.player.body.collideWorldBounds = true;

update : function()
if(this.game.input.activePointer.justPressed()) {
// this.upd=0;
// if(this.upd==0)
// {
//move on the direction of the input
/////////////PHYSICS TRY!!//////////////////
//this.game.physics.arcade.moveToXY(this.player, this.player.x, this.baixo);
/////////////TWEEN TRY/////////////////////
/////////////SIMPLE SPRITE MOVEMENT////////
// }
// this.upd=1;

Link to comment
Share on other sites

Hello guys, I'm back to share how i solved my problems (maybe this can be useful for someone :P).


Problem 3 - sprite.x+40; dont move 40px


After a lot of tries i realized that the problem was that this:

update : function()    {             if(keyboard.left.isDown)         //yep, i changed mouseinput...                      this.player.x+=40;    }

was moving my player 3 times (120px)... I really dont understand why... =/

I've included a variable "pressed" and a test (keyboard.left.isUp? to change pressed to false) and... Solved! :) I'm happy for this solution but i really would be glad if anyone explain me what is happening here...
Problem 2 - Move player 40 px with physics enabled move him forever!
I've added:
this.game.physics.arcade.moveToXY(this.player, this.player.x+40, this.player.y);
Inside a test with the players goal...
if(keyboard.left.isUp)        {            pressed=0;                 if(this.player.x==this.newpos)                this.player.body.moves=false;          }if (keyboard.left.isDown && !pressed) {                  pressed=1;                  this.player.body.moves=true;                    this.newpos=this.player.x+40;                  this.game.physics.arcade.moveToXY(this.player, this.player.x+40, this.player.y);}

I dont tried to solve Problem 1 again because this solutions are enought for my needs... ;)

Link to comment
Share on other sites


  • Recently Browsing   0 members

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