In etwas Code, den ich schreibe, habe ich eine Klasse namens Skeleton gemacht, die Entity erweitert, die GameObject erweitert, das Phaser.Sprite erweitert. Ich habe auch eine Spielerklasse gemacht. Aber der Player Class Code funktioniert gut. Obwohl jetzt, dass ich ein Skelett hinzugefügt habe, wirft es den Fehler: Uncaught TypeError: this.onTextureUpdate is not a function
(Der Fehler ist in der Phaser-Datei) Aber der Player-Code ist mehr oder weniger das gleiche wie Skelett (Der einzige Unterschied ist, dass der Spieler erbt von Entität, nicht Feind)Phaser JS - Uncaught TypeError: this.onTextureUpdate ist keine Funktion
ist Spieler Code:
var Player = function(game, x, y, stats) {
Entity.call(this, game, x, y, 'player', 1, 24, 14, 26, stats, [
[0, 1],
[2, 3],
[4, 5],
[6, 7]
], [
[8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]
], 10, 10);
this.width = 30;
this.height = 40;
};
Player.prototype = Object.create(Entity.prototype);
Player.prototype.constructor = Player;
Player.prototype.update = function() {
game.physics.arcade.collide(this, objects, null, function(obj1, obj2) {
return obj2 instanceof Wall;
});
this.moving = false;
if (cursors.up.isDown) {
this.direction = Entity.prototype.directions.BACKWARD;
this.moving = true;
} else if (cursors.down.isDown) {
this.direction = Entity.prototype.directions.FORWARDS;
this.moving = true;
} else if (cursors.left.isDown) {
this.direction = Entity.prototype.directions.LEFT;
this.moving = true;
} else if (cursors.right.isDown) {
this.direction = Entity.prototype.directions.RIGHT;
this.moving = true;
}
this.requiredFunctions();
};
Skelett:
var Skeleton = function(game, x, y, stats) {
Enemy.call(this, game, x, y, 'skeleton', 1, 23, 13, 5, stats, [
[0, 1],
[2, 3],
[4, 5],
[6, 7]
], [
[8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]
], 5, 5, "LEFT_TO_RIGHT_MOVEMENT");
};
Skeleton.prototype = Object.create(Enemy.prototype);
Skeleton.prototype.constructor = Skeleton;
Gameobject:
var GameObject = function(game, x, y, texture, boxX, boxY, boxW, boxH) {
Phaser.Sprite.call(this, game, x, y, texture);
game.physics.arcade.enable(this);
objects.push(this);
game.math.snapTo(this.x, 32);
game.math.snapTo(this.y, 32);
this.body.setSize(boxW, boxH, boxX, boxY);
};
GameObject.prototype = Object.create(Phaser.Sprite.prototype);
GameObject.prototype.constructor = GameObject;
GameObject.prototype.update = function() {
if(debugMode) {
this.game.debug.body(this);
}
}
Einheit:
var Entity = function(game, x, y, texture, boxX, boxY, boxW, boxH, stats, idleFrames, movingFrames, idleFrameRate, movingFrameRate) {
GameObject.call(this, game, x, y, texture, boxX, boxY, boxW, boxH);
this.stats = stats;
this.idleFrameRate = idleFrameRate;
this.movingFrameRate = movingFrameRate;
this.animations.add("idle forward", idleFrames[0], this.idleFrameRate, true);
this.animations.add("idle right", idleFrames[1], this.idleFrameRate, true);
this.animations.add("idle left", idleFrames[2], this.idleFrameRate, true);
this.animations.add("idle backward", idleFrames[3], this.idleFrameRate, true);
this.animations.add("moving forward", movingFrames[0], this.movingFrameRate, true);
this.animations.add("moving right", movingFrames[1], this.movingFrameRate, true);
this.animations.add("moving left", movingFrames[2], this.movingFrameRate, true);
this.animations.add("moving backward", movingFrames[3], this.movingFrameRate, true);
this.animations.play("idle forward");
this.moving = false;
//0 = forward
//1 = right
//2 = left
//3 = back
this.direction = 0;
this.effects = [];
};
Entity.prototype = Object.create(GameObject.prototype);
Entity.prototype.constructor = Entity;
Entity.prototype.move = function() {
if(this.moving) {
switch(this.direction) {
case Entity.prototype.directions.FORWARDS:
this.body.velocity.y += this.stats.speed;
this.direction = 0;
this.moving = true;
break;
case Entity.prototype.directions.BACKWARDS:
this.body.velocity.y -= this.stats.speed;
this.direction = 3;
this.moving = true;
break;
case Entity.prototype.directions.RIGHT:
this.body.velocity.x += this.stats.speed;
this.direction = 1;
this.moving = true;
break;
case Entity.prototype.directions.LEFT:
this.body.velocity.x -= this.stats.speed;
this.direction = 2;
this.moving = true;
break;
}
}
};
Entity.prototype.requiredFunctions = function() {
this.physics();
this.statusEffects();
this.move();
this.animate();
if(debugMode) {
this.game.debug.spriteBounds(this);
}
};
Entity.prototype.statusEffects = function() {
this.stats.resetToDef();
for (i in this.effects) {
if (this.effects[i].dead) {
this.effects.splice(i, 1);
}
}
};
Entity.prototype.addEffect = function(effect) {
var name = effect.name.split(" ")[0];
var p = [];
for (i in this.effects) {
if (i.name.split(" ")[0] == name) {
p.push(i);
}
}
if (p.length === 0) {
this.effects.push(effect);
} else {
var p1 = [];
var potency = effect.potency;
for (i in p) {
if (i.potency > potency) {
return;
}
if (i.potency === potency) {
p1.push(i);
}
}
if (p1.length === 0) {
this.effects.push(effect);
} else {
var p2 = [];
var time = effect.effectTime;
for (i in p1) {
if (i.effectTime >= time) {
return;
}
}
this.effects.push(effect);
}
}
};
Entity.prototype.physics = function() {
this.body.velocity.y = 0;
this.body.velocity.x = 0;
};
Entity.prototype.animate = function() {
this.smoothed = false;
if (!this.moving) {
switch (this.direction) {
case 0:
this.animations.play('idle forward');
break;
case 1:
this.animations.play('idle right');
break;
case 2:
this.animations.play('idle left');
break;
case 3:
this.animations.play('idle backward');
break;
}
} else {
switch (this.direction) {
case 0:
this.animations.play('moving forward');
break;
case 1:
this.animations.play('moving right');
break;
case 2:
this.animations.play('moving left');
break;
case 3:
this.animations.play('moving backward');
break;
}
}
};
Entity.prototype.directions = {
FORWARD: 0,
FORWARDS: 0,
RIGHT: 1,
LEFT: 2,
BACKWARD: 3,
BACKWARDS: 3
};
Entity.prototype.update = function() {
this.requiredFunctions();
};
Feind:
var Enemy = function(game, x, y, texture, boxX, boxY, boxW, boxH, stats, idleFrames, movingFrames, idleFrameRate, movingFrameRate, movementType) {
Entity.call(game, x, y, texture, boxX, boxY, boxW, boxH, stats, idleFrames, movingFrames, idleFrameRate, movingFrameRate);
this.movementFunc = undefined;
switch(movementType) {
case "LEFT_TO_RIGHT_MOVEMENT":
this.movementFunc = this.LEFT_TO_RIGHT_MOVEMENT;
break;
case "UP_TO_DOWN_MOVEMENT":
this.movementFunc = this.UP_TO_DOWN_MOVEMENT;
break;
case "STATIC_MOVEMENT":
this.movementFunc = this.STATIC_MOVEMENT;
break;
default:
throw "ERROR: Invalid movement type specified for instance of " + this.constructor;
}
};
Enemy.prototype = Object.create(Entity.prototype);
Enemy.prototype.constructor = Enemy;
Enemy.prototype.move = function() {
Enemy.move.apply(this);
movementFunc();
};
Enemy.prototype.STATIC_MOVEMENT = function() {
};
Enemy.prototype.LEFT_TO_RIGHT_MOVEMENT = function() {
if(this.direction === Entity.directions.FORWARDS || this.direction === Entity.directions.BACKWARDS) {
this.direction = Entity.directions.RIGHT;
}
game.phyics.arcade.collide(this, walls, function(obj1, obj2) {
this.direction = this.direction === Entity.directions.RIGHT ? Entity.directions.LEFT : Entity.directions.RIGHT;
});
};
Enemy.prototype.UP_TO_DOWN_MOVEMENT = function() {
if(this.direction === Entity.directions.RIGHT || this.direction === Entity.directions.LEFT) {
this.direction = Entity.directions.FORWARDS;
}
game.phyics.arcade.collide(this, walls, function(obj1, obj2) {
this.direction = this.direction === Entity.directions.FORWARDS ? Entity.directions.DOWNWARDS : Entity.directions.FORWARDS;
});
};
Bitte sagen, wenn Sie mehr von dem Code benötigen.
EDIT: Ich habe gerade festgestellt, dass ich dies zu Beginn von Enemy.call() weggelassen, aber nach dem erfolgt der Fehler immer noch.