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.

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

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.

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.

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.

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!

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.

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

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:

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.

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.

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.

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(intj = 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.

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

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.

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?

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