Shooting Mechanic in a Cellphone Shooter Style Game

Hey There,

I’m making a Cellphone shooter style game, you know like you are the spaceship and you need to kill the enemy ships while dodging their bullets and such, its 2D… anyways… I’m trying to make a shooting mechanism, such that I could draw the bullet as it moves towards its target. Here is the thing, that wouldn’t be hard, but if another bullet is fired and such. I’m not sure how to handle that, I tried creating a new thread, for every bullet, to just iterate through a for loop, that would draw the bullet, sleep for 20ms, adjust the y coordinate and then reiterate. The issue is actually drawing though. Thus i get to my question, How do i draw outside of the paintComponent method? like can I pass the Graphics through the constructor into another class. Because if i make a thread in the paintComponent method and thus a public void run method inside of it it wants the Graphics to be final, but I couldn’t draw outside of the method, posing a slight problem as I couldn’t call another class or method to do the drawing by passing it the Graphics.

Is there something i’m missing or doing wrong?

You are using threads completely wrong, creating a thread for every bullet. Threads are not cheap, synchronizing them is not cheap, and handling their concurrency is difficult for people with years of experience using them. Simply loop through the list of active bullets and draw them in the proper location, using just one thread for the whole game.

Yep eventually figured that out, didn’t think the whole thread thing would work tbh. Managed to get it working with a friend, just needed to soundboard. Sorry for wasted space lol.