Jump to content

Scene change triggering on reaching a certain point in x or y-axis.


ShivamS
 Share

Recommended Posts

This is an example for changing the scenes when an object hits a certain value on x-axis using basic HTML, Phaser and JavaScript. 

Index.Html file. 

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="phaser.js"></script>
  <script src="game.js"></script>
  <title>Test</title>
</head>
<body>
  
</body>
</html>


Now the Game.js file content:-

This contains the Scene wise functions. 

1. GameScene1

var pikachu;
var GameScene1 = new Phaser.Class({
  Extends:Phaser.Scene,

  initialize:
  function GameScene1() {
    Phaser.Scene.call(this,{key: 'GameScene1'});
  },
  preload: function() {
    this.load.image('pikachu','pikachu.png');
  },

  create:function () 
  {    
    cursors= this.input.keyboard.createCursorKeys();  

    this.pikachu = this.add.image(400, 350, 'pikachu').setScale(0.2);
    var txt= this.add.text(300,19,'Scene1 button');
    txt.setInteractive().on('pointerdown', function () {
      this.scene.scene.start('GameScene2');      
    })    
  },
  update:function () {    
    if (cursors.right.isDown)
    {
      if (this.pikachu.x !=1000)
      {
          this.pikachu.x += 2.0;
      } 
    } 
    else if (cursors.left.isDown)
    {         
      if (pikachu.x !=0)
      {
        pikachu.x -= 2.0;             
      }          
    }
      if(this.pikachu.x== 700){          
        this.scene.start('GameScene2');            
      }
  }  
});
 

The update:function contains the most important part of this example: 

if(this.pikachu.x== 700){          
        this.scene.start('GameScene2'); 
 
This defines if the value of x is 700 for the object's position it will trigger to load "GameScene2" i.e. the next scene. 
 
 
2. GameScene2
 
var GameScene2 = new Phaser.Class({
  Extends: Phaser.Scene,
  
  initialize:

  function GameScene2() 
  {
    Phaser.Scene.call(this,{ key: 'GameScene2'});    
  },

  preload: function () {   
    
  },

  create: function () {
    cursors = this.input.keyboard.createCursorKeys();

    var txt = this.add.text(300, 19, 'Exit');
    txt.setInteractive().on('pointerdown', function () {
      this.scene.scene.start('GameScene1');
    })
  },

  update: function () {    
  }
});

Similarly the pointerdowndown method is used in the GameScene2 for exit button. 

Below are some screenshots of the program which will help you in understanding it better:-

1. The initial screen. 

image.thumb.png.cc55195ec0e75e56ef7bec2a0671d484.png

2. Moving the object to right side on x-axis where it is about to touch 700. 

image.thumb.png.ca72e52c3c5323b09f65a8e8eebc8bdc.png

3. On moving the above object a bit more rightwards it'll trigger the scene to change i.e. to the next GameScene2. 

image.thumb.png.ad2d086a4a5d710ed683f43ac3c0468e.png

This Scene is completely empty as you can see. The object is no longer visible as it's not inserted in the GameScene2. 

I am new to phaser so if there are any mistakes please correct me, also I am happy to help. :D

Link to comment
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...
 Share

  • Recently Browsing   0 members

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