Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  Java 2d Alien Weapon firing system help.  (Read 3364 times)
0 Members and 1 Guest are viewing this topic.
Offline Shane75776
« Posted 2010-03-21 00:47:26 »

So i am creating a game called Asteroids (yes i need to choose a new name lol) thats an arcade shooter.
I am having an issue trying to code the Alien sprites to fire there weapons. In my game i have aliens that move in  a straight line only towards you. What i want is to make them fire there weapons ever 1-1.5seconds. So far any attempt i have had in coding this has failed. I am trying to base it off the way I fire my weapons.

Anyways as I know many of you are badass at Java 2d, so i was wonding if you could take a look at my code and maybe see if you can code that part so that it works. All the coding i have done to try and make the alien weapon fire is in the "Board.class", "Alien.class" and "AlienMissile.class" I am having the issues in the Board.class though. I have commented out all my alien weapon fire code that doesnt work so that when you run it at fire you can see the aliens themselves move down the screen on level 2 and get an idea of what im talking about.

Here is the .rar file that has the folder with the souce files and images. Its in Eclipse's folder format so you can paste it into your workspace.
http://www.filefront.com/15886141/Asteroids.rar


If you do get this to work could you please upload the source files and give me a link or post the code/changes to the code you made so that i could use it. And explain to me whats going on in it if you can, I love to learn.

Many many thanks to everyone that has replied and was able to help.

Note: Im a complete noob at java 2d. So please put in an explanation if you post code. Thanks

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #1 - Posted 2010-03-21 02:03:58 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
void update (int delta) {
   for (Alien alien : aliens)
      alien.update(delta);
}
...
public class Alien {
   static Random random = new Random();
   int fireTimer;
   void update (int delta) {
      fireTimer -= fireTimer;
      if (fireTimer <= 0) {
         // Write fire code here.
        // Eg, spawn projectile and play a sound.
        fireTimer = 1000 + random.nextInt(500); // Delay between shots.
     }
   }
}

Offline Shane75776
« Reply #2 - Posted 2010-03-21 02:14:20 »

this is code that will work with my current code?

oh wait i see what your doing, Im not having trouble with the thread or the wait time inbetween, im having an issue in creating the aliens missile sprites, displaying them, and having them move vertically down the screen.

If you downloaded my souce, uncomment all the stuff that i comment out and run it. once you get to level 2 (the level where the alien stuff begins) it freezes, look at the console and look at the error messages. Then you will understand exactly what im having an issue with.

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline moogie

JGO Knight


Medals: 12
Projects: 6
Exp: 10 years


Java games rock!


« Reply #3 - Posted 2010-03-21 02:54:09 »

i had a look at your code... just wondering why you have lots of threads for entities that do not readily seem to need their own threads... i.e. health?

I would suggest you have a single game thread where all your logic is performed then paint your board. In this way you can have the concept of game ticks which allows you to time when events occur. i.e. for your alien firing. e.g.


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  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
long time = System.currentTimeMillis();
int tick =0;

while (alive)
{
   
   do
   {
     gameTime+=(1000/60) // targeting 60 fps :)
    tick++;
     board.update(tick)
     // do other stuff


   
    // check to make sure we at least show one frame per second if things go crazy :)
   long tempTime = System.currentTimeMillis();
    if (Math.abs(tempTime - gameTime)>1000) gameTime = tempTime -1;
 
   }  while (System.currentTimeMillis() > gameTime)
   // draw the board
  board.draw();


}

where in board...

void update(int tick)
{
    for (Entity entity : entities)
    {
         entity.update(tick);
    }
}

void draw()
{
    Graphics g = getGraphics();
    for (Entity entity : entities)
    {
         entity.draw(g);
    }
}


void addEntity(Entity newEntity)
{
    entities.add(newEntity);
}


where an Entity is an abstract class representing a asteroid, ship, etc

e.g.

class Alien extends Entity
{
    private static final int FIRE_DELAY_TICKS = 90;  // assuming your game runs at 60 frames per second, this delay is 1.5 seconds.

    Alien(Board board)
   {
       this.board = board;
    }

