[英]How to make monster move left then right continuously C# XNA
i'm making my first game on XNA for my class. 我正在为我的课程在XNA上进行我的第一场比赛。 I'm trying to make the monsters move left and right automatically. 我正在尝试使怪物自动左右移动。 I have total 4 monsters for now. 我现在总共有4个怪物。 I'm trying to get them move left then right within the screen. 我正在尝试让他们在屏幕内向左然后向右移动。
//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;
}
}
Your first problem is your while
statement, once you enter it you are not going to exit because you are not changing your X value. 您的第一个问题是while
语句,一旦输入,就不会退出,因为您没有更改X值。 If it were me I would have a bool
array variable corresponding to each of your monsters. 如果是我,我将拥有一个与每个怪物对应的bool
数组变量。 I would also change your conditional to trigger the change of the boolean value once the monster has reached the extents at either end. 一旦怪物到达两端的范围,我也将更改您的条件以触发布尔值的更改。 Something like this. 这样的事情。
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.