Jump to content

  • Log In with Google      Sign In   
  • Create Account


creating a bullet


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 burnt_casadilla   Members   -  Reputation: 440

Like
0Likes
Like

Posted 21 August 2012 - 05:40 PM

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.setColor(Color.blue); 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

If you see a post from me, you can safely assume its C# and XNA :)


Sponsor:

#2 chondee   Members   -  Reputation: 135

Like
0Likes
Like

Posted 21 August 2012 - 07:46 PM

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)

#3 webdevapreneur   Members   -  Reputation: 112

Like
1Likes
Like

Posted 21 August 2012 - 08:01 PM

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 instancepublic 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!

#4 burnt_casadilla   Members   -  Reputation: 440

Like
0Likes
Like

Posted 21 August 2012 - 08:49 PM

thanks! that was perfect and also helped me with the other problems i was having :D

If you see a post from me, you can safely assume its C# and XNA :)


#5 Malabyte   Members   -  Reputation: 589

Like
0Likes
Like

Posted 22 August 2012 - 11:18 AM

Yeah, the location of any image (or component body) on the screen is always(?) at its top left corner, not its center.

Edited by DrMadolite, 22 August 2012 - 11:18 AM.

- Awl you're base are belong me! -

- I don't know, I'm just a noob -


#6 BeerNutts   Crossbones+   -  Reputation: 2855

Like
0Likes
Like

Posted 22 August 2012 - 12:58 PM

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, 22 August 2012 - 01:04 PM.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS