ShivamS Posted October 26, 2020 Share Posted October 26, 2020 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. 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.