【问题标题】:ProjectileMovement does not work at second spawningProjectileMovement 在第二次生成时不起作用
【发布时间】:2020-06-27 22:33:08
【问题描述】:

我先用Blueprints 做一个简单的桨游戏,然后用C++ 做。

我的蓝图游戏模式中有当前的工作流程:

这是我的代码基础:

void APaddleGameGameMode::SpawnBall()
{
    UWorld *world = GetWorld();
    if (world)
    {
        Ref_GameBall = world->SpawnActorDeferred<APaddleGameBall>(APaddleGameBall::StaticClass(), FTransform(FRotator::ZeroRotator, FVector::ZeroVector, FVector::OneVector), NULL, NULL, ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding);

        if (Ref_GameBall) world->GetTimerManager().SetTimer(DelayBallMovement, this, &APaddleGameGameMode::SetVelocity, 0.2f);
    }
}

void APaddleGameGameMode::SetVelocity()
{
    if(Ref_GameBall) Ref_GameBall->ProjectileMovement->Velocity = FVector(Direction * Speed, 0.f, 0.f).RotateAngleAxis(FMath::RandRange(-45.f, 45.f), FVector(0.f, 1.f, 0.f));
    Ref_GameBall->ProjectileMovement->Activate();
}

这里的问题是,每次玩家得分时,球就会被摧毁并产生一个新球:

void APaddleGameGameMode::UpdateScore(bool bIsAI)
{
    UWorld *world = GetWorld();
    if (bIsAI)
    {
        SPScore++;
        Direction = 1.f;
    }
    else
    {
        FPScore++;
        Direction = -1.f;
    }
    if (world)  world->GetTimerManager().SetTimer(DelaySpawningBall, this, &APaddleGameGameMode::SpawnBall, 1.f);
}

一开始它工作正常,最初的 spawnin:球被生成并且它需要移动;但它在第二次生成时不起作用,我不明白为什么,生成了球但它没有移动。

谁能给我解释一下并帮我解决?

谢谢。

【问题讨论】:

    标签: c++ unreal-engine4 unreal-blueprint


    【解决方案1】:

    SpawnActorDeferred 函数推迟了BeginPlay,让调用者有机会事先设置参数,这意味着,如果生成的actor在完全构造的中途(是一个半actor)直到@987654322 @函数被调用,无论它放在哪里;这意味着,一个有效的actor实例还没有收到BeginPlay,所以它没有完全初始化。
    这里,Ref_GameBall -&gt;FinishSpawning( Transform ) 可以放在前面激活弹丸运动,或者将SpawnActorDeferred 替换为SpawnActor

    FActorSpawnParameters SpawnParameters;
    SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
    Ref_GameBall = world->SpawnActor<APaddleGameBall>(APaddleGameBall::StaticClass(), FTransform(FRotator::ZeroRotator, FVector::ZeroVector, FVector::OneVector), SpawnParameters);
    

    仅此而已 感谢来自 Discord 的 @KnownBugg 帮助我解决这个问题。

    【讨论】:

      猜你喜欢
      • 2018-06-15
      • 2016-03-30
      • 2021-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-25
      相关资源
      最近更新 更多