Jump to content

Extending Phaser and tile map question


ecv
 Share

Recommended Posts

Hello, 

1.) How would I go about defining a new Phaser class in a separate .js file I can comfortably add to my projects? I'd like to use my class like this:

var game = new Phaser.Game(800, 600, Phaser.AUTO, '', {preload: preload, create: create, update: update});

var myClass = game.add.myClass(blah);

So I'd guess this would involve extending (or rather modifying) Phaser.GameObjectFactory somehow. But I have no idea how to do this in JS. Is it even possible to do this in a separate file? If you could provide some working code that'd be awesome.

As to the reasons, let's pretend for a moment I've got good reasons, and I'll tell you later on why I want to do it, as not to clutter the thread too much. There will probably be better ways to do what I want, but let's pretend there are not, so I (and others) can learn this one.

2.) Can I have a parallax effect by overlapping several different tile maps? Wondering if Phaser provides a built-in way to do this. Otherwise, can I simply overlap two tile maps and manually move one of them across the game world?

 

Thank you very much.

Link to comment
Share on other sites

It's totally possible! The secret is in modifying the GameObjectFactory prototype, like this: "Phaser.GameObjectFactory.prototype.myMethod = function(whatever) {}". You can do that in any file you want. Now the instance of the GameObjectFactory at "game.add" will have your "myMethod" so you can do this: "game.add.myMethod(thing);" and it'll work.

That's one of the big big big strengths of prototypal inheritance (as in JS) over classical inheritance (as in nearly everything else OOPy).

As for the second. In current Phaser (2.4.8), tilemap layers are fixed to the camera. If you want to parallax an individual tilemap layer you can set its scrollFactor properties (scrollFactorX and scrollFactorY). From the docs: "scrollFactorX of 0.5 scrolls half as quickly as the 'normal' camera-locked layers do"

Link to comment
Share on other sites

7 hours ago, drhayes said:

It's totally possible! The secret is in modifying the GameObjectFactory prototype, like this: "Phaser.GameObjectFactory.prototype.myMethod = function(whatever) {}". You can do that in any file you want. Now the instance of the GameObjectFactory at "game.add" will have your "myMethod" so you can do this: "game.add.myMethod(thing);" and it'll work.

That's one of the big big big strengths of prototypal inheritance (as in JS) over classical inheritance (as in nearly everything else OOPy).

Thank you drhayes :) If I want myMethod return a new object Phaser.myClass, can I define this too in a separate file? How? Thank you very much :) As you probably noticed, I know nothing about defining classes in JS, my experience is pretty much limited to C++. I don't even understand many of Java OOP features. Took me a looong time to understand the purpose of interfaces, f.e. :lol: 

7 hours ago, drhayes said:

As for the second. In current Phaser (2.4.8), tilemap layers are fixed to the camera. If you want to parallax an individual tilemap layer you can set its scrollFactor properties (scrollFactorX and scrollFactorY). From the docs: "scrollFactorX of 0.5 scrolls half as quickly as the 'normal' camera-locked layers do"

Looks like the Mario example moves the camera along the world while the tile map is fixed onto the world? I'll take a look at these, though I'm unsure this is what I'm looking for.

 

Lastly, the purpose to extend Phaser to suit my needs, which I didn't mention previously, is because I'm not sure I can have animated tile maps, better yet, animated layered tile maps. While I think this could be done with tile sprites, I just don't like the way it works. I don't want a static camera and having to scroll this tile sprite content to get the illusion of motion. I'd like my camera moving through the world, so I could simply have different sized sprites all over the world, but I just want to use maps to position these, if that makes sense... I've already done this, and, for the moment, I'm not noticing any kind of performance issues, although my system is rather powerful. So, the whole purpose is to extend Phaser as previously mentioned, so I can have this sort of mapped multilayered animated sprites all over my world, and easily port this to any future games. I will of course eventually profile this, in case the way I want to go about this is too insane and will decide if I should drop this approach and adopt a different one, which BTW you're welcome to comment :)

 

Link to comment
Share on other sites

"Can I define this too in a separate file? How?" By just defining it. You can define the class in a file and, in that same file, monkeypatch GameObjectFactory's prototype with your new method that returns your new class. That way it'll all be together, which is nice.

That sounds like a neat plan to me. :)

Link to comment
Share on other sites

Thank you drhayes. I was eagerly awaiting your answer. I guess I'll need to learn how the whole OOP thing works in JS first... :unsure: ahh... mmlazyyy :lol:

Hey, if that sounds good to you, I'm confident this will be a good investment :)

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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