I am in the process of building a Jquery game for school and I am trying to get create() to recall its self when the method is ran by putting a setTimeout() at the end of the function (I am using setTimeout because addEnemySpeed is a random generated so it changes everytime) but it is not working the method only runs once from being called to initiate (smallEnemy.create()) but never recalls itself? I am hoping this is just a simple oversight on my part?? THANKS in advance for the help. CHEERS.
// OBSTACLE OBJECT CONSTRUCTOR //
function Obstacle(type, className, speed, startHealth, currentHealth, damageCause) {
this.type = type;
this.className = className;
this.speed = speed;
this.endX = -160;
this.startHealth = startHealth;
this.currentHealth = currentHealth;
this.damageCause = damageCause;
this.create = function(type, endX, speed) {
type = this.type;
endX = this.endX;
speed = this.speed;
var $obstacle = $('<div>');
// if the obstacle is a enemy add enemies class
if (type == 'smallEnemy' || type == 'bigEnemy') {
$obstacle.addClass('enemies');
}
// add correct class name
$obstacle.addClass(type);
// add obstacle to playground
$('#playGround').append($obstacle);
// animate obstacle down x axis remove if hits destination
$obstacle.transition({
x: endX
}, speed, 'linear', function() {
$(this).remove();
});
setTimeout(this.create,addEnemySpeed);
};
}
smallEnemy.create()