Jump to content

state not found with key:


ebraheeemz
 Share

Recommended Posts

hi guys

i have proplem with making states in phaser,

i make one state until now

in index.html i include all files i need like this

  <script type="text/javascript" src="js/phaser.min.js"></script>  <script type="text/javascript" src="game.js"></script>  <script type="text/javascript" src="load.js"></script>

then i game.js this is my code

 var game = new Phaser.Game(520, 490, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render });  function preload () { }  function create () { }  function update () { }  function render () { }game.state.add('loading', this.loadstate);game.state.start('loading');

and in load.js this is my code

var loadstate = function (game) {};loadstate.prototype = {preload: function() {  game.load.image('bg1', 'assets/bg1.jpg'); },create: function() {// this to start next levelgame.state.start('level1state');}};

i have this error

"Phaser.StateManager - No state found with the key: loading"

 

thanks in advance

Link to comment
Share on other sites

// this.loadstate might be undefinedgame.state.add('loading', this.loadstate);

Because you include game.js before load.js. 

You might use http://browserify.org/ or http://requirejs.org/

 

oh man you are the best, i put many hours of tries without result and search and research, no result too

finaly the solution is simple like this i cant believe

thank you every thing work now

Link to comment
Share on other sites

  • 1 year later...

I have a similar problem i have th error no state found with key Level2. but in my main.js i have

var TopDownGame = TopDownGame || {};

TopDownGame.game = new Phaser.Game(640, 320, Phaser.AUTO, '');

TopDownGame.game.state.add('Boot', TopDownGame.Boot);
TopDownGame.game.state.add('Preload', TopDownGame.Preload);
TopDownGame.game.state.add('Menu', TopDownGame.Menu);
TopDownGame.game.state.add('Level2', TopDownGame.Level2);
TopDownGame.game.state.add('Game', TopDownGame.Game);


TopDownGame.game.state.start('Boot');

Link to comment
Share on other sites

16 hours ago, fusilian said:

I have a similar problem i have th error no state found with key Level2. but in my main.js i have

var TopDownGame = TopDownGame || {};

TopDownGame.game = new Phaser.Game(640, 320, Phaser.AUTO, '');

TopDownGame.game.state.add('Boot', TopDownGame.Boot);
TopDownGame.game.state.add('Preload', TopDownGame.Preload);
TopDownGame.game.state.add('Menu', TopDownGame.Menu);
TopDownGame.game.state.add('Level2', TopDownGame.Level2);
TopDownGame.game.state.add('Game', TopDownGame.Game);


TopDownGame.game.state.start('Boot');

You should probably show us the code of other previous states.

Link to comment
Share on other sites

I don't know how but i fixed it. However i have a new problem (so sorry to bother you). I have a few states in my game (main, boot, preload, menu, lvl1 to level10 [10 states]). I dont know why but the first time i call the Menu state from preload it works but when i call it from the level states it just shows a black screen.

here is the html index :

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8" />
        <title>Galaxy Fighter ABK6</title>

        <script type="text/javascript" src="js/phaser.min.js"></script>
        <script type="text/javascript" src="js/Boot.js"></script>
        <script type="text/javascript" src="js/Preload1.js"></script>
        <script type="text/javascript" src="js/Menu.js"></script>
        <script type="text/javascript" src="js/Game1.js"></script>
        <script type="text/javascript" src="js/Level2.js"></script>
        <script type="text/javascript" src="js/Level3.js"></script>
        <script type="text/javascript" src="js/Level4.js"></script>
        <script type="text/javascript" src="js/Level5.js"></script>
        <script type="text/javascript" src="js/Level6.js"></script>
        <script type="text/javascript" src="js/Level7.js"></script>
        <script type="text/javascript" src="js/Level8.js"></script>
        <script type="text/javascript" src="js/Level9.js"></script>
        <script type="text/javascript" src="js/Level10.js"></script>
        <script type="text/javascript" src="js/End.js"></script>
        
        

        <style>
          body {
            padding: 0px;
            margin: 0px;
          }
          </style>
    </head>

    <body>  
        
        <script src="js/main.js"></script>
    </body>
</html>
here is the boot state

var TopDownGame = TopDownGame || {};

TopDownGame.Boot = function(){};


TopDownGame.Boot.prototype = {
  preload: function() {
    
    this.load.image('preloadbar', 'assets/preloader-bar.png');
  },
  create: function() {
  
    this.game.stage.backgroundColor = '#FFFFF';

    
    this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
    
    
    this.scale.pageAlignHorizontally = true;
    this.scale.pageAlignVertically = true;

    
    this.game.physics.startSystem(Phaser.Physics.ARCADE);
    
    this.state.start('Preload');
  }
};
here is the preload state

var TopDownGame = TopDownGame || {};


TopDownGame.Preload = function(){};

TopDownGame.Preload.prototype = {
  preload: function() {
    
    this.preloadBar = this.add.sprite(this.game.world.centerX, this.game.world.centerY, 'preloadbar');
    this.preloadBar.anchor.setTo(0.5);

    this.load.setPreloadSprite(this.preloadBar);

    
    this.load.tilemap('level1', 'assets/level1.json', null, Phaser.Tilemap.TILED_JSON);
    this.load.tilemap('level2', 'assets/level2.json', null, Phaser.Tilemap.TILED_JSON);
    this.load.tilemap('level3', 'assets/level3.json', null, Phaser.Tilemap.TILED_JSON);
    this.load.tilemap('level4', 'assets/level4.json', null, Phaser.Tilemap.TILED_JSON);
    this.load.tilemap('level4', 'assets/level5.json', null, Phaser.Tilemap.TILED_JSON);
    this.load.tilemap('level4', 'assets/level6.json', null, Phaser.Tilemap.TILED_JSON);
    this.load.tilemap('level4', 'assets/level7.json', null, Phaser.Tilemap.TILED_JSON);
    this.load.tilemap('level4', 'assets/level8.json', null, Phaser.Tilemap.TILED_JSON);
    this.load.tilemap('level4', 'assets/level9.json', null, Phaser.Tilemap.TILED_JSON);
    this.load.tilemap('level4', 'assets/level10.json', null, Phaser.Tilemap.TILED_JSON);
    this.load.image('tiles', 'assets/tiles.png');
    this.load.image('player', 'assets/ship.png');
    this.load.image('asteroidLarge', 'assets/asteroidLarge.png');
    this.load.image('asteroidMedium', 'assets/asteroidMedium.png');
    this.load.image('asteroidSmall', 'assets/asteroidSmall.png');
    this.load.image('portal', 'assets/portal.png');
    this.load.image('bullet','assets/bullet.png');
    this.load.image('heart', 'assets/heart.png');
    this.load.image('shield', 'assets/shield.png');
    this.load.spritesheet('explosionLarge', 'assets/explosionLarge.png', 64, 64, 8);
    this.load.spritesheet('explosionMedium', 'assets/explosionLarge.png', 58, 58, 8);
    this.load.spritesheet('explosionSmall', 'assets/explosionLarge.png', 41, 41, 8);
    this.load.audio('fire', ['assets/fire.ogg', 'assets/fire.m4a']);
    this.load.audio('destroyed', ['assets/destroyed.ogg', 'assets/destroyed.m4a']);
  },
  create: function() {
    this.state.start('Menu');
  }
};

the menu state

var TopDownGame = TopDownGame || {};


TopDownGame.Menu = function(){};
TopDownGame.Menu.prototype = {
    create: function() {
        
         var startInstructions = 'Click to Start -\n\nHead East. Survive and find the portal.\n\nUP arrow key for thrust.\n\nLEFT and RIGHT arrow keys to turn.\n\nSPACE key to fire.\n\nHighscore :'+highscore;

        this.tf_start = this.game.add.text(this.game.world.centerX, this.game.world.centerY, startInstructions, fontAssets.counterFontStyle);
        this.tf_start.align = 'center';
        this.tf_start.anchor.set(0.5, 0.5);
        this.game.input.onDown.add(this.startGame, this);
     },
     startGame: function(){
         this.state.start('Game');
     },
 };

and the lvl1 state (called Game) where i call the menu state in the function endGame.

var TopDownGame = TopDownGame || {};
var bulletProperties = {
    speed: 400,
    interval: 250,
    lifeSpan: 2000,
    maxCount: 30,
};
var playerProperties = {
  velocity: 300,
  timeToReset: 3,
  blinkDelay: 0.2,
};
var asteroidProperties = {
    startingAsteroids: 4,
    maxAsteroids: 20,
    incrementAsteroids: 2,
    asteroidLarge: { minVelocity: 50, maxVelocity: 150, minAngularVelocity: 0, maxAngularVelocity: 200, score: 250, nextSize: 'asteroidMedium', pieces: 2, explosion:'explosionLarge' },
    asteroidMedium: { minVelocity: 50, maxVelocity: 200, minAngularVelocity: 0, maxAngularVelocity: 200, score: 500, nextSize: 'asteroidSmall', pieces: 2, explosion:'explosionMedium' },
    asteroidSmall: { minVelocity: 50, maxVelocity: 300, minAngularVelocity: 0, maxAngularVelocity: 200, score: 1000, explosion:'explosionSmall' },
};

var fontAssets = {
    counterFontStyle:{font: '20px Arial', fill: '#FFFFFF', align: 'center'},
};
var score;
var shipLives;
var highscore;

TopDownGame.Game = function(){};

TopDownGame.Game.prototype = {
  create: function() {
    
    this.map = this.game.add.tilemap('level1'); 
    this.map.addTilesetImage('tiles', 'tiles');
    this.backgroundlayer = this.map.createLayer('background');
    this.blockedLayer = this.map.createLayer('blocks'); 
    this.map.setCollisionBetween(1, 2000, true, 'blocks');
    this.backgroundlayer.resizeWorld();
    
    this.portal=this.game.add.sprite(6208, 1664, 'portal');
    this.game.physics.arcade.enable(this.portal);

    
    this.bulletGroup=this.game.add.group();
    this.bulletGroup.enableBody = true;
    this.bulletGroup.physicsBodyType = Phaser.Physics.ARCADE;
    this.bulletGroup.createMultiple(bulletProperties.maxCount, 'bullet');
    this.bulletGroup.setAll('anchor.x', 0.5);
    this.bulletGroup.setAll('anchor.y', 0.5);
    this.bulletGroup.setAll('lifespan', bulletProperties.lifeSpan);
    this.asteroidGroup=this.game.add.group();
    this.asteroidGroup.enableBody = true;
    this.asteroidGroup.physicsBodyType = Phaser.Physics.ARCADE;
    this.resetAsteroids();
    this.heartGroup=this.game.add.group();
    this.heartGroup.enableBody = true;
    this.heartGroup.physicsBodyType = Phaser.Physics.ARCADE;
    this.resetHeart();
    this.shieldGroup=this.game.add.group();
    this.shieldGroup.enableBody = true;
    this.shieldGroup.physicsBodyType = Phaser.Physics.ARCADE;
    this.resetShield();
    this.tf_lives = this.game.add.text(150, 400, 3, fontAssets.counterFontStyle);
    this.tf_score = this.game.add.text( 400, 400, "0", fontAssets.counterFontStyle);
    this.tf_score.align = 'right';

    this.explosionLargeGroup = this.game.add.group();
    this.explosionLargeGroup.createMultiple(20, 'explosionLarge', 0);
    this.explosionLargeGroup.setAll('anchor.x', 0.5);
    this.explosionLargeGroup.setAll('anchor.y', 0.5);
    this.explosionLargeGroup.callAll('animations.add', 'animations', 'explode', null, 30);
    this.explosionMediumGroup = this.game.add.group();
    this.explosionMediumGroup.createMultiple(20, 'explosionMedium', 0);
    this.explosionMediumGroup.setAll('anchor.x', 0.5);
    this.explosionMediumGroup.setAll('anchor.y', 0.5);
    this.explosionMediumGroup.callAll('animations.add', 'animations', 'explode', null, 30);
    this.explosionSmallGroup = this.game.add.group();
    this.explosionSmallGroup.createMultiple(20, 'explosionSmall', 0);
    this.explosionSmallGroup.setAll('anchor.x', 0.5);
    this.explosionSmallGroup.setAll('anchor.y', 0.5);
    this.explosionSmallGroup.callAll('animations.add', 'animations', 'explode', null, 30);

    this.sndDestroyed = this.game.add.audio('destroyed');
    this.sndFire = this.game.add.audio('fire');

    
    this.player = this.game.add.sprite(250, 250, 'player');
    this.player.anchor.set(0.5);
    this.game.physics.arcade.enable(this.player);
    this.player.maxAngular = 500;
    this.player.angularDrag = 50;

    

    
    this.game.camera.follow(this.player);

    
    this.cursors = this.game.input.keyboard.createCursorKeys();
    this.key_fire=this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);

  },
   init: function () {
      this.bulletInterval = 0;
    
      shipLives = 3;
      score = 0;
      highscore= highscore||0;
      highscore= Math.max(score, highscore);
        
    },
  createAsteroid: function (x, y, size, pieces) {
        if (pieces==undefined) {pieces = 1;}
        for (var i=0; i<pieces;i++) {
            var asteroid = this.asteroidGroup.create(x, y, size);
            asteroid.anchor.set(0.5, 0.5);
            asteroid.body.angularVelocity = this.game.rnd.integerInRange(asteroidProperties.minAngularVelocity, asteroidProperties.maxAngularVelocity);
            var randomAngle = this.game.math.degToRad(this.game.rnd.angle());
            var randomVelocity = this.game.rnd.integerInRange(asteroidProperties.minVelocity, asteroidProperties.maxVelocity);
            this.game.physics.arcade.velocityFromRotation(randomAngle, randomVelocity, asteroid.body.velocity);
            asteroid.body.collideWorldBounds=true;
            asteroid.body.bounce.set(1, 1);
        }
    },
resetAsteroids: function () {
        for (var i=0; i < 150; i++ ) {
            var side = Math.round(Math.random());
            var x;
            var y;
            
            if (side) {
                x = 300 + Math.round(Math.random()) * 4800;
                y = 300 + Math.random() * 3200;
            } else {
                x = 300 + Math.random() * 5000;
                y = 300 + Math.round(Math.random()) * 4800;
            }
            this.createAsteroid(x, y, 'asteroidLarge');
        }
    },

    createHeart: function (x, y) {
        var heart = this.heartGroup.create(x, y, 'heart');
    },

    resetHeart: function () {
        for (var i=0; i < 20; i++ ) {
            var side = Math.round(Math.random());
            var x;
            var y;
            
            if (side) {
                x = 300 + Math.round(Math.random()) * 4800;
                y = 300 + Math.random() * 3200;
            } else {
                x = 300 + Math.random() * 5000;
                y = 300 + Math.round(Math.random()) * 4800;
            }
            this.createHeart(x, y);
        }
    },
    createShield: function (x, y) {
        var shield = this.shieldGroup.create(x, y, 'shield');
    },

    resetShield: function () {
        for (var i=0; i < 20; i++ ) {
            var side = Math.round(Math.random());
            var x;
            var y;
            
            if (side) {
                x = 300 + Math.round(Math.random()) * 4800;
                y = 300 + Math.random() * 3200;
            } else {
                x = 300 + Math.random() * 5000;
                y = 300 + Math.round(Math.random()) * 4800;
            }
            this.createShield(x, y);
        }
    },
    

  update: function() {
    
    this.game.physics.arcade.collide(this.bulletGroup, this.asteroidGroup, this.asteroidCollision, null, this);
    this.game.physics.arcade.overlap(this.player, this.portal, this.nextLevel, null, this);
    this.game.physics.arcade.overlap(this.player, this.heartGroup, this.addLife, null, this);
    this.game.physics.arcade.overlap(this.player, this.shieldGroup, this.shieldShip, null, this);
    if (!this.shipIsInvulnerable) {
            this.game.physics.arcade.overlap(this.player, this.asteroidGroup, this.asteroidCollision, null, this);
            this.game.physics.arcade.collide(this.player, this.blockedLayer, this.borderCollision, null, this);
        }
    
    
    this.player.body.velocity.x = 0;
    this.player.body.velocity.y = 0;
    this.player.body.angularVelocity = 0;
    this.tf_lives.x=this.game.camera.x;
    this.tf_lives.y=this.game.camera.y;
    this.tf_score.x=this.game.camera.x+300;
    this.tf_score.y=this.game.camera.y;


    if (this.cursors.left.isDown) {
      this.player.body.angularVelocity = -300;
    }
    else if(this.cursors.right.isDown) {
      
      this.player.body.angularVelocity = 300;
    }
    if (this.cursors.up.isDown) {
      this.game.physics.arcade.velocityFromRotation(this.player.rotation, playerProperties.velocity, this.player.body.velocity);
    }
    if (this.key_fire.isDown) {
      this.fire();
      
    }
    
  },
  fire: function () {
      if (!this.player.alive){
        return;
      }
              
        if (this.game.time.now > this.bulletInterval) { 
          this.sndFire.play();           
            var bullet = this.bulletGroup.getFirstExists(false);

            
            if (bullet) {
                var length = this.player.width * 0.5;
                var x = this.player.x + (Math.cos(this.player.rotation) * length);
                var y = this.player.y + (Math.sin(this.player.rotation) * length);
                bullet.reset(x, y);
                bullet.lifespan = bulletProperties.lifeSpan;
                bullet.rotation = this.player.rotation;
                this.game.physics.arcade.velocityFromRotation(this.player.rotation, bulletProperties.speed, bullet.body.velocity);
                this.bulletInterval = this.game.time.now + bulletProperties.interval;
            }
        }
    },
    
    asteroidCollision : function (target, asteroid) {
      target.kill();
      asteroid.kill();
      if (target.key=='player') {
        this.destroyShip();
      }
    
      this.splitAsteroid(asteroid);
      this.updateScore(asteroidProperties[asteroid.key].score);
      
      
      var explosionGroup = asteroidProperties[asteroid.key].explosion + "Group";
      var explosion = this[explosionGroup].getFirstExists(false);
      explosion.reset(asteroid.x, asteroid.y);
      explosion.animations.play('explode', null, false, true);
  
      
    },
    destroyShip: function () {
        shipLives --;
        this.sndDestroyed.play();
        this.tf_lives.text = shipLives;
        if (shipLives) {
            this.game.time.events.add(Phaser.Timer.SECOND * playerProperties.timeToReset, this.resetShip, this);
        } else {
            this.game.time.events.add(Phaser.Timer.SECOND * playerProperties.timeToReset, this.endGame, this);
        }
        var explosion = this.explosionLargeGroup.getFirstExists(false);
        explosion.reset(this.player.x, this.player.y);
        explosion.animations.play('explode', 30, false, true);
    },
    resetShip: function () {
        this.shipIsInvulnerable = true;
        this.player.reset(250, 250);
        
        this.game.time.events.add(Phaser.Timer.SECOND * playerProperties.timeToReset, this.shipReady, this);
        this.game.time.events.repeat(Phaser.Timer.SECOND * playerProperties.blinkDelay, playerProperties.timeToReset / playerProperties.blinkDelay, this.shipBlink, this);
    },
    shipReady: function () {
        this.shipIsInvulnerable = false;
        this.player.visible = true;
    },
    shipBlink: function () {
        this.player.visible = !this.player.visible;
    },
    endGame: function () {
        this.state.start('Menu');
    },
    updateScore: function (score1) {
        score += score1;
        this.tf_score.text = score;
    },

    splitAsteroid: function (asteroid) {
        this.sndDestroyed.play();
        if (asteroidProperties[asteroid.key].nextSize) {
            this.createAsteroid(asteroid.x, asteroid.y, asteroidProperties[asteroid.key].nextSize, asteroidProperties[asteroid.key].pieces);
        }
    },
    borderCollision : function (target) {
        target.kill();
        if (target.key=='player') {
        this.destroyShip();
      }

    },
    nextLevel: function (target, portal) {
        if ((target.key=='player') && (portal.key=='portal')) {
            this.state.start('Level2');
        }
    
    },
    addLife: function (target, heart) {
        if ((target.key=='player') && (heart.key=='heart')) {
            heart.kill();
            this.updateShipLives(1);
        }
    },
    updateShipLives: function (life) {
        shipLives += life;
        this.tf_lives.text = shipLives;
    },
    shieldShip: function (target, shield) {
        if ((target.key=='player') && (shield.key=='shield')) {
            shield.kill();
            this.shipIsInvulnerable = true;
            this.game.time.events.add(Phaser.Timer.SECOND * 5, this.endShield, this);
            this.game.time.events.repeat(Phaser.Timer.SECOND * playerProperties.blinkDelay, 24, this.shipBlink, this);
        }
    },
    endShield: function() {
        this.shipIsInvulnerable = false;
        this.player.visible = true;
    },

   
};
  I am very sory to bother you. I am quite new to this.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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