BrokenSkyDev Posted April 8, 2015 Share Posted April 8, 2015 I am generally quite new to Phaser and Javascript as a whole and have some issues i need clarifying. Issue 1: Audio in my game, my Ship sound is created and started like this in the create function. shipsound = this.game.add.audio('shipsound');shipsound.play(); and should be stopped when the player collides with the finish. nextLevel: function() { shipsound.stop(); powerupCollision = false; powerdownCollision = false; this.game.state.start("MainMenu"); }, But it does not stop and carrys on through to the next state, thoughts? Issue 2: I am currently using multiple states for levels, my first level works fine, not sure if its worth mentioning but i am currently applying velocity to my player in all states in the update function. update: function () { //Player velocity player.body.velocity.x = 250; When loading into my second state where the player should start at - player = new Player(this.game, 20,50); With debug the player starts at around 1461 x position, I have no idea what is causing this issue or how to fix it. If anyone can help in any way it would be appreciated, Thank You! Link to comment Share on other sites More sharing options...
drhayes Posted April 8, 2015 Share Posted April 8, 2015 I don't know for sure, but maybe you're not destroying the player from the previous state before transitioning to the new one? According to the docs, the StateManager will look for a shutDown method for the previous state before starting the next one. Maybe you need to kill the previous player? No idea about the sound one, though, sorry! Link to comment Share on other sites More sharing options...
BrokenSkyDev Posted April 9, 2015 Author Share Posted April 9, 2015 I don't know for sure, but maybe you're not destroying the player from the previous state before transitioning to the new one? According to the docs, the StateManager will look for a shutDown method for the previous state before starting the next one. Maybe you need to kill the previous player? No idea about the sound one, though, sorry!Tried adding a shutdown function which i believe is done like so. shutdown: function() { player.destroy(); shipsound.destroy(); mainText.destroy(); enemy.destroy(); speedpowerup.destroy(); speedpowerdown.destroy();} Still no luck however still spawn about quarter through the next level. Link to comment Share on other sites More sharing options...
BrokenSkyDev Posted April 12, 2015 Author Share Posted April 12, 2015 Is this forum dead? Link to comment Share on other sites More sharing options...
valueerror Posted April 13, 2015 Share Posted April 13, 2015 you don't need to shut down your state.. phaser is doing that for you and clears everything.. concerning your player position problem it's hard to say without seeing the whole code.. you obviously defined some sort of player class somewhere ?? @sound.. in the next level there should be NO shipsound or another shipsound or should it just restart the same shipsound? if so you must set forceRestart to true so the next play() call restarts the shipsound Link to comment Share on other sites More sharing options...
BrokenSkyDev Posted April 13, 2015 Author Share Posted April 13, 2015 you don't need to shut down your state.. phaser is doing that for you and clears everything.. concerning your player position problem it's hard to say without seeing the whole code.. you obviously defined some sort of player class somewhere ?? @sound.. in the next level there should be NO shipsound or another shipsound or should it just restart the same shipsound? if so you must set forceRestart to true so the next play() call restarts the shipsound 1) Okay, i will fix this. My first level seems to be fine so i will just post level 2's code, i stripped it of most functionality to see if something else was causing it , but no. Level 2 Code: BasicGame.level2.prototype ={ create: function () { gameStart = true; //Start Game world Physics this.game.physics.startSystem(Phaser.Physics.ARCADE); this.music = this.game.add.audio('music'); shipcrash = this.game.add.audio('crashsound'); shipsound = this.game.add.audio('shipsound'); shipsound.play(); //Loading Sprites map = this.add.sprite(0,0,'level'); landingstrip = this.add.sprite(5050,375,'landingstrip'); //Creating a group for the buildings enabling buildingGroup = this.game.add.group(); buildingGroup.enableBody = true; buildingGroup.physicsBodyType = Phaser.Physics.ARCADE; this.game.physics.arcade.enable(landingstrip); //Defining the landing strip to be imovable and setting the body size landingstrip.body.immovable = true; landingstrip.body.setSize(200, 150, 110, 50); //Generation of Buildings var left = 0 var right = 350; for(var i = 1; i < 11; i++) { left += 400 right -= 7; var Building = buildingGroup.create(left,right,'levelbuilding'); Building.body.immovable = true; } player1 = new Player(this.game,30,50); speedpowerup = new SpeedPowerUp(this.game, 900 , 75); this.game.camera.follow(player1); this.world.setBounds(0, 0,5333,800); cursors = this.input.keyboard.createCursorKeys(); }, update: function () { //Player velocity //player.body.velocity.x = 250; console.log("Player" + player1.body.x); console.log("Powerup" + speedpowerup.body.x); //All of my collisions that are currently possible this.game.physics.arcade.collide(player1, buildingGroup, this.collisionHandler, null, this); this.game.physics.arcade.collide(player1, speedpowerdown, this.powerDownHandler,null,this); this.game.physics.arcade.collide(player1, speedpowerup, this.powerUpHandler,null,this); this.game.physics.arcade.collide(player1, landingstrip, this.nextLevel,null,this); //Controls if (cursors.up.isDown) { player1.body.velocity.y = -50; } else if (cursors.down.isDown) { player1.body.velocity.y = 50; } else { player1.body.velocity.y = 0 } //If the player has colided with a power up for speeding up if(powerupCollision == true) { powerdownCollision = false; player1.body.velocity.x = 300; } //If the player has colided with a power down for slowing down if(powerdownCollision == true) { powerupCollision = false; player1.body.velocity.x = 75; } }, //Handles collisions that would restart the level collisionHandler: function() { shipcrash.play(); var explosion = this.explosions.getFirstExists(false); explosion.reset(player.body.x + player.body.halfWidth, player.body.y + player.body.halfHeight); explosion.body.velocity.y = enemy.body.velocity.y; explosion.alpha = 0.7; explosion.play('explosion', 30, false, true); //These bools are set to this so the speed is reset powerupCollision = false; powerdownCollision = false; this.game.state.start("level2"); }, //Handles collision with the Speed power up and its cooldown powerUpHandler: function() { powerdownCollision = false; speedpowerup.destroy(); powerupCollision = true; this.game.time.events.add(Phaser.Timer.SECOND * 2, this.powerDown, this); },//Handles collision with the power down. powerDownHandler: function() { powerupCollision = false; speedpowerdown.destroy(); powerdownCollision = true; this.game.time.events.add(Phaser.Timer.SECOND * 3, this.powerDown, this); }, //When a powerup is finished (i think this may need to be cut into two functions) powerDown: function() { powerupCollision = false; powerdownCollision = false; }, //When landing on the strip Main menu nextLevel: function() { shipsound.destroy(); powerupCollision = false; powerdownCollision = false; this.state.start("MainMenu"); }, quitGame: function (pointer) {this.state.start('MainMenu'); } }; I am currently using this Player class: Player = function(_game, _x, _y){ console.log("Creating Player!"); this.game = _game; Phaser.Sprite.call(this, this.game, _x, _y, 'ship'); this.game.add.existing(this); this.game.physics.arcade.enable(this); this.body.collideWorldBounds = true; this.body.setSize(50, 50, 1, 0); this.anchor.setTo(0.5, 1); return this;};Player.prototype = Object.create(Phaser.Sprite.prototype);Player.prototype.constructor = Player; Link to comment Share on other sites More sharing options...
BrokenSkyDev Posted April 13, 2015 Author Share Posted April 13, 2015 My game is also hosted here : http://jbwakeham.bitbucket.org/ If it makes it easier to understand the issue. Link to comment Share on other sites More sharing options...
valueerror Posted April 13, 2015 Share Posted April 13, 2015 oh.. first adivse.. upgrade your phaser version to 2.3 -- yours is outdated.... do you have a link to your repository on bitbucket.. its much easier to find a bug by just running the code on your own machine Link to comment Share on other sites More sharing options...
valueerror Posted April 13, 2015 Share Posted April 13, 2015 With debug the player starts at around 1461 x position,what do you mean by "with debug" do you mean it is positioned wrong when you turn on the physics debug body or what? Link to comment Share on other sites More sharing options...
BrokenSkyDev Posted April 13, 2015 Author Share Posted April 13, 2015 what do you mean by "with debug" do you mean it is positioned wrong when you turn on the physics debug body or what?"console.log("Player" + player1.body.x);" sorry meant console log, tells where the player is currently positioned along x axis. Link to comment Share on other sites More sharing options...
valueerror Posted April 13, 2015 Share Posted April 13, 2015 ah oke.. i see the logs.. your game is also never stopping.. even if the browser window loses focus Link to comment Share on other sites More sharing options...
BrokenSkyDev Posted April 13, 2015 Author Share Posted April 13, 2015 ah oke.. i see the logs.. your game is also never stopping.. even if the browser window loses focus Turns out i needed to zero out the world bounds, before loading into the next state. Link to comment Share on other sites More sharing options...
valueerror Posted April 13, 2015 Share Posted April 13, 2015 Turns out i needed to zero out the world bounds, before loading into the next state. out of curiosity.. you "zeroed" out the worldbounds? how? to get what exactly? Link to comment Share on other sites More sharing options...
BrokenSkyDev Posted April 14, 2015 Author Share Posted April 14, 2015 out of curiosity.. you "zeroed" out the worldbounds? how? to get what exactly? this.world.setBounds(0,0,0,0); It fixed my issue. Link to comment Share on other sites More sharing options...
valueerror Posted April 15, 2015 Share Posted April 15, 2015 you mean the issue of the wrong x setting for your airplane after switching states... sorry to say that but you should -not- have to do that.. there is something completely odd going on with your setup.. i've never seen this behaviour before and never had problems with unclean state swaps.. but if you found a workaround.. good.. lets just hope that this the only one just one last question.. did you write the game prototype definition and the rest of the basic strukture on your own or did you build upon a basic code from some tutorial ? Link to comment Share on other sites More sharing options...
BrokenSkyDev Posted April 15, 2015 Author Share Posted April 15, 2015 you mean the issue of the wrong x setting for your airplane after switching states... sorry to say that but you should -not- have to do that.. there is something completely odd going on with your setup.. i've never seen this behaviour before and never had problems with unclean state swaps..but if you found a workaround.. good.. lets just hope that this the only one just one last question.. did you write the game prototype definition and the rest of the basic strukture on your own or did you build upon a basic code from some tutorial ? The Prototype definition and structure and rest of the structure was packaged in with my University template as this project is for a module at University. Link to comment Share on other sites More sharing options...
Recommended Posts