Hi, everyone I am having a little trouble with the new Allegro 5. Any way, I am making a game and I have finally got the character to move smoothly. But I want it to stand still when the left key is not pressed, and the right key is not pressed. But it doesn't seem to work, when I let go of both the left or the right it stays at the last position it was running, and it doesn't stand normally. It used to work with Allegro 4, but the API has been revamped so I have to learn it again. Here is the code below:
put your code in <code></code>
Sorry about that, some other forums use the [code] syntax.
Well, for one thing, you are checking ev.keyboard.keycode when you don't know for sure that the ev.type was ALLEGRO_EVENT_KEY_DOWN or ALLEGRO_EVENT_KEY_UP. At best, you get lucky, and at worst, you access random garbage due to other data being in the union.
Another thing, you only need to call al_convert_mask_to_alpha on your right bitmap once. Don't call it over and over again in your drawing loop.
What is the purpose of all of these blocks?
else if (xpos == blah) { al_rest(0.001); xpos = blah2; }
Organize your code into logical sections :
Thanks for the post, the purpose of those blocks is to animate the sprite because it moves through each of the different sprites positions in the sprite sheet. By the way the, your pseudo code has helped my code be more organized but it has worsened the situation. It seems that my character now doesn't move smoothly anymore instead it runs in its standing position. I did some debugging and it seems that the rightpress and the leftpress from your pseudo code example keeps being false.
You do not need to use explicit double buffering in Allegro 5, and on some hardware, drawing to ALLEGRO_BITMAPs (that are not a ALLEGRO_DISPLAY backbuffer) is rather slow, so in most cases you do NOT want to double buffer like that.
Especially considering you end up with 2 or more hardware buffers, which is why the al_flip_display call is even needed.
OMG, sorry for posting that your code was faulty, but in fact after doing some indepth scan I by accident had a line of code in the xpos block that kept the rightpress false. So again sorry for wasting your precious time here is the code where I fortuitously found the little mistake that caused a hell of a problem:
I didn't realize you were using xpos to set the 'frame' of your sprite.
I would make a separate animation class, give your object a map<state , Animation*> and then set the state when it needs to change :
I did some debugging and it seems that the rightpress and the leftpress from your pseudo code example keeps being false.
Well, I don't see why that would be true. Add in a print statement under the ALLEGRO_KEY_DOWN case and make sure you are getting keypresses in the first place.
There is one thing wrong with what I posted though, it will process if(rightpress) and if(leftpress) once for each event. To prevent that, process the events in a loop and don't break out of the loop until your redraw flag is set.
OMG, sorry for posting that your code was faulty, but in fact after doing some indepth scan I by accident had a line of code in the xpos block that kept the rightpress false. So again sorry for wasting your precious time
Don't worry about it. Most of us aren't really all that high and mighty.