    int fireCountDown = 0;
    Board board;

    void update(tick)
    {
        fireCountDown--;
 
        if (fireCountDown <=0)
        {
             // create a bullet Entity
           
             board.addEnitity(new AlienBullet(getPosition(),getDirection());
            fireCountDown = FIRE_DELAY_TICKS
        }

        // move alien, do other stuff
       //
  }

   void draw(Graphics g)
   {
       // draw an alien
  }
}
   
Offline Shane75776
« Reply #4 - Posted 2010-03-21 03:00:40 »

thanks for the reply, After looking at what you posted im a little confused on where that is supposed to go or how it can be used to fix my issue, Im guessing i would have to remake this game from scratch?

My issue is that im getting a null pointer exception on the line where i have "ArrayList ams = aliens.getMissile();" in my actionPerformed method. This is what im trying to fix and need help with.

The health thread you saw is just a thread delays for 20-25 seconds and then spawn a health pickup which you can grab in game.

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline moogie

JGO Knight


Medals: 12
Projects: 6
Exp: 10 years


Java games rock!


« Reply #5 - Posted 2010-03-21 03:09:55 »

Yes and no Tongue

your current implementation is quite modular and should easily retro fitted into a single threaded implementation such as the skeleton i provided earlier. The benefits of the single threaded implementation, especially tick based one, is that timing is now stupidly easy... if you want an event to occur in say 10 seconds time, just create a local variable by getting the current tick count and then adding 10*60. and now every time your game loop is executed just compare the current tick count to this variable and when they are equal perform your event.

r.e. health thread... it seems resource over kill to use a separate thread for such a simple task, also you are not guaranteed to have the exact delay using this method!
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #6 - Posted 2010-03-21 03:13:06 »

Well, downloading, unzipping, and looking through all your code to try to find what problem you are having is a lot of effort. Generally you want to do as much as you can to make it easy for others to help you. I took a look anyway. I got to level 3. The game never froze on level 2. So, I still have no idea about what help you need.

I can say, from AlienMissile#move, you should be moving the projectile based on the delta from the last frame. Eg, "y += speed * delta / 1000" where speed is pixels per second and delta is milliseconds since the last update.

I see a couple of classes that indicate you are spawning multiple threads to deal with moving projectiles, changing health, reloading, etc. Your game should have only one thread, where you update everything, then draw everything, then repeat. Use timer code like I posted above rather than sleep.

IMO, Java2D is a PITA for making games. I'd suggest moving to a framework like PulpCore or Slick.

Offline Shane75776
« Reply #7 - Posted 2010-03-21 03:15:51 »

Nate-
if you have read my earlier post you would have read that i commented out the areas in board.class that were giving the issue so you can run it and try it without the error. so if you uncomment those and run it again then you will understand. thanks for the reply and sorry for the not so easy to read code.

Moogie-
Ok i understand now, But for the moment, DO you have any idea on why i am unable to get return fire from my alien class? I think its because my "ams" arraylist as mentioned earlier is not being populated, So i believe im getting the error because thats being run before my AlienMissileThread or amThread is able to populate it. DO you know a way around this or a way to fix it for the time being? if so it would be grately appreciated, been trying to fix this single issue for 6 hours now  Tongue

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline moogie

JGO Knight


Medals: 12
Projects: 6
Exp: 10 years


Java games rock!


« Reply #8 - Posted 2010-03-21 03:42:06 »

Well you are getting a null pointer exception because in your Board class you never actually assign the alien variable and thus is null. So when you go and call the the getMissiles method, an exception is raised.

You will need to initalise or assign the alien variable before calling the getMissiles method.

from inital inspection it looks like you should move your missle specific code into the scope of your alien visibility test:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
            for(int i = 0; i < alienList.size(); i++)
            {
               Alien a = (Alien) alienList.get(i);
               if(a.isVisible())
               {
                  g2d.drawImage(a.getImage(), a.getX(), a.getY(),this);
                  ArrayList ams = a.getMissiles();
                  for(int j = 0; j < ams.size(); j++)
                  {
                     AlienMissile m = (AlienMissile) ams.get(j);
                     g2d.drawImage(m.getImage(), m.getX(), m.getY(), this);
                  }
               }
            }


but that is not that good as the missile will only live as long as the alien lives... unless that is what you are after.
Offline Shane75776
« Reply #9 - Posted 2010-03-21 03:46:16 »

I will try this out, ill get back to you as soon as i finish it checking it out. thank you sooo soo much if this works. and atm since im just learning Java 2d, I dont mind that the missiles dissapear when my alien does.

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Shane75776
« Reply #10 - Posted 2010-03-21 03:53:34 »

Ok what you posted there seems to remove the error from my paint method which is amazing. Now all i have left is the error in the action performed with my same arraylist.

This is what code looks like for it in the actionPerformed
1  
2  
3  
4  
5  
6  
7  
8  
9  
         ArrayList ams = alien.getMissiles();
         for(int i = 0; i < ams.size(); i++)
         {
            AlienMissile m = (AlienMissile) ams.get(i);
            if(m.isVisible())
               m.move();
            else
               ms.remove(i);
         }


And this is the error i get.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
   at Board.actionPerformed(Board.java:354)
   at javax.swing.Timer.fireActionPerformed(Timer.java:271)
   at javax.swing.Timer$DoPostEvent.run(Timer.java:201)
   at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
   at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
   at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
   at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline moogie

JGO Knight


Medals: 12
Projects: 6
Exp: 10 years


Java games rock!


« Reply #11 - Posted 2010-03-21 03:58:15 »

Do you understand what the problem was? if so you should be able to fix this problem as it is very similar
Offline Shane75776
« Reply #12 - Posted 2010-03-21 04:08:22 »

Yes i fixed it, so im getting the aliens to fire now using my thread except now, they seem to all fire at the same time like they are using the same thread. and firing extremely fast. not delaying like they should be in the thread.

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline Shane75776
« Reply #13 - Posted 2010-03-21 04:09:48 »

this is what i did to fix it, now they just all fire at the same time and extremely quickly.
Quote
for(int j = 0; j < alienList.size(); j++)
         {
            Alien a = (Alien) alienList.get(j);
            a.amThread = new AlienMissileThread(a);
            a.amThread.start();
            if (a.isVisible())
            {
               a.move();
   
               ArrayList ams = a.getMissiles();
               for(int i = 0; i < ams.size(); i++)
               {
                  AlienMissile m = (AlienMissile) ams.get(i);
                  if(m.isVisible())
                     m.move();
                  else
                     ms.remove(i);
               }
            }
            else
            {
               alienList.remove(j);
               a.amThread.destroy();
            }

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline Shane75776
« Reply #14 - Posted 2010-03-21 04:33:39 »

Ok so i changed the code up like this but im still getting en endless stream of return fire from the aliens. Not sure why. this is where i am completely stumped.

Paint method code
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
for(int i = 0; i < alienList.size(); i++)
            {
               Alien a = (Alien) alienList.get(i);
               if(a.isVisible())
               {
                  g2d.drawImage(a.getImage(), a.getX(), a.getY(),this);
                  ams[i] = a.getMissiles();
                  for(int j = 0; j < ams[i].size(); j++)
                  {
                     AlienMissile m = (AlienMissile) ams[i].get(j);
                     g2d.drawImage(m.getImage(), m.getX(), m.getY(), this);
                  }
               }
            }


ActionPerformed code

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  
      for(int j = 0; j < alienList.size(); j++)
         {
            Alien a = (Alien) alienList.get(j);
            ams[j] = a.getMissiles();
            amThread[j] = new AlienMissileThread(a);
            amThread[j].start();
            if (a.isVisible())
            {
               a.move();
   
               
               for(int i = 0; i < ams[j].size(); i++)
               {
                  AlienMissile m = (AlienMissile) ams[j].get(i);
                  if(m.isVisible())
                     m.move();
                  else
                     ams[j].remove(i);
               }
            }
            else
            {
               alienList.remove(j);
               //amThread[j].destroy();
           }
           

what i had done was, was make my an array of my "ams" ArrayList so each alien entity had there own and the same for the threads. but they still seem to act as if they are running of 1 single thread and ignoring the delay.

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline moogie

JGO Knight


Medals: 12
Projects: 6
Exp: 10 years


Java games rock!


« Reply #15 - Posted 2010-03-21 04:50:50 »

have you put break points in and followed the execution when an alien fires... it might reveal something
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 4
Exp: 8 years



« Reply #16 - Posted 2010-03-21 08:09:54 »

1  
2  
3  
4  
5  
for(int j = 0; j < alienList.size(); j++)

....

alienList.remove(j);


this is wrong . You should not remove an element if you are iterating on a vector like this.

Imagine you  have 10 elements, and is on the 7th iteration :
so your remove code will evaluate to that :

alienlist.remove(6);

with that, the next element (index 7) will be the 6th,  the 8th will become 7h ... well you got the point, all the elements after 6 will be shifted  left .. and the problem with that is that in the next iteration, your counter j will be 7 , and you'll skip the element which was previously on position 7 (and now is on position 6 ).

long story made short , iterate your vector backwards, it will avoid this problem :

1  
for(int j = alienList.size()-1 ; j >=0 ; j--)


ps: I don't know if that is what is causing all your problems, I didn't even read all messages of this thread . But this is definetly causing some problem .


2nd point : I see you're creating a lot of threads . Dont do that . You only need one main Thread, then do
logic, physics, drawing, sleep

There's no guarantee Threads are given the same time share, and that would screw your game physics and smoothness.

good luck

Offline farrell2k

Junior Newbie


Medals: 1



« Reply #17 - Posted 2010-03-23 04:18:27 »

Hello, Shane.

Like you, I am also very, very new to Java, and my game is also a modification of the nice tutorials from zetcode.com Smiley   After about two months of on and off working I have come up with this:
http://www.zshare.net/download/74057249ff46c6f5/

It'll show you how I handled my aliens firing, which worked out pretty well.   It'll also give you an example of my game loop, and how I actively render with a BufferStrategy, instead of overriding paint or paintComponent.

I haven't tried your code, but I am looking forward to in hopes of learning from it.   All the wonderful graphics were done by me, except the background image :p

-Tom
Offline Shane75776
« Reply #18 - Posted 2010-03-23 20:01:44 »

Thanks guys for all the help, I seem to be getting everything in order the only thing left for me to do now is Create a single thread that acts for them all. Though Not all my threads become active at the same time and some have different delays than others and doing this in one thread doesnt seem possible. though thats what everyone is telling me to do lol, so thats what im researching now.

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline Shane75776
« Reply #19 - Posted 2010-03-23 20:33:21 »

So i have Thread1, Thread2, Thread3.,Thread4. At the moment i have my threads setup so that it created a new thread when it needs to start using the thread because not all of my threads start at the same time and have the same delay also they all end at different times aswell.

How could i make a single thread class that does all of this without having to make 4 threads, and be able to have parts of this single thread start and stop at different times and have different delays than other parts within it?

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline moogie

JGO Knight


Medals: 12
Projects: 6
Exp: 10 years


Java games rock!


« Reply #20 - Posted 2010-03-24 11:29:15 »

As i alluded earlier, one way is to have one master loop which consists of calls to perform specific processing at certain frequencies.

i.e.

loop start
   tick ++
   if (tick%2) then move missile
   if (tick%100) then update health
   etc

end loop.
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #21 - Posted 2010-03-24 21:50:59 »

I posted how to do it above. Twice.

Offline zoto

Senior Member


Medals: 4



« Reply #22 - Posted 2010-03-25 20:02:18 »

The code Nate posted is the same way I do it.

One small correction
1  
fireTimer -= fireTimer;

should read
1  
fireTimer -= delta;
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #23 - Posted 2010-03-26 02:19:20 »

Oops! Good catch. Smiley

Offline Shane75776
« Reply #24 - Posted 2010-04-11 22:34:50 »

Yes I notice that now Nate. sorry, but i didnt quite understand what was happening in the code you posted, so that why I asked about it again.

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
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.

Riven (18 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (41 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (60 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!