Jump to content

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


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">
  <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>

Now the Game.js file content:-

This contains the Scene wise functions. 

1. GameScene1

var pikachu;
var GameScene1 = new Phaser.Class({

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

  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 () {
  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){          

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

if(this.pikachu.x== 700){          
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,

  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 () {

  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. 


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


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


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.

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.


  • Recently Browsing   0 members

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