根据实体数组大小,更新方法表现不同。

时间:2017-07-07 19:14:38

标签: java arrays arraylist entity

我正在制作一个大学项目,我在使用EntiyManager更新方法时遇到了问题。这个更新系统由于某种原因使得我的子弹根据我的实体数组大小移动得更快,如果这个数组大小大于1,如果子弹没有正常移动,子弹将移动得更快。

我想让我的所有子弹正常移动。

注意:b.getVel()和vel总是2。

EntityManager方法:

public void update()
{
    if(!handler.getPaused())
    {
        for(int i = 0; i < creatures.size(); i++)
        {
            //entities[i]       at
            Creatures c = creatures.get(i);
            c.update();

            if(!bullets.isEmpty())
            {
                for (int j = 0; j < bullets.size(); j++) 
                {
                    Bullet b = bullets.get(j);
                    //Hit detection;
                    if(c.getCollisionBounds(b.getVel(), 0).intersects(b.getCollisionBounds(0, 0)))
                    {
                        c.HP -= 5;
                        removeBullet(b);
                    }
                    System.out.println(b.getVel());
                    b.update();
                }
            } 
            else System.out.println("empty");
        }

    }

}



public void draw(Graphics g)
{

    for(Entity se : staticEntities)
    {
        se.draw(g);
    }

    for(Bullet b : bullets)
    {
        b.draw(g);
    }

    for(Creatures c : creatures)
    {
        c.draw(g);
    }
}

子弹方法:

@Override
public void update() 
{
    if(vel > 0)
    { 
       txRight = (int) (x + vel + bounds.x + bounds.width) / Tile.TILESIZE;
    }  
    else
    {
       txLeft = (int) (x + vel + bounds.x) / Tile.TILESIZE; 
    }

    if(collisionWithTile(txRight, (int) (y + bounds.y) / Tile.TILESIZE) ||
       collisionWithTile(txRight, (int) (y + bounds.y + bounds.height) / Tile.TILESIZE) ||
       collisionWithTile(txLeft, (int) (y + bounds.y) / Tile.TILESIZE)  ||
       collisionWithTile(txLeft, (int) (y + bounds.y + bounds.height) / Tile.TILESIZE))
    {
        handler.getLevel().getEntityManager().removeBullet(this);
    }   

    x += vel;           
}

@Override
public void draw(Graphics g) 
{   
    g.setColor(Color.yellow);
    g.fillRect((int) x, (int) y, width, height);      
}

1 个答案:

答案 0 :(得分:0)

我现在知道为什么会这样。它正在更新两次子弹。