Jump to content

Group will only Create() one Sprite


RyanTheBoy
 Share

Recommended Posts

I am trying to create a simple meter to keep the beat for a launcher game I am developing. For some reason when I call the group's create function(this.bugs.create()) in my update loop it works once then never again.

 

Why is it not continually adding sprites? Am I wrong in thinking it should? This is incredibly frustrating and I wondered if anyone may be able to spot the problem in my logic below.

 STATES.Game = function(game) {    this.meter = null;    this.bugs = null;    this.timer = null;    this.pattern = new Array(3);    this.initTime = null;    this.currentTime = null;    this.currentBeat = null;    this.elapsedSec = 0;};var spaceBar = null;var BPM = 96;var beep = null;var bugAdded = false;STATES.Game.prototype = {    create: function () {        this.initTime = Math.floor(this.game.time.time);    // Sets initial time to number of milliseconds elapsed since game                                                            // game began. We can manually manipulate our level timers using                                                            // this as a base.        beep = this.game.add.audio('hit1', 0.25);        this.meter = this.add.sprite(0, 0, 'meter');        this.bugs = this.game.add.group();        spaceBar = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);        for (var i = 0, ii = this.pattern.length; i < ii; i++) {            this.pattern[i] = 4 * (i + 1);        }    },    update: function () {        this.currentTime = this.elapsedSec * 1000 + (this.game.time.time - this.initTime);        if (this.game.time.time - this.initTime >= 1000) {            this.elapsedSec++;            this.initTime = Math.floor(this.game.time.time);        }        // Measures beats with sixteenth note precision        this.currentBeat = Math.floor((this.currentTime / 1000) * BPM * (1/60) * 4);        this.currentBeat %= 16;        if (!bugAdded) {            if (this.currentBeat % 4 == 0) {                console.log(this.currentBeat + "\n" + this.currentTime);                this.bugs.create(512, 0, 'bug');                bugAdded = true;            }        } else if (this.currentBeat % 4 != 0) {            console.log('test');            bugAdded = false;            }        this.bugs.x -= 5;        if (this.bugs.getAt(0).body.x <= 0) this.bugs.getAt(0).destroy();        if (spaceBar.isDown) console.log(this.pattern);    },    quitGame: function (pointer) {        //    Here you should destroy anything you no longer need.        //    Stop music, delete sprites, purge caches, free resources, all that good stuff.        //    Then let's go back to the main menu.        this.game.state.start('MainMenu');    }}; 

In this portion of the update loop...

 if (!bugAdded) {            if (this.currentBeat % 4 == 0) {                console.log(this.currentBeat + "\n" + this.currentTime);                this.bugs.create(512, 0, 'bug');                bugAdded = true;            }        } else if (this.currentBeat % 4 != 0) {            console.log('test');            bugAdded = false;            } 

It always triggers the console.log() of both if() statements but only ever adds the one sprite to the group. WHY?!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...