1

我正在做一个简单的项目,它将成为未来项目的支柱。

我的问题是。当我从“寻找”状态转移到“攻击”状态时,我会检查我是否在敌人的火力范围内。如果我是,它会朝我开枪。为此,我在“if(Enemy != none && VSize(Location - Enemy.Location) < AttackDistance)”语句中调用了函数“FireRocket”。这工作得很好,但正如预期的那样,每秒会发射一个音调的导弹(因为这个代码在一个 'tick 函数中)。为了避免这种情况,我尝试使用 SetTimer (SetTimer(FireInterval, true, 'FireRocket');) 这就是我的问题出现的时候。

现在,计时器仅在我移出敌人的射程并且他确实开火时才开始计数,但同样,只有当我在其射程之外时才开始计时。当我回到它的射程参数时,他停止向我开火……

问题出在“正在攻击”状态。

预先感谢您的帮助。

class AwesomeRangedBoss extends AwesomeEnemy
placeable;

var float FireInterval;

////EVENTS////

auto state Seeking
{
    function Tick(float Deltatime)
    {
        local vector NewLocation;

        if(Enemy == none)
        {
            GetEnemy();
        }

        else if(VSize(Location - Enemy.Location) < FollowDistance)
        {
            NewLocation = Location;
            NewLocation += normal(Enemy.Location - Location) * MovementSpeed * DeltaTime;
            SetLocation(NewLocation);

            if(VSize(NewLocation - Enemy.Location) < AttackDistance)
            {
                GoToState('Attacking');
            }
        }
    }
}

state Attacking
{
    function Tick(float Deltatime)
    {
        if(Enemy == none)
        {
            GetEnemy();
        }

        if(Enemy != none && VSize(Location - Enemy.Location) < AttackDistance)
        {
            //SetTimer(FireInterval, true, 'FireRocket');
            //FireRocket();
        }
        if(VSize(Location - Enemy.Location) > AttackDistance)
        {
            GoToState('Seeking');
        }
    }
}

/////////////////////////
function FireRocket()
{
    local UTProj_Rocket MyRocket;
    `log("BOOM!");

    MyRocket = spawn(class'UTProj_Rocket', self,, Location);
    MyRocket.Init(normal(Enemy.Location - Location));

    MyMesh.SetMaterial(0, AttackingMat);
    SetTimer(1, false, 'EndAttack');
}

event TakeDamage(int DamageAmount, Controller EventInstigator,
 vector HitLocation, vector Momentum, class<DamageType> DamageType,
optional TraceHitInfo HitInfo, optional Actor DamageCauser)
{
  Health--;

  if(Health == 0 && EventInstigator != none &&
EventInstigator.PlayerReplicationInfo != none)
    {
        WorldInfo.Game.ScoreObjective(EventInstigator.PlayerReplicationInfo, 1);

        Destroy();
    }
}

defaultproperties
{
    BumpDamage = 15.0

    MovementSpeed = 256
    FollowDistance = 1512
    AttackDistance = 1024
    Health = 5
    FireInterval = 1

    Begin Object Name=EnemyMesh
        Scale3D=(X=1.0, Y=1.0, Z=2.0)
    End Object

    Begin Object Name=CollisionCylinder
        CollisionRadius=128.0
        CollisionHeight=256.0
    End Object
}
4

1 回答 1

1

解决了!这是编辑后的代码。现在以 1 秒的间隔正确射击。

state Attacking
{
    function Tick(float Deltatime)
    {
        //HasFired = true;

        if(Enemy == none)
        {
            GetEnemy();
        }

        if(Enemy != none && VSize(Location - Enemy.Location) < AttackDistance)
        {
            ShootRocket();
        }
        if(VSize(Location - Enemy.Location) > AttackDistance)
        {
            GoToState('Seeking');
        }
    }
}

/////////////////////////
function ShootRocket()
{
    local UTProj_Rocket MyRocket;

    if(Enemy != none && VSize(Location - Enemy.Location) < AttackDistance && HasFired == false)
    {
        `log("BOOM!");

        MyRocket = spawn(class'UTProj_Rocket', self,, Location);
        MyRocket.Init(normal(Enemy.Location - Location));

        MyMesh.SetMaterial(0, AttackingMat);
        SetTimer(1, false, 'EndAttack');

       HasFired = true;

        if(HasFired == true)
        {
            SetTimer(FireInterval, false, 'IsAttacking');
        }
    }
}

function IsAttacking()
{
    HasFired = false;
    GoToState('Attacking');
}
于 2014-02-05T12:41:56.190 回答