Big Projects code organization
Someone

Hi,

I'm looking at isometric example and I see many parts of code like the witch talk function are on main iso.js file .

If I want to organize my code in a way that make a witch.js, cauldron.js, terrain.js separate classes/files like Java how I have to do?

 

I have seen behaviors have onclick,onmousedown,etc. function definitions but I want that all code relative to witch is in a witch file included the sceneObject initialization. How you suggest to do that? Do you have an example?

 

Thanks!

All 4 Comments
Gio

Hi

 

There are several ways to do that. I think that if you want to have something like Java classes, behaviors are probably the way to go.

 

I would personally put all the static data (like the list of animations and so on) together with the map data (with all the map data in its own json file). Then you can give the witch a Witch behavior that handles all the things that the witch does, using an onAddToScene function to do the scene object initialization. In my opinion that's the cleanest way of doing it, totally separating data and code. But that's personal preference.

 

If you want to have all the static data for the witch in the same file too, that's also possible. In fact, you could simply move all the code that relates to the witch to a witch.js file, and use wade.loadScript('witch.js')  from you main app file. If you give each object a unique name, it's totally fine to handle objects in separate files - if you need to get a reference to the witch object from another file, you can simply do

var myWitch = wade.getSceneObject('witch');

So basically you don't need to pass variables that reference the witch from one file to another, just use unique names and wade.getSceneObject (which is really fast).

 

Note that, by default, wade.loadScript loads a script and executes it right away, but you can also pass a parameter to it, to load the script without executing. You can then execute the script when it's more appropriate with

eval(wade.getScript('aScriptWeLoadedBefore.js'));

As a side note, you can also use wade.getSceneObjects to get all the objects of a same type, as you can use it to select all objects that have a given key/value pair. So for example, if every time you create a flower you do:

var flower = wade.iso.createObject(flowerData, flowerPosition, {type: 'flower'});

Then from any file in your app, you can do this to get a list of all the flowers in the scene:

var allFlowers = wade.getSceneObjects('type', 'flower');
Someone

Thank you,

I see onAddToScene is like a contructor. Can I pass parameters? something like:

 

var myWitch = wade.getSceneObject('witch', {witchLevel: 5, money: 500});

Gio

For normal (not isometric) scene objects that you add to the scene with wade.addSceneObject, you can pass parameters into wade.addSceneObject, and your object (and its behaviors) will receive them in their onAddToScene functions.

var myObject = new SceneObject();myObject.onAddToScene = function(data){     console.log(data.hello); // prints out 'world'};wade.addSceneObject(myObject, true, {hello: 'world'});

For isometric objects, however, you normally don't use wade.addSceneObject, but wade.iso.createObject instead (which creates an object on the isometric plane and adds it to the scene). Since the object is created and added at the same  time, you can't really add an onAddToScene function to it before it's added to the scene. But if your object has a behavior, then the behavior's onAddToScene function will be called. From there, you can access all the object's properties, since you can access the object via this.owner. So for example, you could define a behavior like this:

Witch = function(){    this.onAddToScene = function()    {        console.log(this.owner.money); // prints out 500    };};// somewhere in your app, where you create the objectvar witchData ={    sprites: ....    behaviors: [Witch, IsoCharacter, ...]};var myWitch = wade.iso.createObject(witchData, witchPosition, {money: 500, level: 5});

I should also note that, if you were to put all the witch's data into a json map, you could put the {money: 500, level: 5} bit in the map as well, in the instanceData field for the object. So you don't need to put any numbers at all in your .js files.

Someone

Thank you!

Post a reply
Add Attachment
Submit Reply
Login to Reply