使用炸弹消灭全屏的敌人的方法

首先在bombManager脚本里面申请一个bool变量

bool isPressedButton=false;

然后写一个使用炸弹的方法

 public void UseBomb()
    {
        isButtonPressed = true;//将按钮状态设置为按下
        if (count>0)
        {
            count--;
            bombCount.text = "X " + count;
        }
         if(count<=0)
        {
            bomb.SetActive(false);
            bombCount.gameObject.SetActive(false);
        }
         StartCoroutine(ResetButtonPressed());// Corotine协同程序,即在主程序运行时同时开启另一段逻辑处理,来协同当前程序的执行。换句话说,开启协同程序就是开启一个线程。
    }

    //在按钮按下**秒之后将按钮设为未按下状态
    IEnumerator ResetButtonPressed()
    {
        yield return new WaitForSeconds(0.000000000000000000001f);
        isButtonPressed = false;
    }

 

然后在enemy脚本里申请一个bool变量:

bool getScore=false;

将其置入

 //死亡并得分
    public void ToDeadGetScore()
    {
        if (getScore) //如果已经得到分数,就不执行这段代码
        {
            return;
        }
        isExplosion = true;
        GameManager._instance.score += score;
    }
    //按炸弹按钮按下摧毁敌人的方法

 

的Update里加入

void Update () {
        this.transform.Translate(Vector3.down*speed*Time.deltaTime);
        if (transform.position.y<-6.6)
        {
            Destroy(this.gameObject);
        }
        if (isExplosion)
        {
            ExplosionAnimation();
        }
        else
        {
            BeHitting();
        }
        if (BombManager._instance.isButtonPressed)
        {   //如果bombmanager脚本的isButtonPressed为true,就执行一下程序
            ToDeadGetScore();
            getScore = true;  //将getScore设置为true
        }
        
    }

最后在unity编辑界面的脚本上点击“Execution Order...”

 

将脚本的运行顺序进行设置

最后将button的on click赋上bomb脚本挂载的游戏物体,然后选择usebomb方法就可以了

posted @ 2016-04-06 15:23  礼桀  阅读(299)  评论(0)    收藏  举报