我正在学习这篇MelonJS教程。我开始熟悉面向对象的类、构造器等。我有一些关于构造器的问题。
在下面的代码片段中...
1) init是一个melonJS的特殊函数(我通过API,http://melonjs.github.io/docs/me.ObjectEntity.html读到的,看起来不像是甜瓜),还是JavaScript?它似乎是在创建playerEntity时自动调用的...什么叫init?
2)似乎有时this被称为(this.setVelocity),有时me被称为(me.game.viewport.follow)。你什么时候给每个人打电话?
3)对于速度,为什么需要乘以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')) {发布于 2014-06-16 22:53:47
init是一个构造函数--使用Object.extend()方法创建的每个对象都将实现一个定义init方法的接口。
至于this与me的对比,请参阅documentation
me指的是melonJS游戏引擎--所以所有的melonJS函数都是在me中定义的,而this将在给定的上下文中引用。例如,在您提供的代码片段中,它将引用播放器实体的实例。https://stackoverflow.com/questions/22211632
复制相似问题