[英]How to make monster move left then right continuously C# XNA
我正在為我的課程在XNA上進行我的第一場比賽。 我正在嘗試使怪物自動左右移動。 我現在總共有4個怪物。 我正在嘗試讓他們在屏幕內向左然后向右移動。
//Monster movements
for (int i = 0; i < numOfMonster; i++)
{
if (destinationMonster[i].X >= screenWidth - 60)
{
while(destinationMonster[i].X != -10)
moveLeft = true;
}
else
{
moveRight = true;
}
if (moveLeft)
{
int temp = destinationMonster[i].X;
temp = destinationMonster[i].X - monsterSpeed;
//This prevents the object passing the screen boundary
if (!(temp < -10))
{
destinationMonster[i].X = temp;
}
moveLeft = false;
}
if (moveRight)
{
int temp = destinationMonster[i].X;
temp = destinationMonster[i].X + monsterSpeed;
//This prevents the object passing the screen boundary
if (!(temp > screenWidth - 50))
{
destinationMonster[i].X = temp;
}
moveRight = false;
}
}
您的第一個問題是while
語句,一旦輸入,就不會退出,因為您沒有更改X值。 如果是我,我將擁有一個與每個怪物對應的bool
數組變量。 一旦怪物到達兩端的范圍,我也將更改您的條件以觸發布爾值的更改。 這樣的事情。
if (destinationMonster[i].X >= screenWidth - 60)
{
moveRight[i] = false ;
}
else if (destinationMonster[i].X <= -10)
{
moveRight[i] = true ;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.