Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  Bullet shooting in mouse direction  (Read 3593 times)
0 Members and 1 Guest are viewing this topic.
Offline CogWheelz
« Reply #30 - Posted 2014-08-10 05:31:28 »

The only thing I'm having trouble with is making it shoot only when the mouse is pressed. I tried a lot of decisions and Booleans but am not getting it right.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 271
Exp: 5 years



« Reply #31 - Posted 2014-08-10 05:33:42 »

I recommend something along the lines of ClickListener, which basically works the same as MouseListeners in Swing.

EDIT: Actually you probably just want InputProcessor because you don't need scene2d stuff I'm guessing.
Offline Phased
« Reply #32 - Posted 2014-08-10 05:36:58 »

or if you don't want to use ClickListener, you could do something like

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   boolean hasReleasedMouse = true;

   public void shoot(){
      if(Input.mousePressed){
         if(hasReleasedMouse){
            hasReleasedMouse = false;
            //shoot
         }
      }else{
         hasReleasedMouse = true;
      }
   }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CogWheelz
« Reply #33 - Posted 2014-08-10 05:40:40 »

or if you don't want to use ClickListener, you could do something like

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   boolean hasReleasedMouse = true;

   public void shoot(){
      if(Input.mousePressed){
         if(hasReleasedMouse){
            hasReleasedMouse = false;
            //shoot
         }
      }else{
         hasReleasedMouse = true;
      }
   }


Pretty much what I'm doing but I have it wait about a second and then it disposes the texture. How do i remove the texture without perminantly disposing it?
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 271
Exp: 5 years



« Reply #34 - Posted 2014-08-10 05:43:03 »

What do you mean "remove the texture"? Stop rendering it?
Offline CogWheelz
« Reply #35 - Posted 2014-08-10 05:45:23 »

In the render method, I'm trying to click once and it shoots the bullet but it keeps on going because of how the render method is set up. I tried changing it so the bullet doesn't keep going, but I'd have to dispose it.

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  
 if (Gdx.input.isButtonPressed(0) && !pressed){
         entities.add(new Entity(cannonBall));
      pressed = true;
      b = true;
      }
     
      batch.begin();
     
      for (Entity e : entities) {
       
         e.update(delta);
         //if(!pressed)
         e.render(batch);
         
         if(b)
         try {
            resetCanonball();
         } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
         }
         
         
       
      }
     
      batch.end();
   }
   
   void resetCanonball() throws InterruptedException {
     
      if( frameCount++ == 10){
         
           cannonBall.dispose();
         frameCount = 0;
      }
     
   }
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 271
Exp: 5 years



« Reply #36 - Posted 2014-08-10 05:50:06 »

Don't dispose the texture, it's separate from all the bullet entities. You want to remove the bullet entity from the entities list, then it won't get updated or rendered (and eventually garbage collected).

What is your criteria for removing a bullet? Leaving screen bounds? Time limit?
Offline CogWheelz
« Reply #37 - Posted 2014-08-10 05:58:36 »

I was trying to do
1  
entities.remove(cannonBall)
.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 271
Exp: 5 years



« Reply #38 - Posted 2014-08-10 06:07:51 »

cannonBall isn't an entity, it's the texture. Maybe it should be renamed "cannonBallTexture"...

Again, what exactly is your removal criteria?
Offline CogWheelz
« Reply #39 - Posted 2014-08-10 06:14:04 »

cannonBall isn't an entity, it's the texture. Maybe it should be renamed "cannonBallTexture"...

Again, what exactly is your removal criteria?

Ah, well instead of screen bounds, I'm aiming for it to jump from point A to B only when clicked.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 271
Exp: 5 years



« Reply #40 - Posted 2014-08-10 06:19:12 »

What jumps from A to B when what is clicked?
Offline CogWheelz
« Reply #41 - Posted 2014-08-10 06:29:52 »

I'm trying to say when I want the bullet to shoot at the mouse after one mouse click  Stare
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 271
Exp: 5 years



« Reply #42 - Posted 2014-08-10 06:38:54 »

So use InputProcessor:

1  
2  
3  
4  
5  
6  
7  
8  
9  
@Override
public boolean touchDown (int x, int y, int pointer, int button) {
   if (button == Buttons.LEFT) {
      Entity ball = new Entity(cannonBallTexture);
      entities.add(ball);
      return true;
   }
   return false;
}
Offline CogWheelz
« Reply #43 - Posted 2014-08-10 06:57:30 »

Wasn't too sure where to use that method. I removed the pointer parameter because I don't know what to put in it. I used it and still no change in results
1  
touchDown(Gdx.input.getX(), Gdx.input.getY(), 0);
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 271
Exp: 5 years



« Reply #44 - Posted 2014-08-10 17:26:19 »

You don't call it yourself, and you can't change the method signature.

If you read the libGDX wiki page I linked, you would see this:

1  
Gdx.input.setInputProcessor(inputProcessor);

Quote
From this point on, all new input events will be pushed to the MyInputProcessor instance. Events are dispatched right before the call to ApplicationListener.render(), on the rendering thread.

Just make your TestScreen implement InputProcessor, and set it as the main event handler.
Offline CogWheelz
« Reply #45 - Posted 2014-08-10 22:14:06 »

Unnecessary, I figured it out. Another lesson learned, read the code. I just had to comment this part of the Entity class.
1  
2  
if (!screenBounds.contains(position.x, position.y))
         reset();

so it wouldn't reset.

Click to Play

Pages: 1 [2]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (8 views)
2014-11-26 15:22:04

toopeicgaming1999 (10 views)
2014-11-26 15:20:36

toopeicgaming1999 (6 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (24 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50
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!