Hi!
1)I want to make an autoshooter for a space shooting game i made long ago, so the ship will shoot every 1.2 seconds if the space button is being pressed.
2)I want to pause the game, but there is no command to do that.
I have tried al_rest(1.2) but that was a stupid idea.
EDIT: I accidentally found how to pause the game.
I tried something like this
The game launches but no bullet launch.
I tried this one and i get problems
Try pressing space 3000 times it will work.
Also that while will never loop, it will only be true once, so why no if?
It won't be true even once, since he increments count before it.(assuming count started at 0)
Looks like i got it to work somehow, but it doesnt work as needed.
I have to press it each time.
I added a timer, created it outside my while(!done)
Inside the Space button i have put this,
but i cannot make any differences with the if statement,
because the timer, is not an integer!
@taron
Actually it doesnt work well!
int bulletTimer = 0; while(game_running){ if(key_space && bulletTimer == 0){ fire_bullet(); bulletTimer = 20; } if(bulletTimer > 0){ bulletTimer -= 1; } }
I wrote your idea, but the timer is not an int.
I mean it has to be a second. If I use an int it wont work.
And it didn't.
EDIT: I actually modified the counter = 20 to counter = 0 and it worked launching infinite bullets.
When i try to add something > 0 it doesnt shoot.
Any idea?
If your code editor has debugging support, drop a breakpoint on the line of code that's decrementing the timer and run it in the debugger to see if it breaks. If it doesn't, something's wrong with the block.
After dropping a breakpoint with F9 on visual studio 2012, the program went on and the game started like always. The ship could not use any bullets. This is the REAL coding i made for the autoshooting piece.
I have searched everywhere on the internet and i only got a reply from someone who has made autoshooter telling me i have to make a % 5 with the counter. I did that myself because that is the basic logic at first, but it doesnt happen.
Normally the counter increases, but the game FREEZES!
If i try to make it % 5000 it doesnt shoot at all.
I learned one thing though. How to freeze the ship when i press space!
else if (counter > 0)
Move that code to your timer event and remove the else too.
Did it like you said, it only shoots one time. If i keep the button pressed, it wont shoot anymore.
Actually i created a kind of debugging. I added text and used it to show the counter.
Using this piece of code, if i press space, the counter value went always to 20.
If i delete counter = 20 it removes by one. That is not quite logical, because what i want is to shoot every 5 frames for example.
And not removing the counter value.
After some other research i found this piece of code
But this is even worse. The ship shoots only when the space is pressed and the frame is % 5 == 0. So you have to press it several times for the ship to shoot!
After many attempts i found out something else even more amazing
It raises the frames by 15000!
I found a minimized version when the ships shoots every time the frames % 5 == 0, but the problem is that you have to press the key!
How can i make it without pressing the key?
I tried the while cycle but that crashes the whole game!
This code is completely untested.
May or may not work 'out of the box', but it should get the idea across.
Also you can use a switch statement for your event handling instead of all those if statements, whatever you prefer.
EDIT:
if(counter = counter + 15000)
It raises the frames by 15000!
there is nothing surprising about this.
you assign counter + 15000 to counter. "==" != "="
And because this value will (most likely) never be 0 it will always evaluate to true.
I get only an error with the timer.
I deleted my timer and added yours.
My timer was a bit different, because it was:
const FPS = 80;
timer = al_create_timer(1.0, FPS);
I added mine and deleted yours and still on both cases the timer is labeled with red
in this piece of code:
al_register_event_source(event_queue, timer);
My event is event_queue so no problem there, only timer is red.
It says after compiling and getting the error:
IntelliSense: argument of type "ALLEGRO_TIMER *" is incompatible with parameter of type "ALLEGRO_EVENT_SOURCE *"
oops, forgot something.
that should be:
al_register_event_source(queue, al_get_timer_event_source(timer));
The game freezes
Can you try your code with one of your projects and prove it?
Maybe it is my fault.
EDIT: After entering the code again, i got it shooting only 1 time!
if this code freezes, it's your computer.
Tested 100% this time.
Didn't bother making proper error checking or clean up though.
FINALLY IT WORKED!!!!
I will replace your code with my game and if i encounter any error i will ask again!
Thank you very much
You should try to understand how and why the code works, otherwise you'll run into problems again real fast.
You are totally right.
I will try to create the game from the start this time to get more knowledge!
EDIT: I am trying to understand your code, but that seems to me a bit strange.
Can you add comments to the code line?