zoukankan      html  css  js  c++  java
  • Delegate 让你的函数更专向

    function Touch(Actor Other,PrimitiveComponent OtherComp,Vector HitLocation,vector HitNormal)
    {   
          OnTouch(self,Other,OtherComp,HitLocation,HitNormal);
    }
    
    simulated delegate OnTouch(Actor Caller,Actor Other,PrimitiveComponent OtherComp,vector HitLocation,vector HitNormal);

      代理Delegate可以使一个方法在不同的类中有不同的反馈方法。例如Trigger的Touch函数和炮塔,机器人,生物敌人。

    Delegate可以让你的类定义包含的变量类的内部方法。这句话听起来有些拗口;) 没关系,示例是你的好老师。我要声明一个Trigger类,这个类可以在别人接触他的时候触发事件,例如炮塔可以用它划定自己的攻击触发事件。机器人也可以用它触发自己的事件。

      在自定义的Trigger中,你首先实现它的两个常规函数Touch和UnTouch。但是具体怎么实现的时候使用Delegate函数。

      然后再AntTower中使用该类

    class AntTower extends pawn placeable;
    
    var AntTrigger DetectiveSight;
    
    simulated function PostBeginPlay()
    {
         super.PostBeginPlay();
         
         DetectiveSight=Spawn(class'AntTrigger',,,location);

       if(DetectiveSight!=none)
    {
    //这里非常重要,实现Delegate函数的地方
    DetectiveSight.OnTouch=InternalOnTouch;
          }
    }

       代理类自身的销毁也非常重要,关系到垃圾处理。优化一款游戏应该谨慎垃圾回收。

    simulated event Destroyed()
    {
         super.Destroyed();
       
         if(DetectiveSight!=none)
        {
              DetectiveSight.OnTouch=none;
              DetectiveSight.Destroy();
        }
    }

      最后实现自己代理的函数

    function IntervalOnTouch(Actor Caller,PrimitiveComponent OtherCOmp,vector HitLocation,vector HitNormal)
    {
        //...
    }

      

         

     

  • 相关阅读:
    [BZOJ2763] [JLOI2011] 飞行路线
    [BZOJ4033] [HAOI2015] 树上染色
    [BZOJ2565] 最长双回文串
    [luogu5048] [Ynoi2019模拟赛] Yuno loves sqrt technology III
    又犯了低级错误了
    Win10系统无法使用小米手机的远程管理功能
    DevExpress破解和消除弹出框问题
    重写导致的问题
    EXCEL统计不重复值的数量
    C#中Button.DialogResult属性
  • 原文地址:https://www.cnblogs.com/NEOCSL/p/2840312.html
Copyright © 2011-2022 走看看