Jump to content

sprite stop moving weirdly


aladine
 Share

Recommended Posts

Hello everyone, 

i was trying to do a little practice after i have finished the platformer tutorial, and am stacked with a weird problem, the sprite just move a little bit then it stops moving at all, even that the X velocity  is different from zero, 

here is a link to see the problem : 

https://dl.dropboxusercontent.com/u/88553415/phaser/PhaserProject1/index.html  

the code is there but, here you go : 

<script type="text/javascript">var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });function preload() {	game.load.image('blue','assets/blue.png');	game.load.image('sky','assets/sky.png');	game.load.spritesheet('cat', 'assets/myCat.png', 100, 100);		}var speed = 250;function create() {	//sky	sky = game.add.sprite(0,0,'sky');	//platform 	platforms = game.add.group();		for(var i=0;i<32*25;i+=32){			var ground = platforms.create(i,game.world.height-32,'blue');			ground.body.immovable = true ;		}	//the cat 	player = game.add.sprite(32,game.world.height-250,'cat');	player.anchor.setTo(.5,.5);		player.animations.add('run',[0,1,2],10,true);	player.animations.add('jump',[3],true);	player.animations.add('idle',[0],true);		player.body.gravity.y = 6;	player.body.bounce.y = 0.01;	player.body.collideWorldBounds = true;}function update() {	game.physics.collide(player,platforms);					if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT) ) {           player.animations.play('run');           player.scale.x =-1;           player.body.velocity.x =-250;           console.log(player.body.velocity.x)        }        else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT) ) {           player.animations.play('run');           player.body.velocity.x =250;           player.scale.x =1;            console.log(player.body.velocity.x)        }        else if (game.input.keyboard.isDown(Phaser.Keyboard.UP) ) {           player.animations.play('jump');           player.body.velocity.y = -250;        }else{        	player.body.velocity.x = 0 ;        	player.animations.play('idle');        }}</script>

thank you very much 

Link to comment
Share on other sites

There seems to be an issue in your code with your player colliding with the platform, if you comment out line 58, you can see that the cat runs fine. Maybe show some bounding-box debugging info about your player and each ground object to visually see whats going on

Link to comment
Share on other sites

There seems to be an issue in your code with your player colliding with the platform, if you comment out line 58, you can see that the cat runs fine. Maybe show some bounding-box debugging info about your player and each ground object to visually see whats going on

yeah i was thinking abut that either, i didn't know how to make the bounding-box debugging thing but i tried running the game outside the server (so it doesn't load images) and it seems that everything is "fine"

thank you 

Link to comment
Share on other sites

I didn't find anything wrong, weird. It's kinda similar to the new example Rich posted.

 

What I did and seems to "solve" the problem was instead of creating a lot of squares to make the ground, I edited the image so only one sprite of 800 width (the game width) was there. 

 

What I changed in your code was this, in order to create only one: 

	//platform 	platforms = game.add.group();	ground = platforms.create(0,game.world.height-32,'blue');	ground.body.immovable = true ;

'blue' was the image that I edited:

aisz.png

 

And you can use the render function to debug, if you want. Then you can render the sprite body of your image, so you can see what is the size of your colliding box.

 

You just need to change this:

var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render });

Change the Phaser.AUTO to Phaser.CANVAS, because it won't render otherwise. And then you just add "render: render" after "update: update";

 

Then you can do something like this, as shown in the starstruck example:

 

function render () {    game.debug.renderSpriteBody(player);}
Link to comment
Share on other sites

@Martiny

this cannot be a solution for the problem, we can't always use an image equal to the game frame, what if we want to make a tile based game or anything that need something like i did in the op, i think we need Rich intervention here  ;)

and thank you for the render method information

Link to comment
Share on other sites

update:

i change the player sprite, am using a single 32x32 png and i also tried to do the same thing as the code above does but with an array of "ground" instead of phaser group, and now the player get stacked only when it goes to the left, even that the console log show that the player velocity is -250 when the left arrow is pressed, 

i also added another platform just like PhotonStorm did in their tutorial and it worked fine, so i want to know if it is obligatory to do it that way ? 

cause am trying to port and ameliorate a level editor that i made with Java half year ago and having the ability to control each tile of the map is something critical.

 

Link for the new build :

https://dl.dropboxusercontent.com/u/88553415/HTML5/PhaserProject2/index.html

 

PS :

is it okay to have the debug rectangle of the body shifted a little bit when the sprite is moving ? 

Capture.jpg

 

thank you all 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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