RyanTheBoy Posted March 30, 2014 Share Posted March 30, 2014 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 More sharing options...
clark Posted March 30, 2014 Share Posted March 30, 2014 Edited: Sorry you are right, I got confused. Link to comment Share on other sites More sharing options...
RyanTheBoy Posted March 30, 2014 Author Share Posted March 30, 2014 Hey Is it possible it is because create() is a method name already of the Group class? Phaser.Group.prototype.create = function (x, y, key, frame, exists) I don't quite understand what you mean: I am not redefining create, I am calling the class's function. Link to comment Share on other sites More sharing options...
Dream Of Sleeping Posted March 30, 2014 Share Posted March 30, 2014 edit: just ignore me I'm talking crap. Link to comment Share on other sites More sharing options...
JP91 Posted March 30, 2014 Share Posted March 30, 2014 is the simple fact is that creating these one above the other Link to comment Share on other sites More sharing options...
RyanTheBoy Posted March 30, 2014 Author Share Posted March 30, 2014 I knew it was something stupid obvious. Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts