Jump to content
  • Advertisement
Sign in to follow this  

creating a bullet

This topic is 2305 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have a method that holds a constructor for a bullet

[source lang="java"] class Bullet
int x;
int y;
int width;
int height;
int bxspeed;

public Bullet(int x, int y, int width, int height, int bxspeed)
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}//end ball

public void paintBullet(Graphics g)
g.fillRect(spaceShip.xpos, spaceShip.ypos, 20, 10);
} [/source]

and when the enter key is pressed, i want a bullet to be created at the middle of the spaceShip, but whenever i create one it starts at the top left corner of the spaceship

Share this post

Link to post
Share on other sites
I'd guess Line 20:
g.fillRect(spaceShip.xpos+xOffset, spaceShip.ypos+yOffset, 20, 10);

where the offsets are the x and y distances from the top left corner of the spaceship to the point where you actually want it to spawn (middle of spaceShip)

Share this post

Link to post
Share on other sites
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;

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

}//end of fire()

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)

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


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!

Share this post

Link to post
Share on other sites
Let me suggest something a little different than what webdevap... has.

You should not have the bullet list in the Player class. It should be a global list (or held in your Game class if you have one). That bullet list should be able to contain bullets fired by the player or by enemies.

Also, you might want to inherit from a Super class for any object that has physical properties (X, Y, Height, Width, Rotation, Image, and X and Y Velocities).

Something like this (in pseudo-code, only listing the members and some functions)

class PhysicalObject
float xPos;
float yPos;
float xVelocity;
float yVelocity;
float Angle;
int Height;
int Width;
Image ObjectImage; // or maybe you have an animation class, if it animates

void Update()
xPos += xVelocity;
yPos += yVelocity;

void Draw()
ScreenDraw(ObjectImage, xPos, yPos);

class Bullet : public PhysicalObject
int Damage; // how hard it hits
bool IsPlayerOwner; // determines if it's a player bullet or enemy
int TimeToLive; // how long the bullets stays alive

Then, when ever anything fires a bullet, you create Bullet, add it to a list. In your Game Update, you loop through the BulletList, move the bullets, and check for collisions. If it collides, you apply the damage and remove it from the list.

Good Luck! Edited by BeerNutts

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!