0

如何使我的子弹从 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.

类似的东西。

4

1 回答 1

0

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

改变你的子弹类

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--;
        }
    }         
}

这应该有效。

于 2014-09-02T00:18:21.490 回答