Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Problem with a 2d game in java...

Options
  • 07-01-2006 4:50am
    #1
    Registered Users Posts: 8,449 ✭✭✭


    Hey guys, I have a space invaders type game pretty much done but i'm trying to limit the amount of player bullets on screen to 5.

    Player (the ship) and Bullet are two seperate sub-classes of Actor and I want an int in the Player class to be updated in a method in the Bullet class.

    It just wont seem to work.

    I have within a method in Bullet a part that removes the bullet when it goes off-screen (y < 0) and it is here that i want to change an int in the Player class.

    Then I want the int to be used again in the player class.

    If i havent made any sense i could post the code as well.

    Thanks


Comments

  • Registered Users Posts: 304 ✭✭PhantomBeaker


    It seems pretty simple.

    If your Player class fires bullets, and you call fire() or similar on your player to fire the bullets, just keep track of the number of bullets it has out there.

    Just keep track of what Player fired what Bullet when it's removed. It seems fairly simple.


  • Registered Users Posts: 877 ✭✭✭clearz


    Create a static int in your Bullet class

    public static int totalBullets;

    Then increment it in your Bullets constructor and decrement it in your remove method.
    Then just call Bullet.totalBullets at any time to check the total bullets in the game.


  • Registered Users Posts: 304 ✭✭PhantomBeaker


    clearz, would that work if the OP had more than one Player object on the go at one point? That would limit the number of bullets to 5 among all players rather than 5 each.

    That said, I don't think I've ever seen a 2 player space invaders :)


  • Registered Users Posts: 8,449 ✭✭✭Call Me Jimmy


    Hey guys thanks for the replies, I got it workin :)

    i actually did try the two player thing, added another of the Player class as the keys for movement etc. were defined in the player class using KeyEvent KeyPressed and KeyReleased I didnt know how to make different controls for a different instance of (player 2) the same class. I wanted player 2 to have WSAD controls but it just wouldnt work

    here's the Player class Code



    import java.awt.event.KeyEvent;

    public class Player extends Actor
    {

    protected static final int MAX_SHIELDS = 200;
    public static final int MAX_BOMBS = 5;
    protected static final int PLAYER_SPEED = 8;
    protected int vx;
    protected int vy;
    protected int fireOtherCannon = 1;
    private boolean up,down,left,right;

    private int clusterBombs;
    private int ammo;



    private int score;
    private int shields;





    public Player(Stage stage)
    {
    super(stage);
    setSpriteNames( new String[] {"ship0.gif"});


    clusterBombs = 2;
    shields = MAX_SHIELDS;
    ammo =2;

    score = 0;
    }


    public void act()
    {
    super.act();
    x+=vx;
    y+=vy;
    if(x<0)
    x=0;
    if(x>Stage.WIDTH - getWidth())
    x=Stage.WIDTH-getWidth();

    if(y<0)
    y=0;
    if(y > Stage.PLAY_HEIGHT-getHeight())
    y = Stage.PLAY_HEIGHT - getHeight();

    if(getShields() < 0 && currentFrame == 9)
    {
    stage.gameOver();

    }
    }



    public int getVx()
    {
    return vx;
    }
    public void setVx(int i)
    {
    vx = i;
    }
    public int getVy()
    {
    return vy;
    }
    public void setVy(int i)
    {
    vy = i;
    }

    protected void updateSpeed()
    {
    vx=0;
    vy=0;
    if(getShields() > 0)
    {
    if(down) vy = PLAYER_SPEED;
    if(up) vy = -PLAYER_SPEED;
    if(left) vx = -PLAYER_SPEED;
    if(right) vx = PLAYER_SPEED;
    }
    }

    public void keyReleased(KeyEvent e)
    {
    switch (e.getKeyCode())
    {
    case KeyEvent.VK_DOWN : down = false;break;
    case KeyEvent.VK_UP : up = false;break;
    case KeyEvent.VK_LEFT :left = false; break;
    case KeyEvent.VK_RIGHT : right = false;break;
    }
    updateSpeed();
    }


    public void keyPressed(KeyEvent e)
    {
    switch (e.getKeyCode())
    {
    case KeyEvent.VK_DOWN : down = true;break;
    case KeyEvent.VK_UP : up = true;break;
    case KeyEvent.VK_LEFT : left = true; break;
    case KeyEvent.VK_RIGHT : right = true;break;
    case KeyEvent.VK_SPACE : fire();break;
    case KeyEvent.VK_C : fireCluster();break;
    }
    updateSpeed();
    }

    public int getAmmo()
    {
    return ammo;
    }

    public void addAmmo()
    {
    ammo++;
    }





    public void fire()
    {

    Bullet b = new Bullet(stage,this);

    if(ammo == 0)
    return;


    if(fireOtherCannon % 2 == 0)
    {
    b.setX(x+38);
    }
    else
    {
    b.setX(x);
    }
    b.setY(y-(b.getHeight()-25));
    stage.addActor(b);
    stage.getSoundCache().playSound("photon.wav");
    fireOtherCannon++;
    ammo--;
    }


    public void fireCluster()
    {
    if(clusterBombs ==0)
    return;

    clusterBombs--;
    stage.addActor( new Bomb(stage,Bomb.UP_LEFT,x,y));
    stage.addActor( new Bomb(stage, Bomb.UP,x,y));
    stage.addActor( new Bomb(stage, Bomb.UP_RIGHT,x,y));
    stage.addActor( new Bomb(stage, Bomb.LEFT,x,y));
    stage.addActor( new Bomb(stage, Bomb.RIGHT,x,y));
    stage.addActor( new Bomb(stage, Bomb.DOWN_LEFT,x,y));
    stage.addActor( new Bomb(stage, Bomb.DOWN,x,y));
    stage.addActor( new Bomb(stage, Bomb.DOWN_RIGHT,x,y));

    stage.getSoundCache().playSound("cluster.wav");
    }

    public int getScore()
    {
    return score;
    }

    public void addScore(int i)
    {
    score += i;
    }

    public void setScore(int i)
    {
    score = i;
    }

    public int getShields()
    {
    return shields;
    }

    public void setShields(int i)
    {
    shields = i;
    }

    public void addShields(int i)
    {
    shields+=i;
    if(shields> MAX_SHIELDS)
    shields = MAX_SHIELDS;
    }

    public int getClusterBombs()
    {
    return clusterBombs;
    }

    public void setClusterBombs(int i)
    {
    clusterBombs = i;
    }

    public void removePlayer()
    {
    }





    public void collision(Actor a)
    {
    if(a instanceof Monster)
    {
    a.remove();

    setSpriteNames( new String[] {"ship1.gif","ship0.gif"});
    addScore(40);
    addShields(-40);
    setFrameSpeed(5);


    }

    if(a instanceof Laser)
    {
    a.remove();
    addShields(-25);
    stage.getSoundCache().playSound("shiphit.wav");
    }



    if(getShields() < 0)
    {
    setSpriteNames( new String[] {"shipexplode0.gif","shipexplode1.gif","shipexplode2.gif","shipexplode3.gif","shipexplode4.gif","shipexplode5.gif","shipexplode6.gif","shipexplode7.gif","shipexplode8.gif","shipexplode9.gif"});
    setFrameSpeed(2);
    stage.getSoundCache().playSound("explosion.wav");
    }

    }

    }


  • Registered Users Posts: 877 ✭✭✭clearz


    clearz, would that work if the OP had more than one Player object on the go at one point? That would limit the number of bullets to 5 among all players rather than 5 each.

    That said, I don't think I've ever seen a 2 player space invaders :)

    Your right Phantom but I was presuming that there would only one player. There are a ton of other ways to find the number of bullets and I would probably not use that static variable method myself. I was just feeling lazy when writing the reply :D


  • Advertisement
Advertisement