Jump to content

Problem with collision


Recommended Posts

Hi all i have a big problem with collision, i don't know why because the code is ok there is no error.


Here is the code :

create: function () {				cursors = this.game.input.keyboard.createCursorKeys();		speed = 1;		score = 0;				back = this.game.add.tileSprite(0,0,320,480,'background');				back2 = this.game.add.tileSprite(0,412,320,480,'groundGrass');		back2.enableBody = true;						this.game.physics.startSystem(Phaser.Physics.ARCADE);				coins = this.game.add.group();		coins.enableBody = true;		bombs = this.game.add.group();		bombs.enableBody = true;		player = this.game.add.sprite(100,230,'player');		player.anchor.setTo(0.5,0.5);		this.game.physics.arcade.enable(player);		player.body.gravity.y = 300;						this.time.events.loop(500, this.createCoin, this);		this.time.events.loop(2000, this.createBomb, this);			},		update: function(){			player.body.velocity.x = 0;						if (typeof bomb !== 'undefined'){                    this.game.physics.arcade.overlap(player, bomb, this.bombed, null, this);                }		if (typeof coin !== 'undefined'){                    this.game.physics.arcade.overlap(player, coin, this.getCoin, null, this);                }		//Scrolling background		back.tilePosition.x -= speed;		back2.tilePosition.x -= speed*2;				if (cursors.up.isDown){			if (player.body.velocity.y > -500){player.body.velocity.y -= 10;}					}			},	createCoin: function(){		posY = this.game.rnd.integerInRange(64, 420);		coin = coins.create(320,posY,'coin');		coin.body.velocity.x = -speed*200;	},	getCoin: function(_player,_coin) {                score++;		_coin.kill();	},	createBomb: function() {		posY = this.game.rnd.integerInRange(64, 420);		bomb = bombs.create(320,posY,'bomb');		bomb.body.velocity.x = -speed*400;	},		bombed: function(){		console.log('GAME OVER');	}

The bomb's collision is fine but the coin one don't work, is there any problem ???

Link to comment
Share on other sites

My guess is it is because you are only storing the refence to a single coin and single bomb...

As you create a coin every 500ms, the reference that you check in the overlap function would always point to the newest one, rather than the one you are trying to collect...

Bombs are created far less often, and as such have less change to have the reference overwritten...


You will either:

 - need to store references to each created coin (and bomb) and loop over those checking overlap

 - or, Use the groups references to all its children and loop that instead

 - OR, change the overlap function to check the group for overlaps, which in turn checks all the groups children...



I'd go with this:

 /* Change this part    if (typeof bomb !== 'undefined'){        this.game.physics.arcade.overlap(player, bomb, this.bombed, null, this);    }    if (typeof coin !== 'undefined'){        this.game.physics.arcade.overlap(player, coin, this.getCoin, null, this);    }*/ // To this:     this.game.physics.arcade.overlap(player, bombs, this.bombed, null, this);    this.game.physics.arcade.overlap(player, coins, coin, this.getCoin, null, this);
Link to comment
Share on other sites


  • Recently Browsing   0 members

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