Sign in to follow this  
casey

getting name of this variable

Recommended Posts

I am trying to create an adventure game. Very new to Phaser and Js. I've been through multiple tutorials and am now trying to make my way on my own. 

If you imagine a stage that can be returned to at any time, I need to know if the coins on that stage were collected before. I can write individual functions for each coin successfully:

var State1 = {
preload: function(){
 game.load.image('coin','coin.png');
},

 create: function(){
//note these vars are declared in Main state
        numCoins = 0;
//the array that will tell me if the coin has already been collected. that check hasn't been written yet. 
        coinsCollected = [];

        this.coinText =game.add.text(game.world.width-20, 30, numCoins, {font: "26px Amatic SC"});
       

//single coins to collect on the stage. trying 2 different ways to show, as button and as sprite

 this.coin1=game.add.button(600, 300, 'coin', this.getCoin1, this);

 var coin2 = game.add.sprite(350, 250, 'coin');
        coin2.inputEnabled=true;
        coin2.events.onInputDown.add(this.getCoin, this);
        var coin3 = game.add.sprite(400, 100, 'coin');
        coin3.inputEnabled=true;
       coin3.events.onInputDown.add(this.getCoin, this);

 },


getCoin1: function(coin){
       numCoins ++;
       this.coinText.text=numCoins;
               
      //which coins have been collected
       coinsCollected.push('coin1');
       console.log(coinsCollected);
          coin.kill();
        
    },
    
    getCoin2: function(coin){
      numCoins ++;
      this.coinText.text=numCoins;
       coinsCollected.push('coin2');
       console.log(coinsCollected);
          coin.kill();
        
    },




So that works fine, but obviously I don't want to write out a hundred "getCoin" functions. 

How do I pass the name of the coin to a single getCoin function so I can store it in an array?  

so getCoin: function(coin){

numcoins ++;

this.coinText.text=numCoins;

coinsCollected.push(coinNAME);

}

 

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.