Jump to content

Reusable code snippet / function?


Recommended Posts

Hi guys


just wondering whether anyone has an answer to this ...


Is there anyway to do 'reusable snippets/functions' in Panda?


Let's say (for example) that I have 20 levels (in different js files) and in each level, have to set up 100 sprites and put them on screen.


I don't really want to retype the same thing in every scene and Im wondering if there is some sort of reusable code snippet functionality (sort of like html and 'includes').


something where I can just type it once and then do ...



... in each level.


PS. I'm not talking about classes or objects - Literally a piece of of code which I can 'include' in my level.


Thank you heaps for any help!

Link to comment
Share on other sites

There are few ways to do it.


1. Make global function on game object

game.myFunction = function() {    // Do something};game.myFunction();

2. Inject function into Scene class

game.Scene.inject({    myFunction: function() {        // Do something    }});// Now you got myFunction in every Scene classgame.createScene('MyScene', {    init: function() {        this.myFunction();    }});

And again, make sure you don't overwrite any existing functions.


Is that what you are looking for?

Link to comment
Share on other sites

Hi @enpu


This sounds good and I think it's exactly what I need (I will need to play with it a little).


Just to clarify ...


If I am making a 3 containers for use in 20 levels ...


game.createContainers = function() {

        game.bg = new game.Container().addTo(game.scene.stage); <- Do I need to preface the container names with 'game'?

        game.mg = new game.Container().addTo(game.scene.stage);
        game.fg = new game.Container().addTo(game.scene.stage);
I could then run game.createContainers(); in each of my 20 levels and insert sprites into for example game.bg.
Would this be the correct way of doing things?
Thank you heaps for all your help :)
Link to comment
Share on other sites

I would make one "base" scene for all levels, and then extend that.

game.createScene('Level', {    init: function() {        this.bg = new game.Container().addTo(this.stage);        this.mg = new game.Container().addTo(this.stage);        this.fg = new game.Container().addTo(this.stage);    }});game.createClass('Level1', 'Level', {    init: function() {        this.super();        // Do something    }});


Link to comment
Share on other sites

Hi @enpu


Is there anything wrong with having multiple scenes for levels?


I don't really understand how I would switch between levels using 'extend'.


I'm just looking for ways to streamline my work a little.


I'm going to have to look into 'extend' as it's a little over my head right now :(


Thank you!!

Link to comment
Share on other sites

Personally i would code my game so that there is only one scene for all levels and then load for example correct JSON data depending on what level you are.


The extend function is really simple, always when you create new class (Scene or any other) you are extending from another class.

Extended class will have all properties and methods from the class that it extends from.


If you create new class with game.createClass function, but don't define class to extend from, you are extending from game.Class class, that is empty class.

// Creates game.MyClass, that extends from game.Classgame.createClass('MyClass', {});// So that's same asgame.createClass('MyClass', 'Class', {});// Create game.Player class with some properties and functionsgame.createClass('Player', {    health: 10,    jump: function() {        console.log('test');    }});// Create new class game.Panda that extends from game.Player (so it does have all properties and functions from game.Player class)game.createClass('Panda', 'Player', {    init: function() {        console.log(this.health); // 10        console.log(this.jump()); // test    }});// game.createScene is just a shorthand to game.createClass('MyScene', 'Scene', {});game.createScene('MyScene', {});// Same asgame.createClass('MyScene', 'Scene', {});

Does that make sense to you?

Link to comment
Share on other sites

Hi @enpu


That makes sense and I'm very grateful for you taking your time out to explain it :)


Just one more question ...


I know you said you would code your game with only one scene and load the correct json file, but in my case, I often have games where each stage is a completely new mini-game.


Wouldn't it be easier to just lay your graphics out inside different scenes?


I find this much easier for organising my games, so I'm just wondering if this practice is wrong and if it's going to cause any problems?


Thanks you heaps again!

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