首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >How to make my bullet go random by Y -+3

How to make my bullet go random by Y -+3
EN

Stack Overflow用户
提问于 2014-09-02 02:43:22
回答 1查看 109关注 0票数 0

如何使我的项目符号从X轴开始到y轴随机化+3 -3像素

代码语言:javascript
复制
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

永远不要只在一个点上击球。只是为了让Y轴随机化一点。我想要改变的是

代码语言:javascript
复制
//set movment for bullet
bullet.position.X = bullet.position.X + bullet.speed;

类似这样的东西

代码语言:javascript
复制
BUllet.position.X = Bullet.position.X + Bullet.random.next(-3,3).Y + bullet.speed.

差不多吧。

EN

回答 1

Stack Overflow用户

发布于 2014-09-02 08:18:21

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

更改你的Bullet类

代码语言:javascript
复制
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方法

代码语言:javascript
复制
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--;
        }
    }         
}

这应该是可行的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25611404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档