Jump to content

BrokenSkyDev
 Share

Recommended Posts

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

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

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

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

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

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

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

 Share

  • Recently Browsing   0 members

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