Jump to content

Accessing variables from my Game Scene in other classes


RoastMyCode
 Share

Recommended Posts

Hey there, 
I just started out programming in JS and currently am trying to implement the multiplayer card game 'Durak' in Phaser 3.  
So the basic communication between client and server works and now I am cleaning up my GameScene.js because it became really crowded.  

My idea is basically to have a ZoneHandler that handles the creation of zones and a Dealer that manages the rendering and movement of cards.  
Also I have an incomingEvents.js where all the incoming Events and the actions are saved in. Now I would like to call the Dealer that gets created in the GameScene.js and tell it to deal the cards to the client. 

Unfortunately I cant manage to access the dealer variable from my incomingEvents.  
Here is what part of my GameScene.js looks like:
 

const incomingEvents = require('../communication/incomingEvents');

class GameScene extends Phaser.Scene {
...
create() {
...

this.zoneHandler = new ZoneHandler(this);

this.zoneHandler.createPlayZone(1280 / 2, 720 / 2, 1280 / 2, 720 / 2, 0xdbff33);
this.zoneHandler.createOwnZone(1280 / 2, 720 * 0.9, 1280 / 2, 135, 0xFFBD33);
this.zoneHandler.createOpponentZone(1280 * 0.1, 720 / 2, 135, 1280 / 2, 0x00000);

this.dealer = new Dealer(this, this.zoneHandler);

this.socket = io('http://localhost:3000');
Object.entries(incomingEvents).forEach(([event, action]) => this.socket.on(event, action));

...
}
module.exports = GameScene;

Now in my incomingEvents.js I want to call the function dealOwnCards(cards) of the dealer but I dont understand how to access the dealer object in the GameScene.

const GameScene = require('../scenes/GameScene');

module.exports = {
    connect: () => {
        console.log('connected');
    },
    dealOwnCards: (cards) => {
        console.log('deal own cards received from server');
        GameScene.dealer.dealOwnCards(cards);
    },
}

I also tried exporting simple strings in the GameScene.js but that also didnt work. Besides that I tried different syntax never made it work.  
Im fairly sure that the solution isn't too complicated so it would be nice if you guys could help me out!  
Once again, Im new to this stuff so bear with me. Thanks

 

edit. rereading my post I realize that my question might not be relevant to Phaser 3 because it could be more of a general Javascript question but Im not really sure of that. Please feel free to move it or tell me to republish it at another place in the forum Mr. Admin.

Edited by RoastMyCode
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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