Hi all:
I was wondering if it was legal in allegro5 to have a
MOUSE DOWN inside a Timer function. The code below works
as far as the Timer, but allegro5 does NOT recognize when
I click the mouse button.
Sorry code got kind of chopped off, but you can see the code.
If this is NOT legal, how could it be written to work.
Thanks for your time!
The EVENT object is a union of specialized events. In a timer event, the mouse event data is invalid. You need to get mouse data elsewhere. Personally I keep variables that get updates during mouse events.
Hi Daniel:
Thank you for the info! I was afraid I had hit another
bump in the road! I will have to try something else!
Thanks again!
I think what you're looking for is something slightly different than what you tried. Your condition is impossible because if the event type is a TIMER event, then it can't be a MOUSE_DOWN event.
Thanks Edgar:
I am looking over your post. I am still having trouble
understanding how the mouse click is connected to the timer
function. I will work on the info you posted and maybe I can
see how it is connected.
Thanks for the information!
In allegro 5, we no longer use callbacks with timer functions. You process the event and then handle it at your leisure.
I am in effect replicating the ALLEGRO_MOUSE_STATE by using the mouse button up and down events to get the pressed state of the button. Then inside our reaction to the timer event, we process our mouse state so it only happens when the timer has a tick on it. This makes the rate steady with your timer.
Hi Edgar:
That was the answer I was looking for.
Thanks again. Have a great day
for(i=0; i < 30; i++) { ball_x += cos(angle) * speed; ball_y += sin(angle) * speed; al_clear_to_color(al_map_rgb(20, 20, 20)); al_draw_filled_circle(ball_x, ball_y, 15, al_map_rgb(255, 0, 0)); al_flip_display(); update(); }
Hey Scooter, just checking, does this code do what you want it to? Typically we only flip the display when we draw everything else on a timer event.
Hi Edgar:
I had a few minutes and thought I would check in and you had
replied. Actually you solved the problem but I have many more
problems on top of that. This is the first time I have tried
to run two different things at the same time and it is NOT
going well at all. It is a very simple game I was trying my
hand at. I got the idea off the internet. I am trying to run
two timers at the same time.
I tried to post a picture of what I was trying to do, but couldn't
fine a way to do it. You used to be able to upload but not
anymore I guess.
Anyway, I have a round disc with a slot in it that rotates. You
then bounce a ball off the side and try to get the ball into the
slot. That's about it. Very simple I thought.
I can't seem to get the ball to move correctly and the rotating
disc seems to disappear. Then I would have to get involved in
collision detection. I was going to continue trying to figure it
out and get back with you later,
Thanks for the reply.
Most likely you only need one timer and two variables for ticks that increment on different rates.
As for collision detection, keep it simple as you can. A slot in a disc can be represented by a line segment on the radii of the disc. Then you test if the ball is at a certain angle to the center of the disc and see if it is close enough to be inside.
Edgar:
I now have something that kinda works. I cut out the mouse and angles.
I now shoot the ball straight into the rotating disc. One problem still
remains: when I shoot the ball the rotating disc disappears. I am still
running two timers with update function for each timer.
What I still don't understand is how to connect each timer to its update
function and keep everything on the screen. I have a timer that rotates
the disc and a timer that shoots the ball. Are these functions declared
outside of main and the update functions declared inside main?
I don't know how this is done. I forgot to tell you I am using the space
bar to shoot the ball.
Thanks
The event variable gives you the timer that triggered the event. Use that to determine which update is needed.
That being said, you don't need a timer for each object. You really just need one timer and update all objects when triggered.
An example of what DanielH and I are saying :
Hi Daniel and Edgar:
Just a suggestion, but input, logic and drawing should ALL be separated.
1. process input
2. process logic
3. process drawing
What are the speeds of your timers, can't you adjust to use one? Or adjust ball velocity to match the speed of one timer.
You have to check which timer it is in both cases:
// this shoots the ball if (ev.type == ALLEGRO_EVENT_TIMER && ev.timer.source == ball_timer)
Edgar:
Here is what I have now. I think everything is here. I have no
idea how the timing flows in the program. I wanted to upload all
the bitmaps but never found a way to upload the zip file. I wanted
you to look at my file and tell me where the timing should be placed.
No hurry, just when you have the time. Thanks!
Oh, where to start?
You only have one timer now. You won't need to check if the timer source matches anymore.
You can also consolidate it all under one allegro_event_timer if statement
Why are you doing this 60 times?
for(i = 0; i < 60; i++) { al_clear_to_color(al_map_rgb(0,0,0)); al_draw_bitmap(image2, 500, 727, 0); al_draw_bitmap(image3, 383, 20, 0); al_draw_bitmap(image4, 582, 495, 0); al_draw_filled_circle(ball_x, ball_y, ball_dia, al_map_rgb(255, 0, 0)); al_flip_display(); update(10); }
I think he wants a short animation of the ball going into the disc.
Hi Daniel:
I tried what you posted and I still have the same problem.
The disc disappears while the ball is moving. I am not sure,
but I think this is something I have already tried before.
Any yes, Edgar is right. This gave me more control of the ball,
mainly to be sure it stopped moving.
Please take another look to see what is wrong.
Thanks guys!!!!!!!!!
You can do animation this way, but be aware it is blocking your program from getting input. You need to draw the disc in the same section of code you draw your ball. That's why it disappears. Track the state of the 'start' variable. bticks and dticks do no good if you don't use them. You need variables to track the accumulated time. Then check if one of the variables has ticked or not.
What you should try to achieve is freeing your logic from drawing. Always draw the whole screen, based on what is visible. And use your logic to determine what is where and when.
Hope this helps.