I think the thing about javascript is that there are so many ways to do things that you have to pick a method and run with it.
I come from a python background, so I tend to think instead of âprototypesâ in terms of 'classes.'
The thing to realize is that they are objects.
Letâs say we have a Rocket ship. It may have engines.
Letâs make a class (or javascript prototype smash) of it. A rocket has some engines. The method I use is:
var Rocket = function () {
//Creates a rocket with some fuel
this.fuel = 100
}
Rocket.prototype.useEngines = function(time){
this.fuel = this.fuel - time*BURN_RATE //burn_rate is the fuel per second
}
Using your method it would be (I think. Iâm not familiar with your method):
var Rocket = function(){
this.fuel = 100
this.prototype = {
useEngines: function(time){
this.fuel = this.fuel - time*BURN_RATE
}
}
}
Now where am I going with this? The thing is that these are objects. So letâs create a rocket (or two):
rocket1 = new Rocket()
rocket2 = new Rocket()
These are now separate. If I go:
rocket1.useEngines(50)
console.log(rocket1.fuel)
console.log(rocket2.fuel)
They will be different. Only rocket1 will have used fuel because they are different objects.
From within a prototype/object/class you can access internal variables/functions by using "this.thingname"
From outside you can access them with ârocket.thingnameâ where rocket is an instance of the class.
So in your example youâre defining this âobjectâ called a CoverSprite (which inherits all the properties of pc.entity). Within that youâre adding a whole bunch of methods such as grow, shrink, setPos etc. Iâm not familiar with tween, but I suspect you can access other class/prototype functions from within the onUpdate by passing in as the callback both the entity and the function, so if it were our Rockets, we would pass:
tween.onUpdate(rocket2.useEngines)
or if you are inside the object:
tween.onUpdate(this.useEngines)
(No idea what tween onUpdate does or how you use it, but hopefully you get the idea).
One of the great things about programming is that while syntax changes, core ideas do not. So if you canât figure out javascript prototyping, have a shot at classes in python, or structs in C (ugh, C). They are all conceptually the same, just different implementations.
If you want an example of a prototype I made yesterday, here are some links to some code.
This is a map class/prototype. Within it it holds a representation of a map and some functions to work with it:
http://pastebin.com/2F6RWg4b
Then there is some more code that uses this map representation to build up a cellula-automata-like maze. Note in particular that I create an instance of the map (line 76) and then pass it around performing operations on it. (eg the randomize_map function uses the maps internal set_point function to randomize the map)
http://pastebin.com/6ySe0Gsw
The first stages of the project can be seen here:
HTML Only: http://sdfgeoff.totalh.net/cavex15/maptest.html
PlayCanvas Incomplete: http://sdfgeoff.totalh.net/cavex15/
(Press R for a new map in the playcanvas version. Be aware the playcanvas version is incomplete and will spout javascript errors left right and center until the assets have loaded. I need to figure out loading screens and physicsâŚ). For my second day working with playcanvas, Iâm pretty pleased I got that far though.