如何让我的子弹随机移动Y - + 3

时间:2014-09-01 18:43:22

标签: c# xna spread

如何使我的子弹从X轴开始到y轴随机化+3 -3像素

public void UpdateBullets()
{       
    //For each bullet in our bulletlist: Update the movment and if the bulet hits side of the screen remove it form the list
    foreach(Bullet bullet in bulletList)
    {
        //set movment for bullet
        bullet.position.X = bullet.position.X + bullet.speed;

        //if bullet hits side of screen, then make it visible to false
        if (bullet.position.X >= 800)
            bullet.isVisible = false;
    }
    //Go thru bulletList and see if any of the bullets are not visible, if they aren't  then remove bullet form bullet list
    for (int i = 0; i < bulletList.Count; i++)
    {
        if(!bulletList[i].isVisible)
        {
            bulletList.RemoveAt(i);
            i--;
        }
    }         
}

当我拿着太空子弹时,只需前进,我也希望它能够在Y轴上进行小扫射。

这是我想要做的http://www.afrc.af.mil/shared/media/photodb/photos/070619-F-3849K-041.JPG

永远不要只打1点。只是将Y轴随机化一点。 我想改变的是

//set movment for bullet
bullet.position.X = bullet.position.X + bullet.speed;

类似

BUllet.position.X = Bullet.position.X + Bullet.random.next(-3,3).Y + bullet.speed.

类似的东西。

1 个答案:

答案 0 :(得分:0)

您需要为每个项目符号定义一个常量值,表示y轴随时间的变化:

更改您的Bullet类

public Class Bullet 
{
    // ....
    public int Ychange;
    System.Random rand;
    public Bullet()
    {
        // ....
        rand = new Random();
        Ychange = rand.Next(-3, 3);
    }

    // Add the above stuff to your class and constructor 
}

新的UpdateBullets方法

public void UpdateBullets()
{       
    //For each bullet in our bulletlist: Update the movment and if the bulet hits side of the screen remove it form the list
    foreach(Bullet bullet in bulletList)
    {
        //set movment for bullet
        bullet.position.X += bullet.speed;
        bullet.position.Y += bullet.Ychange; 
        // Above will change at a constant value

        //if bullet hits side of screen, then make it visible to false
        if (bullet.position.X >= 800)
            bullet.isVisible = false;
    }
    //Go thru bulletList and see if any of the bullets are not visible, if they aren't  then remove bullet form bullet list
    for (int i = 0; i < bulletList.Count; i++)
    {
        if(!bulletList[i].isVisible)
        {
            bulletList.RemoveAt(i);
            i--;
        }
    }         
}

这应该有用。