Sign in to follow this  
craidencool

MainMenu

Recommended Posts

Here is my code sir concerning the change of state. When I click the no button it is supposed to go to the main menu but it only shows the background color.

//CODE FOR MY INDEX
<body>
    <!-- Main canvas for rendering game stage -->
    <div id="game"></div>
    <div id="orientation"></div>
    <script type="text/javascript">
        window.onload = function(){
            var game = new Phaser.Game(640, 1024, Phaser.AUTO, 'game');
                game.state.add('Boot', DigestiveSystem.Boot);
                game.state.add('Preloader',DigestiveSystem.Preloader);
                game.state.add('Menu', DigestiveSystem.Menu);
                game.state.add('Stage1', DigestiveSystem.Stage1);
                game.state.start('Boot');
            };
    </script>
</body>

///CODE FOR MY MAIN MENU
DigestiveSystem.Menu = function(game){
};
var play, texts;
DigestiveSystem.Menu.prototype = {
    
    create: function () {   
        
        this.texts = this.add.bitmapText(this.world.centerX, this.world.centerY-200, 'font', 'The \n\n Digestive \n\n \t System', 20);
        this.texts.anchor.setTo(0.5,0.5);
        
        play = this.add.image(this.world.centerX, this.world.centerY, 'play');
        play.anchor.setTo(0.5,0.5);
        play.inputEnabled = true;
        play.events.onInputDown.addOnce(this.startGame, this);
        
       // this.settings = this.add.image(this.world.centerX, this.world.centerY + settingsh, 'settings');
       // this.settings.anchor.setTo(0.5,0.5);
      //  this.settings.scale.setTo(times,times);
        
        
      //  this.exit = this.add.image(this.world.centerX, this.world.centerY + exith, 'exit');
     //   this.exit.anchor.setTo(0.5,0.5);
     //   this.exit.scale.setTo(times,times);
        
    },
    
    update: function() {
        
    },
    
    startGame: function () {
        this.stage.backgroundColor = '#BA3838';
        this.state.start('Stage1');
    },
    
};



//CODE FOR MY STAGE1
DigestiveSystem.Stage1= function(game) {
    this.game = game;
};

var spawnSaliva, groupSaliva, nextSaliva, salivaDelay;
var life = 5, tries = 3;
var gameovermessage, yes, no, gameovers;


