gpt4 book ai didi

c# - 尝试在运行时调整 bat (桨,乒乓球)的速度

转载 作者:太空宇宙 更新时间:2023-11-03 16:29:36 28 4
gpt4 key购买 nike

编辑 2: Pastebin link to the full ball, bat, and AIbat classes.

编辑:我已经更新了 turbo 代码,只是对其进行了更好的评论。我也有一个链接到我所有的 GameplayScreen code in Pastebin ,因为我不想用它淹没此页面。

我正在制作乒乓球类游戏。我正在创建一个会导致三件事发生的通电:

1) 球减速(工作)2) 屏幕变黑变白 (working)3 bat 减速(不工作)

我只是使用“speed”变量调整球的速度,一切正常。我正在尝试对球棒做同样的事情,但在比赛过程中它的速度没有变化。但是,当我进行调试时,在激活加电后,我可以将鼠标悬停在速度变量上并看到它已从默认速度 7.0f 更改为 30.0f,但是游戏本身没有发生明显的变化。

我还在屏幕上运行调试,显示球棒和球的速度,我注意到球的速度会相应变化,他的右球棒 (AiBat) 也会发生变化。它移动到 30。

但出于某种原因,左球棒(玩家控制的球棒)保持相同的速度。奇怪的。

我在这里做错了什么,但我一辈子都弄不明白。为什么我可以在一个位置改变速度并且它工作正常,但在另一个位置却不行?

Class bat
{

/// <summary>
/// Controls the bat moving up the screen
/// </summary>
public void MoveUp()
{
SetPosition(Position + new Vector2(0, -moveSpeed * elapsedTime));

}

/// <summary>
/// Updates the position of the AI bat, in order to track the ball
/// </summary>
public virtual void UpdatePosition(Ball ball, GameTime gameTime)
{
size.X = (int)Position.X;
size.Y = (int)Position.Y;

elapsedTime = 50.0f * (float)gameTime.ElapsedGameTime.TotalSeconds;

// Just here for debugging. Hitting Z WORKS FINE and slows the bats down
previous = current;
current = Keyboard.GetState();
if (current.IsKeyDown(Keys.Z))
{
elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds * 5.0f;
}
}
}

Class GameplayScreen
{

.......
private int disableCooldown;
private int coolDown;
private int powerDisableCooldown = 2000;
private int powerEnableCooldown = 5000;
......


public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)

// Updating bat position
leftBat.UpdatePosition(ball, gameTime);
rightBat.UpdatePosition(ball, gameTime);

if (gScaleActivated == true)
{
leftBat.moveSpeed = 30.0f;
rightBat.moveSpeed = 30.0f;
}


// If spaceBar is down and the turbo bar is not empty, activate turbo. If not, turbo remains off
if (input.ActivateTurbo(ControllingPlayer))
{
if (disableCooldown > 0)
{
leftBat.isTurbo = true;
coolDown = powerEnableCooldown;
leftBat.moveSpeed = 30.0f;
disableCooldown -= gameTime.ElapsedGameTime.Milliseconds;
}
else
{
leftBat.DisableTurbo();
}
}

// If spacebar is not down, begin to refill the turbo bar
else
{
leftBat.DisableTurbo();
coolDown -= gameTime.ElapsedGameTime.Milliseconds;
// If the coolDown timer is not in effect, then the bat can use Turbo again
if (coolDown < 0)
{
disableCooldown = powerDisableCooldown;
}
}

// Makes sure that if Turbo is on, it is killd it after () seconds
if (leftBat.isTurbo)
{
disableCooldown -= gameTime.ElapsedGameTime.Milliseconds;
}

/// <summary>
/// Logic to trigger Powerups
/// </summary>
private void PowerupActivated(object sender, PowerupEventArgs e)
{
...........
case PowerupType.SlowMo:
{
this.SlowMo(gScaleActivated);
gScaleActivated = true;
break;
}
...........
}


// Activates the SlowMo and grayscale effect for the powerup.
public void SlowMo(bool gScaleActivated)
{
gScaleActivated = true;
ball.SlowMoBall();
AudioManager.Instance.PlaySoundEffect("SlowMotion1");
}
}

最佳答案

在没有看到所有类的情况下,一种猜测是因为您在 Update 中设置 moveSpeed 之前执行了 UpdatePositions 调用,左 bat 的 moveSpeed 进一步向下重置,因此当您每次执行 UpdatePosition 调用时它会短暂地保持其重置值循环。要对此进行测试,请尝试在更新位置之前设置 moveSpeed?

            leftBat.UpdatePosition(ball, gameTime);
rightBat.UpdatePosition(ball, gameTime);

if (gScaleActivated == true)
{
leftBat.moveSpeed = 30.0f;
rightBat.moveSpeed = 30.0f;
}

关于c# - 尝试在运行时调整 bat (桨,乒乓球)的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11248721/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com