Jump to content

PhaserJS - collide and call back


xronn
 Share

Recommended Posts

Hi,

 

So i'm trying to make it so when my player collides with an element it will callback to another function where I will run my magic;

        //Coins        coinGroup = this.add.group();        coin = coinGroup.create(163, 228, 'coin');        coin1 = coinGroup.create(163, 208, 'x3coin');        coin2 = coinGroup.create(500, 228, 'coin');        coin3 = coinGroup.create(500, 208, 'x3coin');        var score = 5;

Create function, adding and positing my coins

 

//Coin Collectgame.physics.collide(hero, coinGroup, coinPickup, null, this); 

Update function, the collide check and call back to the function

coinPickup: function(){   score  = 50;}

The callback function

 

 

error - 

https://www.dropbox.com/s/vit43bnc2ew9txs/Screenshot%202014-02-25%2000.03.57.png

Link to comment
Share on other sites

From the code you send the only thing I see is that you create your coinPickup function as an object notation, but I don't think you used it in the rest of your game.

 

What id you replace 

coinPickup: function(){

by

coinPickup= function(){

?

Link to comment
Share on other sites

Line 186 is this :

186:   quitGame: function (pointer) {        //  Here you should destroy anything you no longer need.        //  Stop music, delete sprites, purge caches, free resources, all that good stuff.        //  Then let's go back to the main menu.        this.this.state.start('MainMenu');    }

But this function was fine until I added the coinPickup function so not sure why this would cause any issue

Link to comment
Share on other sites

Okay, that removed that error, I got the menu screen up with no errors but when I click to load my game state it throws this error:
 

Uncaught ReferanceError: coinpickup is not defined game.js:130

Line 130 is this - 

this.physics.collide(hero, coinGroup, coinPickup, null, this);
Link to comment
Share on other sites

One final question, I'm updating the score now in this function when the player hits a coin using phaser 1.1.3 i see theres a remove sprite function. I'm trying to remove the sprite the player steps on so far I got this;

 

        //Coins        coinGroup = this.add.group();        coin = coinGroup.create(163, 228, 'coin');        coin1 = coinGroup.create(163, 208, 'x3coin');        coin2 = coinGroup.create(500, 228, 'coin');        coin3 = coinGroup.create(500, 208, 'x3coin');                //Coin Collect        this.physics.collide(hero, coinGroup, this.coinPickup, null, this); 	},    coinPickup: function(){        score += Math.floor((Math.random()*10)+1);        scoreText.setText(score);        this.coinGroup.remove();    },

this.coingroup.remove();

Throws out this error:

Uncaught TypeError: Cannot call method 'remove' of undefined

game.js:165

Line 165 is:
this.coinGroup.remove();

Link to comment
Share on other sites

Your problem is probably scoping.

coinGroup = this.add.group();

This is either going to be a global var, or if you defined it earlier, it'll be local to the (create?) function.

That makes it completely different to the variable you are trying to reference here:

this.coinGroup.remove();

That one is looking at the (state?) object that the function is called on, and trying to get the group there. It is not there, because you never added it to the (state?) object.

 

jflowers45's suggestion should work, provided you complete the remove function call. You will need to tell the group what to remove.

 

The collision function is passed 2 parameters from the Phaser Engine that correspond to the colliding objects, in the order you put them in the collision function.

In this case, you would have:

coinPickup: function(hero, coin){

Note that coin is the sprite object involved in the collision - the one you are picking up - not the coin group.

 

(Things in brackets with ? are assumptions about your code)




			
		
Link to comment
Share on other sites

  • 5 months later...

Ehm accidentally sent an unfinished post, used to the code hotkeys now ^^

I'll summarize this time. So the problem is; sometimes the order of the colliding objects are wrong.
 

update: function() {    //this.player is a group with two sprites    //this.effects is a list, it consists of a particle emitter and just a sprite    this.physics.arcade.overlap(this.player, this.effects, this.Death, null, this);},Death: function(p, effect) {    console.log(p, effect);}

When it's the emitter it logs the player sprite first, then the emitted sprite. When it's the sprite effect, for some reason it logs that first and the player sprite becomes the effect parameter.
So... it feels you probably need more to help me but if you have any idea that would be great!

Cheers!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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