Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by zimett

There is probably a very simple answer to this, but I'm teaching myself some game programming and have hit my first real speed bump.

In my main.js file I'm creating a Stats Screen that shows how long you've been playing and how many mouse clicks you've made.

Here is my Stat Screen code in the Draw function of main.js. It works fine if I comment out the line tracking 'Clicks' - the offending piece of code is definitely 'EntityMonster.clicks'

	if(this.showStats){
            this.statMatte.draw(0,0);
            var x = ig.system.width / 2;
            var y = ig.system.height/2 - 20;
            this.statText.draw('Level Complete', x,y,ig.Font.ALIGN.CENTER);
            this.statText.draw('Time Played: ' + Math.floor(this.stats.time), x, y+30, ig.Font.ALIGN.CENTER);
            this.statText.draw('Clicks: ' + EntityMonster.clicks, x, y+40, ig.Font.ALIGN.CENTER);
            this.statText.draw('Press TAB to continue.', x, y+50, ig.Font.ALIGN.CENTER);
        }

If I leave 'EntityMoster.clicks' in the code the stat screen opens and immediately closes when called and doesn't show anything after the Time Played.

Here is my EntityMonster code:

    animSheet: new ig.AnimationSheet('media/monster.png', 100, 129),
    size: {x:100, y:129},
    offset: {x:0, y:0},
    clicks: 0,

    init: function(x, y, settings){
        this.parent(x, y, settings);
        this.addAnim('idle', 1, [1]);
        this.addAnim('clicked', 1, [0]);

    },

    update: function (){
        if (ig.input.state('leftButton') && this.inFocus()) {
            this.currentAnim = this.anims.clicked;
        }
        else{
            this.currentAnim = this.anims.idle;
        }
        if (ig.input.pressed('leftButton') && this.inFocus()) {
           this.clicks++;
        }


        this.parent();
    },

    inFocus: function() {
        return (
            (this.pos.x <= (ig.input.mouse.x + ig.game.screen.x)) &&
                ((ig.input.mouse.x + ig.game.screen.x) <= this.pos.x + this.size.x) &&
                (this.pos.y <= (ig.input.mouse.y + ig.game.screen.y)) &&
                ((ig.input.mouse.y + ig.game.screen.y) <= this.pos.y + this.size.y)
            );
    }

Basically, I want to increment the clicks by 1 whenever the player clicks the monster, and be able to call that property in the stats screen. I'm guessing I've made a really rookie scope mistake somewhere, but I can't figure out what I need to do after an hours worth of forum searching.

Any help would be really appreciated!

1 decade ago by mahgo

The way you're doing it is storing a clicks variable in every monster object, which I presume is not what you want to do. If you did, however, to get that variable, you'd have to get reference of the particular Monster object, and get the variable from it.

At the moment you're trying to get a non-static variable, statically (if that makes sense).

I'd recommend reading up on Object-Oriented Programming.

1 decade ago by zimett

I fixed it by doing this:

In Main.js
ig.global.clicks = 0;
ig.main( '#canvas', StartScreen, 60, 500, 400, 1 );

In EntityMonster
        if (ig.input.pressed('leftButton') && this.inFocus()) {
            ig.global.clicks++;
        }

That got me the global variable I was looking for!

Here's the post that helped: http://impactjs.com/forums/help/super-global-variable-thats-available-for-all-property-definitions/page/1
Page 1 of 1
« first « previous next › last »