Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [solved]Shooting towards the mouse  (Read 494 times)
0 Members and 1 Guest are viewing this topic.
Offline Genius Coder
« Posted 2014-01-31 15:40:29 »

I am having trouble shooting a bullet towards the mouse. The  bullet shoots fine and moves towards the mouse however clicking to shoot again will adjust the old bullets rotation aswell. For example i click on the left, the bullet will fly off to the left as expected you then click to the right and another bullet will appear and fly off to the right as expect however the first bullet fired will follow the second bullet fired because the mouses position as been updated. My question to you guys is how do i make it so that once a shot has been fired its velocity doesnt change after?

Bullet Class
-----------------------------------------------------------------------
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
public Bullet(int x, int y, int type) {
      super(x, y);
      this.type = type;
      Image();
   }
   
   public void update() {
      y += velY;
      x += velX;
      y += secvelY;
      x += secvelX;
     
      if (type == 0) { currentImage = lead_Bullet;}
      if (type == 1) { currentImage = laser_Bullet;}
   }
   
   public void draw(Graphics2D g2d) {
      g2d.drawImage(currentImage, x, y, null);
      if (Game.debugMode) { g2d.draw(getBounds());  }
   }

   public static double velX;
   public static double velY;
   public static int secvelX;
   public static int secvelY;
   public int type=0;
   public Image currentImage;
   
   // Characters
   private Image lead_Bullet;
   private Image laser_Bullet;
   
   
   public void Image() {
      ImageIcon q1 = new ImageIcon(this.getClass().getResource("/misc/lead bullet.png"));
      lead_Bullet = q1.getImage();
      ImageIcon q2 = new ImageIcon(this.getClass().getResource("/mobs/player old 1/player_right_old1.png"));
      laser_Bullet = q2.getImage();
   }
    public Rectangle getBounds() {
      return new Rectangle (x,y,9,9);
    }
}


Shooting code:
--------------------------------------------------------------

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public static void shoot() {shot = true;}
   public void BulletUpdate() {
      if (shot==true && Lead_Bullets > 0) {
         shotTimer++;
         Bullet tempBullet = new Bullet(Game.player.x, Game.player.y, 0);
         Bullets.add(tempBullet);
         dx = Object_Control.msx - x;
         dy = Object_Control.msy - y;
         
         double theta = Math.atan2(Object_Control.msy - y, Object_Control.msx - x);
         dx = (int) ((Environment.speed*bulletspeed)*Math.cos(theta));
         dy = (int) ((Environment.speed*bulletspeed)*Math.sin(theta));

         Bullet.velX = dx;
         Bullet.velY = dy;
         Lead_Bullets-=1;
      }
      if (shotTimer > 0) {
         shot = false;
         shotTimer = 0;
      }
   }



ALL help is appriecated Smiley

I love making games...
Offline opiop65

JGO Kernel


Medals: 156
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #1 - Posted 2014-01-31 15:42:28 »

Why do you think making the same topic three times will yield different answers? If you want help, ask in your two other topics that are similar to this one.

Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #2 - Posted 2014-01-31 19:41:29 »

Wait, didn't you help someone out with this same problem?  Pointing

Anyway, the short answer is, you have to let the bullets keep track of where they are firing. In other words, don't let it shoot towards the mouse, but have it store the location of the mouse and shoot in that direction. Bullets should always keep track of their own direction and velocity.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline philfrei
« Reply #3 - Posted 2014-01-31 21:46:40 »

I am noticing that you ARE storing the movement data for the bullet with the Bullet class. The problem is that the information is static. Thus, whatever you last put into those variables will be shared by all instances of that class.

Without having looked at your code too deeply, it seems to me that something as simple as losing the static keywords for the variables and creating individual bullets instead of calling Bullet statically could solve your problem. You might have to write some additional code to manage the bullets and their lifetimes as a collection.

"It's after the end of the world! Don't you know that yet?"
Offline pploco1996
« Reply #4 - Posted 2014-01-31 23:00:41 »

I'd add mousex and mousey integers to my bullet class then set the velocity of the bullet with it's own mouse position field.
Offline Genius Coder
« Reply #5 - Posted 2014-02-01 06:58:38 »

Why do you think making the same topic three times will yield different answers? If you want help, ask in your two other topics that are similar to this one.

I posted a question before to find out how to get the bullet to shoot towards the mouse. I then repeated the thread accidentally forgetting that this was the site i had originally posted on so for that i apologise. However after getting my answer on one or the threads i had noticed that it had worked at first glance however after some testing i realised there was a problem with the code because the velocity would change for every bullet when i clicked. So here is the thread trying to fix this

I love making games...
Offline StumpyStrust
« Reply #6 - Posted 2014-02-01 07:56:58 »

Each bullet needs its own velocity. Why in the world you made that static I do not know.

Also, DO NOT LOAD A NEW IMAGE FOR EACH BULLET. Sorry the caps was rude. You can load an image once and then set the reference to other objects.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
Image a b c;
void loadImages()
{
a = load blah blah blah;
b = load blah blah blah;
c = load blah blah blah;
}

public class Bullet
{
//stuff.
BufferedImage image;

public Bullet(your args)
{
 image = a b c or what ever.
}

}


Sorry for bad formatting. I usually have a static class that has a reference to all images and then grab the reference when an object needs the image.

Image a  = ImageResources.get("imagename");

This way you do not have a thousands of images loaded but only one.

Offline Genius Coder
« Reply #7 - Posted 2014-02-01 10:13:43 »

I am noticing that you ARE storing the movement data for the bullet with the Bullet class. The problem is that the information is static. Thus, whatever you last put into those variables will be shared by all instances of that class.

Without having looked at your code too deeply, it seems to me that something as simple as losing the static keywords for the variables and creating individual bullets instead of calling Bullet statically could solve your problem. You might have to write some additional code to manage the bullets and their lifetimes as a collection.

Yeah the static was the problem. As silly as it sounds I have never really fully understood what static means I have just used it if ive gotten an error but now I know, Thankyou!

I love making games...
Offline StumpyStrust
« Reply #8 - Posted 2014-02-01 21:01:25 »

....google what static means in java. It is something very basic and you should really know. Don't worry about not currently knowing static but also use the google more when you do not know something.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (38 views)
2014-10-17 03:59:02

Norakomi (28 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (53 views)
2014-10-14 00:39:48

TehJavaDev (54 views)
2014-10-14 00:35:47

TehJavaDev (43 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!