Jump to content

How to create Custom Classes/Objects


cwright
 Share

Recommended Posts

I'm sorry if this is basic, but I've been scouring the web for hours and can't find what I figured would be a highly discussed topic.

I want to create custom classes, but all I can find is extending Sprites. What if I don't want to extend the sprite class? What if I just need an 'empty' structure to hold object specific data?

I'm obviously missing something crucial, so if someone could fill me in I would be grateful.

Thanks,

Chris

Link to comment
Share on other sites

You should, since Phaser is just Javascript.

Are you, by chance, working with modules and didn't import or require() the object you need? Alternatively, you may assign your structure to the Phaser.Game itself (as long as you choose the name wisely).

Link to comment
Share on other sites

Thank you, that helped a bit, but now when I make a call to playerio to authenticate and I set the success callback to a function in the module, 'this' now refers to window instead of the module itself.

Also, I can't reference global variables like 'game' from inside my modules? 

THE PROBLEM IS IN THE FUNCTION this.loginReply CALLED FROM this.loginClicked

<body>
	<div id="gameDiv"></div>
	<div id="orientation"></div>
	<script type="text/javascript">
		window.onload = function() {
				
			$("#gameDiv").height($(window).height());
			$("#gameDiv").width($(window).width());

		    var game = new Phaser.Game(1024, 768, Phaser.AUTO, 'gameDiv', null, false, false);
			game.state.add('Boot', GameEngine.Boot);
			game.state.add('Preloader', GameEngine.Preloader);
			game.state.add('MainMenu', GameEngine.MainMenu);
		    game.state.add('Play', GameEngine.Play);
			game.state.add('Editor', GameEngine.Editor);
			game.state.start('Boot');

			game.global = {
				gameId: "###",
				client: null,
				connection: null,
				startMap: ""
			};

			};
	</script>
</body>

GameEngine.MainMenu = function (game) {

	this.game;
	this.input;

	// local vars
	this.menuState = null;

	this.bg = null;
	this.music = null;
	this.loginButton = null;
	this.createButton = null;

};

GameEngine.MainMenu.prototype = {

	create: function () {

		this.bg = this.add.tileSprite(0, 0, 1920, 1080, 'titleBG');
		this.music = this.add.audio('titleMusic');
		//this.music.play();

		this.updateMenu("main");

	},

	update: function () {

		this.bg.tilePosition.x -= .3;

	},

	// ======================= STATES =======================

	updateMenu: function (state) {

		this.menuState = state;
		switch (this.menuState) {
			case "main":
				this.loginButton = this.add.button(750, 550, 'loginButton', this.loginClicked, this, 'over', 'out', 'down');
				this.createButton = this.add.button(750, 650, 'createButton', this.createClicked, this, 'over', 'out', 'down');
				break;
		}

	},

	// ======================= LOGIN STATE =======================

	loginClicked: function (pointer) {
		
		this.loginButton.destroy();
		this.createButton.destroy();

		var that = this,
			gameId = that.game.global.gameId,
			username = "###",
			password = "###";

		PlayerIO.authenticate(
		    gameId,
		    "public",
		    {
		        username: username,
		        password: password
		    },
		    {},
		    this.loginReply,
		    function (error) {
		    	console.log(error);
		    }
		);

	},

	loginReply: function (client) {

		// ------------------------------------------------------------------------------
		// this refers to window, not the module
		// ------------------------------------------------------------------------------

		that.game.global.client = client;

    	// use local development server
		client.multiplayer.developmentServer = 'localhost:8184';

		// join the loader room
		client.multiplayer.createJoinRoom(
			'LoaderRoomId', 
			'Loader', 
			true, 
			null, 
			{ name: username }, 
			function (connection) {

				that.game.global.connection = connection;
				
				//connection.addMessageCallback("*", function (message) {
				connection.addMessageCallback("*", function (message) {
					switch (message.type) {
						case "loader": 
							that.game.global.connection.disconnect();
							that.game.global.startMap = message.getString(1);
							console.log(that.game.global.startMap);

							this.camera.fade(0x000000, 200, false);
							this.time.events.add(
								200, 
								function() {
									this.game.state.start('Game');
								}, 
								this
							);
							break;
						
					}
				});

				/*
				connection.addDisconnectCallback(function () {
					addChatLine('error','', 'disconnected from room')
			    })
			    */
			}, 
			function (error) {
				console.log(error);
			}
		);
	},

	createClicked: function (pointer) {
		
		this.loginButton.destroy();
		this.createButton.destroy();

		var that = this,
			gameId = that.game.global.gameId,
			username = "###",
			password = "###",
			email = "###";

		PlayerIO.authenticate(
		    gameId,
		    "public",
		    {
		        register: "true",
		        username: username,
		        password: password,
		        email: email
		    },
		    {},
		    function (client) {
		        //Success!
		        //The user is now registered and connected.
		    },
		    function (error) {
		    	console.log(error);
		        //Error registering.
		        //Check error.message to find out in what way it failed,
		        //if any registration data was missing or invalid, or if
		        //the entered captcha value didn't match the captcha image.
		    }
		);

	},

	// ======================= MODE STATE =======================

    goFullscreen: function () {

    	console.log("!!! Toggling Fullscreen !!!");
        if (this.scale.isFullScreen)
        {
            this.scale.stopFullScreen();
        }
        else
        {
            this.scale.startFullScreen(false);
        }

    }

};

 

Link to comment
Share on other sites

I apologize in advance in case I miss something, I'm bad at reading code made by someone other than myself.

If you're having problems with what this refers to, I strongly suggest reading these two chapters.

About not being able to get global variables... Well, you should be able to get these variables as long as they're in the main window object, and every global variable is put there. Did you check the order they're defined? Remember hoisting only happens with functions, and only inside their own scripts.

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