ProjectileMovement 在第二次生成时不起作用
ProjectileMovement does not work at second spawning
我首先用 Blueprints 做一个简单的球拍游戏,然后用 C++。
我的 blueprint 游戏模式中有当前的工作流程:
这是我的代码基于:
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 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
函数被调用,放在哪里;这意味着,一个有效的 actor 实例尚未收到 BeginPlay
,因此尚未完全初始化。
这里,可以把Ref_GameBall ->FinishSpawning( Transform )
放在激活弹丸运动之前,或者用SpawnActor
代替SpawnActorDeferred
:
FActorSpawnParameters SpawnParameters;
SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
Ref_GameBall = world->SpawnActor<APaddleGameBall>(APaddleGameBall::StaticClass(), FTransform(FRotator::ZeroRotator, FVector::ZeroVector, FVector::OneVector), SpawnParameters);
就这些
感谢来自 Discord 的@KnownBugg 帮助我解决这个问题。
我首先用 Blueprints 做一个简单的球拍游戏,然后用 C++。
我的 blueprint 游戏模式中有当前的工作流程:
这是我的代码基于:
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 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
函数被调用,放在哪里;这意味着,一个有效的 actor 实例尚未收到 BeginPlay
,因此尚未完全初始化。
这里,可以把Ref_GameBall ->FinishSpawning( Transform )
放在激活弹丸运动之前,或者用SpawnActor
代替SpawnActorDeferred
:
FActorSpawnParameters SpawnParameters;
SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
Ref_GameBall = world->SpawnActor<APaddleGameBall>(APaddleGameBall::StaticClass(), FTransform(FRotator::ZeroRotator, FVector::ZeroVector, FVector::OneVector), SpawnParameters);
就这些 感谢来自 Discord 的@KnownBugg 帮助我解决这个问题。