繁体   English   中英

ProjectileMovement 在第二次生成时不起作用

[英]ProjectileMovement does not work at second spawning

我先用蓝图做一个简单的桨游戏,然后用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:球被生成并且它需要移动; 但它在第二次生成时不起作用,我不明白为什么,球生成了但它不动。

任何人都可以向我解释并帮助我解决它吗?

谢谢。

SpawnActorDeferred function 推迟BeginPlay ,让调用者有机会预先设置参数,这意味着,如果在完成构建的中途产生的actor(是一个半actor)直到FinishSpawningActor ZC1C425268E68385D1AB5074C17A94F14 被调用,无论它放在哪里; 这意味着,一个有效的演员实例还没有收到BeginPlay ,因此它没有完全初始化。
这里, Ref_GameBall ->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 帮助我解决这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM