Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Collision problem between sprite


Recommended Posts

I just descover phaser, it looks to be a very powerfull framework for HTML5 developpment.
I start a small projet to try phaser, but I have a problem with collion between 2 sprites.
I took the code of the tutorial for making a first game to start.
My game is : platforms are generated randomly and move to the top of the screen.
My "player" sprite ofter passes through my "myPlatforms" group. I try with an Array of sprites instead of a group, but I always get the same issue. I can't find any information about this problem.
Is there something wrong in my code ?
Thanks for your help;
var game = new Phaser.Game(480, 320, Phaser.AUTO, '', { preload: preload, create: create, update: update });function preload() {    game.load.image('sky', 'assets/sky.png');    game.load.image('ground', 'assets/platform.png');    game.load.image('star', 'assets/star.png');    game.load.spritesheet('dude', 'assets/dude.png', 32, 48);}var player;var cursors;var score = 0;var myPlatforms;function create() {    myPlatforms = game.add.group();    player = game.add.sprite(32, game.world.height - 150, 'dude');    //Player physics properties. Give the little guy a slight bounce.    player.body.bounce.y = 0.2;    player.body.gravity.y = 6;    player.body.collideWorldBounds = true;    //Our two animations, walking left and right.    player.animations.add('left', [0, 1, 2, 3], 10, true);    player.animations.add('right', [5, 6, 7, 8], 10, true);    //Our controls.    cursors = game.input.keyboard.createCursorKeys();}var updateTime = 150;var time = 0;function update() {	time++;	player.body.velocity.x = 0;			if (time == updateTime) {				var hole = Math.floor(Math.random() * game.world.width) + 1;		v = myPlatforms.create(hole - 300,  game.world.height, 'ground');		v.body.immovable = true;		v = myPlatforms.create(hole + 200,  game.world.height, 'ground');		v.body.immovable = true;				time = 0;	}	myPlatforms.forEachAlive(function(item) {		item.y--;    });    game.physics.collide(player, myPlatforms);    if (cursors.left.isDown) {        player.body.velocity.x = -150;    } else if (cursors.right.isDown) {        player.body.velocity.x = 150;    }}


Link to comment
Share on other sites

There is definetly a problem with this part :

myPlatforms.forEachAlive(function(item) {		item.y--;    });

I suggest you try with item.body.velocity.y = -100 and you will see it will work.

Another aproach for this game is to move the camera up instead of moving every item in the myPlatforms group down. Also, instead of generating the surfaces based on time, I would use an invisible collider and when the player collides with it, you can generate a number of grounds and another invisible collider and also delete the ones that are not in the screen anymore.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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