Jump to content

A question about classes


Ninjadoodle
 Share

Recommended Posts

Hi @enpu

I have a lot of different scenes with different gameplay mechanics.

I use classes a lot to setup my level objects/sprites.

What would be the best way to make a class local to a scene / module?

Currently all my classes are global, so that also means if I have two levels with cats, I have to label my classes accordingly.

It's not a big deal, but I'm still a bit confused about this, and wondering whether there is a better way I can set stuff up :)

Thank you in advance!

Link to comment
Share on other sites

@Ninjadoodle

Panda is designed in a way so that everything is inside the global game class. I'm having a bit hard to understanding what is actually your problem?

You have two Cat classes that have totally different functionality, but you want to name them both Cat? Do they have any same functionality? If so, then you should first make some kind of base class that has all the methods that you use on both, and then extend from that class. That's really the power of classes, that you can make one and include only the things that you are using commonly, and then extend from that class. That would not work if the classes were local objects. Also you can always use parameters to make you class do something little different. That way you should be able to just make one class and use that in for example different levels. Something like this:

game.createClass('Enemy', {
    jump: function() {
        // This is same for all classes that extend from Enemy
    }
});

// Extend from Enemy class
game.createClass('Cat', 'Enemy', {
    init: function(level) { // Use parameter on init function
        // Do something specific for Cat class

        if (level === 1) {
            // Do something specific when level parameter is 1
        }

        // This class will include jump function
        this.jump();
    }
});

game.createScene('Main', {
    init: function() {
        var cat = new game.Cat(1); // Pass parameter when creating instance of class
    }
});

Does this solve your problem at all? :)

Link to comment
Share on other sites

Hi @enpu

Thank you, that does actually help me with understanding the possibilities a little better.

I tend to make things difficult for myself, by creating games with lots of different mini-games/levels, that hardly share any of the same logic lol.

I think I'm actually pretty happy with the system I'm currently using as it seems to be working well. I'm just always looking for ways to improve my workflow a little bit.

Thanks again for the explanation :)

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.

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

  • Recently Browsing   0 members

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