以编程方式在melonJS中插入实体

Tor*_*sen 1 javascript game-engine melonjs

我一直在研究melonJS教程,同时牢记我的应用程序.

我希望能够以编程方式添加实体,而不是在为每个级别加载的.tmx文件中指定它们.这是可能的melonJS还是必须使用平铺编辑器指定实体?这似乎相当严格.

或者melonJS只是这个任务的错误框架,如果是这样,还有另一个框架可能比仅仅在画布上绘图更好吗?

Mar*_*tin 6

您可以以编程方式添加对象.这里有一些关于它的文章.

要恢复,您可以这样做:

在屏幕功能中,将"类"添加到甜瓜池中:

onResetEvent: function() {
  // tell the entity pool what classes it needs to work with
  me.pool.register('main', game.MainEntity, true);
},
Run Code Online (Sandbox Code Playgroud)

然后,当您需要从池中创建该类型的新对象时

var mainObject = me.pool.pull('main', 100, 100, otherData);
Run Code Online (Sandbox Code Playgroud)

当你需要将它添加到游戏中时调用它.

me.game.world.add(mainObject);
Run Code Online (Sandbox Code Playgroud)

最后,如果你需要删除它.

me.game.world.removeChild(mainObject);
Run Code Online (Sandbox Code Playgroud)

有关池的更多信息

这仍然与新版本的MelonJS 2.0.x兼容

  • 这应该被标记为正确的答案!工作完美! (2认同)