简体   繁体   中英

ProjectileMovement does not work at second spawning

I'm doing a simple paddle game with Blueprints at first and C++ after.

I have the current workflow in my blueprint game mode: 游戏模式

And this is my code base on:

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();
}

The problem here is, every time the player make a point, the ball is destroyed and a new ball is spawned: 更新分数

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);
}

It works fine at the first, the initial, spawnin: ball is spawned and it takes movement; but it does not work at the second spawn, i don't understand why, the ball is spawned but it does not move.

Can anyone to explain me it and help me to solve it?

Thanks.

SpawnActorDeferred function defers the BeginPlay , giving a opportunity to the caller to set parameters beforehand, it means, the spawned actor if in a half-way to being full constructed(is a half actor) until the FinishSpawningActor function is called, wherever it is put; it means, a valid actor instance that didn't receive BeginPlay yet and so it is not fully initialized.
Here, Ref_GameBall ->FinishSpawning( Transform ) can be put before to activate the projectile movement, or replacing SpawnActorDeferred by SpawnActor :

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

That's all Thanks to @KnownBugg, from Discord, to help me with this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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