Jump to content

Function organisastion help


scope2229
 Share

Recommended Posts

Hi I've been trying to learn phaser3 using ruby on rails and so far i managed to make a very small scale game but now ive managed to learn more about the bootState preloadState gameState etc and have managed to organise my file much more efficently as such

setup.js // this imports all the neccessary files for the game in the below order.  (i built one rails site that hosts many of my games rather than keep building a new one)

Globals/globalvariables == Holds all variable for the game from player1 to creating the backgrounds

boot == sets further config for the game loading preloader logo

GameUi/preload == Loads all assets inside the game. Displays a progress bar with % complete and the current file loading 

GameUi/mainMenu == Choose whether to goto settings scoreboard or play a new game

GameUi/scoreBoard == History of score

GameUi/settings == game settings volume, screen size, language.

Levels/lvl1 == bit obvious

GameUi/gameOver == same

 

after figuring all this out i managed to get a ok ish game to run. But now i have come into a problem. I want to have multiple weapons and those weapons have a bullet associated with them. looking at the defender example i decided to go with a class inside the create function called friendlyWeaponSelect which would increase the weapons damage depending on the weapons xp. as is with the code the way it is my game loads the lvl opens i can move around but the second i hit spacebar the game crashes and i get no reports as i have to reboot to ffix the crash. If someone could help that would be great. heres a look at what im trying.

 

 

var lvl1State = new Phaser.Class({
  Extends: Phaser.Scene,
  initialize:
  function lvl1(){
    Phaser.Scene.call(this, {key:"lvl1"});
  },
  create: function(){
    this.starfield = this.add.tileSprite(400,300,800,4000, 'starfield');
    this.juststars = this.add.tileSprite(400,300,800,4000, 'juststars');
    this.nebula1 = this.add.tileSprite(400,300,800,4000, 'nebula1');
    this.stars2 = this.add.tileSprite(400,300,800,4000, 'juststars');
    this.nebula2 = this.add.tileSprite(400,300,2000,4000, 'nebula2');

    friendlyWeaponSelect = new Phaser.Class({
      Extends: Phaser.GameObjects.Image,
      initialize:
      function Weapon (scene){
        //first select the startingGun with image and set its damage
        weaponSelect = startingGun;
        bulletSelect = laserLvl1;
        laserLvl1 = Phaser.GameObjects.Image.call(this, scene, 0, 0, 'laserLvl1');
        laserLvl1DMG = 5;
        if(startingGunXP = 10){
          laserLvl1DMG = 10;
        }else if (startingGunXP = 20){
          laserLvl1DMG = 15;
        }else if (startingGunXP = 30){
          laserLvl1DMG = 20;
        }else if (startingGunXP = 30){
          laserLvl1DMG = 25;
        }else if (startingGunXP = 40){
          laserLvl1DMG = 30;
        }else if (startingGunXP = 50){
          laserLvl1DMG = 35;
        }else if (startingGunXP = 60){
          laserLvl1DMG = 40;
        }else if (startingGunXP = 70){
          laserLvl1DMG = 45;
        }else if (startingGunXP = 80){
          laserLvl1DMG = 50;
        }else if (startingGunXP = 90){
          laserLvl1DMG = 55;
        }else if (startingGunXP = 100){
          weaponSelect = startingGunLvl1;
          bulletSelect = laserLvl2;
          laserLvl2 = Phaser.GameObjects.Image.call(this, scene, 0, 0, 'laserLvl1');
        }else if (startingGunLvl1XP = 10){
          laserLvl2DMG = 75;
        }

        this.speed = 0;
        this.born = 0;
      },
      fire: function (player1){
        this.setPosition(player1.x, player1.y);
        this.speed = Phaser.Math.GetSpeed(-1000 + player1.vel.y, 1);
        this.born = 0;
      },
      update: function (time, delta){
        this.y += this.speed * delta;
        this.born += delta;
        if (this.born > 1000){
          this.setActive(false);
          this.setVisible(false);
        }
      }
    });//End of friendlyWeaponSelect

    this.createStarfield();
    //this.createEnemies();
    this.createBulletEmitter();

    //set weapon and bullets used.
    this.bullets = this.add.group({ classType: friendlyWeaponSelect, runChildUpdate: true });

    //create player1 for now but update later for the option of player 2
    player1 = this.physics.add.sprite(32, config.height - 150, "ship");
    player1.setCollideWorldBounds(true);
    this.cursors = this.input.keyboard.createCursorKeys();
  },//end of create
  update: function(time, delta){
    player1.setVelocity(0,0);
    if (cursors.left.isDown){
      player1.setVelocityX(-160);
    }
    else if (cursors.right.isDown){
      player1.setVelocityX(160);
    }
    else{
      player1.setVelocityX(0);
    }
    if (cursors.up.isDown){
      player1.setVelocityY(-150);
    }
    else if (cursors.down.isDown){
      player1.setVelocityY(+150);
    }

    //  Emitters to bullets
    this.bullets.children.each(function(b) {
      if (b.active)
      {
        this.laserEffect1.setPosition(b.x, b.y);
        this.laserEffect1.setSpeed(b.speed + 500 * -1);
        this.laserEffect1.emitParticle(1);
      }
    }, this);
    this.starfieldEffects();
  },//end of update

  //-------GLOBAL FUNCTIONS-----//

  createStarfield: function(){
    this.starfield = this.add.tileSprite(400,300,800,4000, 'starfield');
    this.juststars = this.add.tileSprite(400,300,800,4000, 'juststars');
    this.nebula1 = this.add.tileSprite(400,300,800,4000, 'nebula1');
    this.stars2 = this.add.tileSprite(400,300,800,4000, 'juststars');
    this.nebula2 = this.add.tileSprite(400,300,2000,4000, 'nebula2');
  },

  createBulletEmitter: function(){
    this.flares = this.add.particles('flares').createEmitter({
      x: 1600,
      y: 200,
      angle: { min: 170, max: 190 },
      scale: { start: 0.4, end: 0.2 },
      blendMode: 'ADD',
      lifespan: 500,
      on: false
    });
  },

  starfieldEffects: function(){
    this.starfield.y += 0.5;
    this.juststars.y += 0.2;
    this.stars2.y += 3.8;
    this.nebula1.y += 0.8;
    this.nebula2.y += 0.3;
    this.nebula2.x -= 0.1;
  }
});//end of var

//ADD GAME TO SCENES
Space_Game.scenes.push(lvl1State);

 

I though maybe using the class is not the right way about it and could possibly work by breaking the class into more managable global functions that are called upon just like when the background is created. have a function that selects the weapon depending on the xp and then a function for the bullet attached then when i set up the bullet use something like

check what weaponSelect is == to

find the bullet associated

create a config for the animations of that particular bullet

write the code to display the bullets

then when spacebar is pressed the value from weaponselect and bulletSelect are used to then fire the function displaying the bullets.

 

Unless someone has a better way or a good source to review ????

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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