Sign in to follow this  

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(){

 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(, 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;, this);
        var coin3 = game.add.sprite(400, 100, 'coin');
        coin3.inputEnabled=true;, this);


getCoin1: function(coin){
       numCoins ++;
      //which coins have been collected
    getCoin2: function(coin){
      numCoins ++;

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 ++;







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.