Greetings!
I am creating a 2d side-scroller game where I have only 2 levels but I have a problem. I will re-write all the codes again in the create()
function in level1Scene into my level2Scene
.
So my question is, is there a way where I can create a scene where I can drop the character creation, character control functions, sounds, etc...
so it can be accessed in any scenes?
Thank you.
You can, if you make the character sprites into separate objects. I use something similar to this:
/*Obstacle class*/
class Obstacle extends Phaser.Physics.Arcade.Sprite {
constructor(scene, x=0, y=0, texture = 'obstacle', frame = 0) {
super(scene,x,y,texture,frame)
scene.add.existing(this)
scene.events.on('update', this.update, this)
}
}
This object can be accessed in any scene, but I still have to have this code in the create()
function for each level:
this.obstacle = new Obstacle(this,x, y,texture,frame);
You can make character control into a function, but I currently do not know if you can make it into a function outside of the scene. perhaps if you add it to your character class?
Hope this helps a bit.
P.S. what changes between the scenes? Because if it’s just the background or map, I believe you can keep it in the same scene and just change the assets
Thanks for the reply.
-Okay, I’ll give it a try to apply this code you gave.
P.S. what changes between the scenes? Because if it’s just the background or map, I believe you can keep it in the same scene and just change the assets
-Yes, only the JSON Tilemap will change.
If you’re interested, I wrote an article that may help you with this. I approached it from the angle of loading map data from an API, but I do go over a technique that you could easily adapt to your use case.