繁体   English   中英

如何使怪物连续从左向右移动C#XNA

[英]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.

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