• Advertisement


  • Content count

  • Joined

  • Last visited

Community Reputation

112 Neutral

About webdevapreneur

  • Rank
  1. creating a bullet

    I am a complete novice at game dev. My experience is all mainly ASP.NET in C# and some Java desktop app development. I have only ever made one game, it was recently and using Java. It is a space invader / Galaga analog. My approach was different than yours as a whole, but I thought this may help you, because I had the same issue. Here is my bullet class: [source lang="java"]package game; import java.awt.*; import javax.swing.ImageIcon; //class that creates and manages a bullet instance public class Bullet { //instance variables //x and y coordinates of a bullet instance private int x, y; //image that holds the image of the bullet private Image img; //variable that holds the visibility of a bullet private boolean visible; //constructor public Bullet(int xS, int yS) { x = xS; y = yS; ImageIcon newBullet = new ImageIcon("src/images/bullet.png"); img = newBullet.getImage(); visible = true; }//end of constructor //method that moves a bullet public void move() { y += -2; if(y < 5) { visible = false; } }//end of move //method that returns bounds of bullet instance(used for collision detection public Rectangle getBounds() { return new Rectangle(getX(), getY(), 10, 30); }//end of getBounds method //method to return x position of a bullet public int getX() { return x; }//end of getX //method to return y position of a bullet public int getY() { return y; }//end of getY //returns the Image of a Bullet public Image getImage() { return img; }//end of getImage //method to set visibility of a bullet public void setVisible(boolean con) { visible = con; }//end of setVisible //method to return visibility of a bullet public boolean getVisible() { return visible; }//end of getVisible }//end of class Bullet[/source] Here is my fire method in my player class: [source lang="java"]public void fire() { Bullet z = new Bullet(x + 72, y - 20); bullets.add(z); }//end of fire() [/source] then I drew the bullets in the actionPerformed handler: [source lang="java"] //event that fires every time the swing timer ends(currently set to 5 millis) public void actionPerformed(ActionEvent arg0) { if(!playerDead) { ArrayList bullets = p.getBullets(); for(int i = 0; i < bullets.size(); i++) { Bullet m = (Bullet) bullets.get(i); graphics.drawImage(m.getImage(), m.getX(), m.getY(), this); } } }[/source] here is my player constructor: [source lang="java"]public Player() { //sets visibility to true visible = true; //get the path for spaceship image ImageIcon ic = new ImageIcon("src/images/spaceCraft.png"); //loads the image file into the image object img = ic.getImage(); //starting position x = 500; y = 800; //instantiate bullet ArrayList bullets = new ArrayList(); }//end of constructor[/source] When I call the fire method the 72 and (-20) are the offset so the bullet looks like it is coming out of the nose of the fighter image that is the player instance. I am not sure of your game type but hopefully this helps!
  • Advertisement