Polska296

Setting up a game for multiple scenes using preload, create, and update

Recommended Posts

I am creating my first game in Phaser (an RPG) and was wondering what the best way to set up the game would be.  I have already set up the first scene and gotten everything to work as I want it. For different scenes, I will be changing the code in the preload and create functions to have different background images and characters show up on each scene. But I would like to keep the update function the same for all my scenes. What is the best way to get the update function into all my scenes? Below is a summary of how I set up my scene.

class Scene1 extends Phaser.scene {

preload() { This is where I load all my images used in the current scene}   Will change for each scene

create() { This is where I add my images and any text I have to the screen }   Will change for each scene

update() { Where all my functions for the game that allow the main character to battle other characters, assign movement to computer players, and other game play functions }   Will stay the same for each scene

}

 

Share this post


Link to post
Share on other sites

Probably the easiest would be to extend Scene1 and only override the preload and create methods.

 

The way I do it so that I don't have to have dozens (if not hundreds of scenes). Is to have one scene. Then in the preload method of that scene I load a particular section of an asset pack that contains the level specific assets (that level's tilemap, sounds, etc). From there create method uses the tilemap to build the level. My code never changes between levels but because its being fed a different tilemap and sounds each level is unique. I'm still working on doing this in Phaser 3 as the loader was just given support for pack loading last week but I have done it successfully in Phaser 2 and it works very well.

 

 

Share this post


Link to post
Share on other sites

Thank you B3L7! Your response helped me to understand a github example I found (https://github.com/jackyrusly/jrgame) and I just got it to work by creating a Basescene class that has the upload methods and then extending my scene 1 and scene 2 to Basescene, which each had different methods to load a different scene and characters.

I am curious about your second way of doing it and would love to see an example once you get it to work in Phaser 3 if you would be willing to show it.

 

Share this post


Link to post
Share on other sites

I know this thread is over a year old, but I ran across it looking for resources on how to organize/structure a Phaser project.

Just wanted to drop a quick note to B3L7 - your structure and example is an enormously helpful resource!

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

  • Recently Browsing   0 members

    No registered users viewing this page.