// set Game function prototype
DigestiveSystem.Stage1.prototype = {
    create: function(){
        
        touch.modal = new gameModal(this.game);
        createModals();
        this.startModal();
        this.CreateSaliva();
        
        this.game.touchControl = this.game.plugins.add(Phaser.Plugin.TouchControl);
        this.game.touchControl.inputEnable();
        
        player = this.add.sprite(50, this.world.centerY, 'player');
        player.anchor.setTo(0.5,0.5);
        this.game.physics.enable(player, Phaser.Physics.ARCADE);
        player.body.collideWorldBounds = true;
        walk = player.animations.add('walk');
        player.animations.play('walk', 30, true);
        this.camera.follow(player);
        
        this.map = this.add.tilemap('mouth_1025');     
        this.map.addTilesetImage('tileset');
        this.map.addTilesetImage('tileset4');
        
        this.layer = this.map.createLayer('Tile Layer 1');
        this.layer.resizeWorld();
       this.map.setCollisionBetween(0,12);
        
       this.map.setTileIndexCallback(7, this.touchInfo1, this);
       this.map.setTileIndexCallback(12, this.exitMouth, this);
        
        
    },
    update: function(){
        this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
        
        this.physics.arcade.collide(player,this.layer);
        
        this.spawnSaliva();
        
        var speed = this.game.touchControl.speed;
        var delay=0;
        
        player.body.velocity.x = 0;
        player.body.velocity.y = 0;
        
        if(this.input.activePointer.isDown){
            player.body.velocity.y -=  speed.y*5;//this.easeInSpeed(speed.y);
            player.body.velocity.x -=  speed.x*5;//this.easeInSpeed(speed.x);
            //back.tilePosition.x -= 0.9;
        }
       else{
            player.body.velocity.x = 0;
            player.body.velocity.y = 0;
        }
    },
    
    CreateSaliva: function() {
        //this.timer.start();
        //Create saliva group
        groupSaliva = this.add.group();
        
        //Enables body to group saliva and sets physics to arcade
        groupSaliva.enableBody = true;
        groupSaliva.physicsBodyType = Phaser.Physics.ARCADE;
        
        //Creates number of saliva
        groupSaliva.createMultiple(30, 'saliva');
        
        //Automatically kills saliva 
        groupSaliva.setAll('outOfBoundsKill', true);
        groupSaliva.setAll('checkWorldBounds', true);
        
        //sets anchors of all saliva
        groupSaliva.setAll('anchor.x', 0.5);
        groupSaliva.setAll('anchor.y', 0.5);
        
    },
    spawnSaliva: function(){
        
        this.physics.arcade.overlap(player, groupSaliva, this.playerHit, null, this);
        
        nextSaliva = 0;
        salivaDelay = 1000;
        
        
        
         if (nextSaliva < this.time.now && groupSaliva.countDead() > 0) {
            
            nextSaliva = this.time.now + salivaDelay;
             

            spawnSaliva = groupSaliva.getFirstExists(false);
            
            spawnSaliva.enableBody = true;
             
            spawnSaliva.reset(this.rnd.integerInRange(100,this.world.width), 64);

            spawnSaliva.body.velocity.y = this.rnd.integerInRange(200,500);
            
            spawnSaliva.body.velocity.x = this.rnd.realInRange(-150,150);
             
            var scale = this.rnd.realInRange(0.5, 0.8);
            spawnSaliva.scale.x = scale;
            spawnSaliva.scale.y = scale;

        }
    },
    

    
     playerHit: function(player, spawnSaliva){
        life--;
        spawnSaliva.kill();
        
        if(life===4){
            heart5.kill();
            console.log('4');
        }
        else if(life===3){
            heart4.kill();
            console.log('3');
        }
        else if(life===2){
            heart3.kill();
            console.log('2');
        }
        else if(life===1){
            heart2.kill();
            console.log('1');
        }
        else if(life===0){
            heart1.kill();
            tries -= 1;
            console.log('0');
            player.kill();
           if (tries===0){
                gameovers = this.add.image(this.camera.view.centerX, this.camera.view.centerY- 100 , 'gameover');
               gameovers.anchor.setTo(0.5,0.5);
               gameovers.scale.setTo(0.8,0.8); 
               this.gameover = true; 
           }else{
                gameovermessage = this.add.image(this.camera.view.centerX, this.camera.view.centerY- 100 , 'try');
               gameovermessage.anchor.setTo(0.5,0.5);
               gameovermessage.scale.setTo(0.8,0.8);

               yes = this.add.button(this.camera.view.centerX, this.camera.view.centerY , 'yes', this.retry);
               yes.anchor.setTo(0.5,0.5);
               yes.scale.setTo(0.8,0.8); 
               no = this.add.button(this.camera.view.centerX, this.camera.view.centerY + 100 , 'no', this.quit);
               no.anchor.setTo(0.5,0.5);
               no.scale.setTo(0.8,0.8);
               this.gameover = true; 
           }
           
      }
    },
    
    retry: function(){
        this.game.state.start('Stage1');
        life = 5;
    },
    
    quit: function(){
         this.game.state.start('Menu');
    },
    
    startModal: function(){
        this.game.physics.arcade.isPaused = true;
        touch.modal.showModal("StartMouth");
    },
    
    touchInfo1: function(player, tile){
      this.map.putTile(-1, tile.x,tile.y);  
      this.game.physics.arcade.isPaused = true;
      touch.modal.showModal("mouth_modal1");
    },
    
    
};

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.