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

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.

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.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.