2014-06-22 47 views
0

我正在为学校构建一个jquery游戏的过程中,我试图让方法通过在setTimeout()的末尾运行来调用create()函数 (我正在使用setTimeout,因为addEnemySpeed是一个随机生成的,所以它每次都会改变),但它不工作该方法只从被调用启动(smallEnemy.create())运行一次,但永远不会回忆自己?我希望这只是我的一个简单的疏忽?先谢谢您的帮助。 CHEERS。jQuery对象构造函数 - 方法调用本身

// 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() 
+0

有多少次你想'this.create'要重复? –

+0

对于初学者来说,当''this.create'被'setTimeout()'调用时'this'的值不正确。你可以使用'setTimeout(this.create.bind(this)',addEnemySpeed)来代替。 – jfriend00

+0

@AminJafari我需要它继续不断地调用自己,直到我不再需要它。游戏运行在一个定时器上,如果其他语句运行,如果语句控制它,则例如:如果定时器在100和80之间,我将以随机速度创建一种类型的障碍,如果定时器在80和60之间将创建另一种障碍等等 感谢您的帮助 – VinceBrown

回答

0

这不是原型。相反,它是一个构造函数。当你创建一个新的障碍,你会想打电话var foo = new Obstacle(...);

function Obstacle(type, className, speed, startHealth, currentHealth, damageCause) { 

    // private vars 
    var timeout, 
     endX = -160; 

    // private methods 
    function create(addEnemySpeed) { 
    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(); 
    }); 

    timeout = setTimeout(create, addEnemySpeed); 
    } 

    // I added this method so you can stop spawning this obstacle if you want 
    function stop() { 
    if (timeout) { 
     clearTimeout(timeout); 
     timeout = null; 
    } 
    } 

    // public api (exports) 
    this.type   = type; 
    this.className  = className; 
    this.speed   = speed; 
    this.startHealth = startHealth; 
    this.currentHealth = currentHealth; 
    this.damageCause = damageCause; 

    this.create   = create; 
    this.stop   = stop; 
} 

好吧,让我们把它用现在

var smallEnemy = new Obstacle(...); 

// Spawn a new enemy of this type ever 5 seconds 
// Don't forget to pass addEnemySpeed 
// (addEnemySpeed was undefined in your code) 
smallEnemy.create(5000); 

可选:通话时要停止产卵stop

smallEnemy.stop(); 

作为一个附注,您可能需要考虑定义一些默认值并将对象传递给构造函数。这样你不需要传递6个有序的参数给构造函数(我认为这是相当多的)。

new Obstacle({type: "foo", speed: 100}); 

对于没有在对象中设置的任何键的默认值回退。如果这是您的选择,您会比我更了解我。只是想我会提到它。

+0

非常感谢你的彻底答案,刚开始上个月的JavaScript/jQuery,所以试图尽可能多地接受和像这些帮助的答案。肯定会采取你的建议在默认值,因为6个参数被证明是乏味的记住顺序,并真正感谢停止方法的帮助。 干杯。 – VinceBrown

2

你尝试过这样的:

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(); 
    }); 
    var that=this; //this is how you can use "this" element in a function 
    setTimeout(function(){that.create;},addEnemySpeed); 
    }; 
} 

smallEnemy.create() 
+1

你试过了吗?它不会工作,因为您仍然将该函数作为引用传递,而不是作为闭包或绑定函数。下面的答案解释了JavaScript中的'this'以及如何使用原型:http://stackoverflow.com/a/16063711/1641941你将它设置为'this',所以可能想要通过闭包,然后正确代码将是:setTimeout(function(){that.create;},addEnemySpeed); – HMR

+0

好点,我的坏! ;) –

相关问题