我正在用javascript为个人用途创建HTML5 Canvas的游戏引擎,但是我遇到了问题。我创建了一个具有超级构造函数和一些函数的实体类(如删除和添加新实体)以及类中的更新和初始化函数。但是,当我在代码的末尾运行主init时,使用了entities.init();它报告了一个错误,并说它不是一个函数,尽管我确信我公开了它。下面是代码类功能不是函数
function entities(){
//Entities class holds all objects that: take damage, move,and do things that a static object could not.//
//A list of all current entities in game//
var entitiesList = new Array();
//Allows removal of an entitiy from the game, and the current list of entities//
function removeEntity(id){
//snip!//
}
//entity superclass//
function entity(name, spriteName, HP){
//snip!//
var updateEntity = new function(){
console.log("UPDATING Entities")
//drawSprite(sprite, posX, posY);
if(this.timer > 0){
this.timer = this.timer - 1;
}else{
removeEntity(this.entityID);
delete this;
}
if(this.health == 0){
removeEntity(this.entityID);
delete this;
}
}
}
//Method to create a new entity//
function createNewEntity(entName, sprite, posX, posY, HP){
//snip!//
}
var damageField = new function(radius, power, posX, posY) {
//Damage any entities within a "square" radius of an entity. I plan to add radial version later//
//snip!//
}
this.init = function(){
console.log("INIATING ENTS");
createNewEntity("NUGGET", "chaingun_impact.png", 250, 250);
}
//update function for superclass update function to call//
this.update = function(){
entity.updateEntity();
}
}
主要初始化函数
function init(){
pushToSheetList();
jsonParser();
entities.init();
}
而且我相信99.99%的更新功能不叫要么是相同的代码非常简单,只是更新()代替。
我真的不知道该怎么做,除非我想让它成为这样,因此屏幕上的每个精灵都是手动硬编码的,没有人希望这是一个可重用的引擎。
这似乎已经奏效,_for now_,我不知道如果说我有一个包含地图数据的地图类,并有如果我打电话给createNewEntity将实际工作,我一个实体产卵猜想我会在稍后看到。 – NeonTheCoder