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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.