大学项目:setTimeout问题

时间:2013-12-08 16:56:42

标签: javascript

我正在意大利比萨学习信息工程,我的考试之一就是创建一个Web服务。我的选择是用Javascript编写的小游戏。有一个名为“Hobo”的闪存游戏与我的相似。我的问题是:

我必须创建一个恢复敌人空闲动画的功能。我需要在另一个函数(attack函数)中调用它。 restore_idle_gif函数设置setTimeout,在800毫秒后恢复原始动画但我遇到以下麻烦:它有多个敌人,该函数恢复最后一个攻击敌人的动画。

代码是:

function enemy_class ( ..... )
{
    // Variables

    // Methods
    this.attack_method = attack_fun ;
    this.move_method   = move_fun   ;

    // Movement timer and Attack Cooldown ( they works as i wanted )
    this.move_timer=setInterval(this.move_enemy.bind(this),( 1000/this.movement_speed));
    this.attack_timer=setInterval(this.attack_enemy.bind(this),( 1000/this.attack_speed));
}

function attack_fun()
{
   // Some stuff that we don't need for my problem
   if ( chance_of_attack == true )
   {
      nemico = document.getElementById( this.get_enemy_id() );
      nemico.setAttribute( "src", "Media/images/enemy_attack.gif" ) ;
      damage_hero ( this ) ;
      restore_idle_gif( this ) ;
   }
}

function damage_hero( that ) 
{
   // works
}

最后,EVIL功能让我感到不安:

function restore_idle_gif( nemico_che_attacca )
{
      var restore_timer = setTimeout ( function() { var restore = document.getElementById( nemico_che_attacca.get_enemy_id() ) ; restore.setAttribute ( "src", "Media/images/enemy.gif" ) ; } , 800 ) ;

}

其余的代码工作正常,除了这个“故障”。我真的需要它才能工作,因为我需要为所有剩余的动画创建代码。请帮我处理该代码或给我提出想法/替代解决方案。对不起我的英文不好以及格式错误的文字,这是我第一次来Stackoverflow。

0 个答案:

没有答案