I'm following this MelonJS tutorial. I'm getting familiar with OOP- classes, constructors, etc... I have some questions about constructors.
In the following code snippet...
1) is init
a melonJS special function (I read thru API, http://melonjs.github.io/docs/me.ObjectEntity.html, doesn't seem to be melon), or JavaScript? It seems to be called automatically when the playerEntity is created... what is calling init
?
2) It seems sometimes this
is called (this.setVelocity
), sometimes me
is called (me.game.viewport.follow
). When do you call each?
3) For velocity, why do you need to multiply accel * timer tick
? : this.vel.x -= this.accel.x * me.timer.tick;
/*-------------------
a player entity
-------------------------------- */
game.PlayerEntity = me.ObjectEntity.extend({
/* -----
constructor
------ */
init: function(x, y, settings) {
// call the constructor
this.parent(x, y, settings);
// set the default horizontal & vertical speed (accel vector)
this.setVelocity(3, 15);
// set the display to follow our position on both axis
me.game.viewport.follow(this.pos, me.game.viewport.AXIS.BOTH);
},
/* -----
update the player pos
------ */
update: function() {
if (me.input.isKeyPressed('left')) {
// flip the sprite on horizontal axis
this.flipX(true);
// update the entity velocity
this.vel.x -= this.accel.x * me.timer.tick;
} else if (me.input.isKeyPressed('right')) {
init
is a constructor - every object that is created with the Object.extend() method will implement an interface that defines aninit
method.As for the
this
vs.me
- see the documentation:me
refers to the melonJS game engine - so all melonJS functions are defined in theme
namespace.this
is going to refer to whateverthis
is in the given context. For example, in your code snippet provided, it would refer to the instance of the player entity.