Jump to content

About custom classes


codesforests
 Share

Recommended Posts

How about Internet users? I am new to the forum and I have a very small javascript tour, perhaps my problem was that as soon as I entered javascript a friend was teaching me React.

Sorry for my bad English, I'm Spanish-speaking.

I suppose that many have already touched on this topic but I want you to clarify my doubt that if you can or not, to start with phaser I have more or less a week I did a lot of practices and everything was fine, but when I decided to create a platform project myself Example they have in phaser but in es6 and typescript I wanted to implement the philosophy of react separating the project into pieces in such a way that the code can be reusable, for example, if I need another button, I simply reuse the component that generates the button.

All good so far, my question is the following I want to create a class of "Player", if I am not mistaken more or less this is the code to implement.

class Character extends Phaser.GameObjects.Sprite {
    constructor(config) {
        super(config.scene, config.x, config.y, "character");
        //keep a reference to the scene
        this.scene=config.scene;
        config.scene.add.existing(this);

What I want is to know if I can place the image loads (preload) within that code, and the same movement mechanics (update) because what I have even understood is that for each scene it seems that you have to reload the images and as I said my idea is to reuse the codes creating class, I do not know if this is the correct idea, but I have been trying and I do not achieve anything, I have reviewed the example and I only see that they create the class alone without loading images and mechanics on the outside and they put everything in a file making it tedious, I repeat the attempt to implement the react philosophy and in order not to waste much time I ask them if I can achieve such a thing.

In theory I want to achieve something like this, "WARNINGi" is just an assumption I know the code is wrong. Really thank you for your time I hope you can clarify my doubts and I hope you are well. :)

class Character extends Phaser.GameObjects.Sprite {
    constructor(config) {
        super(config.scene, config.x, config.y, "character");
        //keep a reference to the scene
        this.scene=config.scene;
        config.scene.add.existing(this);
    }

        // load images
    preload ()
    {
        this.load.image('character', 'assets/sprites/character.png');
    }
    update ()
    {
        // player movement mechanics
    }
}
export default Character;

 

Link to comment
Share on other sites

  • 2 weeks later...

"sprite" does not have a preload. That is the Scene. All your preloading needs to happen on the scenes preload.

also note, that by default, your sprites update function will not be called. It will need to be added to a group, (its something people miss)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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