zoukankan      html  css  js  c++  java
  • 【委托事件】一个订阅玩家死亡的事件

    using System.Collections;
    using System;
    
    public class NewBehaviourScript2 : MonoBehaviour {
    
    
            void Start () 
            {
                    Player player = new Player ();
    
                    player.PlayerHp += GameManage.HeroDeath;
                    player.PlayerHp += AduioManage.HeroDeath;
    
                    player.Dmage ();
    
            }
            
            public class Player
            {
                    public class DieEventArgs:EventArgs
                    {
                            public DieEventArgs()
                            {
                                    Debug.Log("你挂了");
                            }
                    }
    
                    public delegate void PlayerHpEventHandle(object sender,DieEventArgs e);
                    public event PlayerHpEventHandle PlayerHp;
    
                    private int curHp = 100;
    
                    public void Dmage()
                    {
    
                            while(curHp!=0)
                            {
                                    curHp -= 1;
                                    if(curHp==0)
                                    {
                                            DieEventArgs e = new DieEventArgs();
                                            Die(e);
                                    }
                            }
    
                    }
    
                    protected virtual void Die(DieEventArgs e)
                    {
                            if(PlayerHp!=null)
                            {
                                    PlayerHp(this,e);
                            }
                    }
    
            }
    
            public class GameManage
            {
                    public static void HeroDeath(object sender,Player.DieEventArgs e)
                    {
                            Debug.Log ("英雄死亡,用户失去控制英雄的权限");
                    }
            }
            
            public class AduioManage
            {
                    public static void HeroDeath(object sender,Player.DieEventArgs e)
                    {
                            Debug.Log ("英雄死亡,播放游戏失败的音乐");
                    }
            }
    
            
    
    }
    

      

  • 相关阅读:
    private知识笔记
    finalize知识笔记
    java实现队列的练习
    测试知识笔记(2)
    static和final知识笔记
    测试知识笔记(1)
    overloading知识笔记
    windows Copssh + git 搭建git服务器
    Java Servlet规范
    身份证验证JS代码
  • 原文地址:https://www.cnblogs.com/hellozzz/p/4741729.html
Copyright © 2011-2022 走看看