Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » having a problem with animation when screen moves

This thread is locked; no one can reply to it. rss feed Print
having a problem with animation when screen moves
allegronewb
Member #13,942
January 2012

hey everyone,

just wanted to say thanks for all the help thus far (3 threads in probably 2 days 8-))

I have another issue though: So I figured out how to animate my Player on the screen. He seems to be moving just fine until he gets to the point where the screen itself moves with the camera integers. At this point, the animation starts "jumping"; I'm not really sure how to explain it but it stops becoming smooth at the point. Unfortunately I have no video-capture tools on my computer otherwise I'd gladly post an idea of what is going on. If it helps though, here's the code for the drawPlayer function and the rendering if-loop

#SelectExpand
1void InitPlayer(Player &player, int x, int y, int bx, int by, ALLEGRO_BITMAP *image) 2{ 3 player.x = x; 4 player.y = y; 5 player.bx = bx; 6 player.by = by; 7 8 player.maxFrame = 6; 9 player.curFrame = 0; 10 player.frameCount = 0; 11 player.frameDelay = 5; 12 player.frameWidth = 48; 13 player.frameHeight = 48; 14 player.animationColumns = 6; 15 player.animationDirection = 1; 16 17 player.animationRow = 0; 18 19 player.image = image; 20} 21void DrawPlayer(Player &player, float camerax, float cameray) 22{ 23 /* 24 al_draw_filled_rectangle(player.x - player.bx - camerax, player.y - player.by - cameray, 25 player.x + player.bx - camerax, player.y + player.by - cameray, 26 al_map_rgb(0,0,255)); 27 al_draw_bitmap_region(player.image, 28 */ 29 int fx = (player.curFrame % player.animationColumns) * player.frameWidth; 30 int fy = player.animationRow * player.frameHeight; 31 32 al_draw_bitmap_region(player.image, fx, fy, player.frameWidth, 33 player.frameHeight, player.x - player.frameWidth/2 - camerax, 34 player.y - player.frameHeight/2 - cameray, 0); 35 36} 37else if(ev.type == ALLEGRO_EVENT_TIMER) 38 { 39 bool right = false; 40 camerax = player.x - WIDTH/2; 41 cameray = player.y - HEIGHT/2; 42 if(keys[RIGHT]) 43 right = true; 44 if (right) 45 { 46 if(++player.frameCount >= player.frameDelay) 47 { 48 player.curFrame++; 49 player.x += keys[RIGHT] * 5; 50 player.frameCount = 0; 51 } 52 } 53 54 player.x -= keys[LEFT] * 5; 55 56 if (camerax < 0) {camerax = 0;} 57 if (camerax >= mapwidth*mapblockwidth - WIDTH) {camerax = mapwidth*mapblockwidth - WIDTH;} 58 if (cameray < 0) {cameray = 0;} 59 if (cameray >= mapheight*mapblockheight - HEIGHT) {cameray = mapheight*mapblockheight - HEIGHT;} 60 61 render = true; 62 } 63 64 if(render && al_is_event_queue_empty(event_queue)) 65 { 66 render = false; 67 68 MapDrawBG(camerax,cameray, 0, 0, WIDTH, HEIGHT); 69 DrawPlayer(player, camerax, cameray); 70 71 al_flip_display(); 72 al_clear_to_color(al_map_rgb(0,0,0)); 73 }

I know the code inside the if-loops are extremely poor right now because I was just experimenting to figure out what went wrong.

Thanks again

weapon_S
Member #7,859
October 2006
avatar

There's no such thing as an if-loop :P That can't be the code you use :P
Back-up the source file in question (if you don't already have done so) and start removing ALL code, except what is necessary to test what is going wrong.

allegronewb
Member #13,942
January 2012

Sorry, typo haha.

Good tip! I just found out the problem; its here:

#SelectExpand
1else if(ev.type == ALLEGRO_EVENT_TIMER) 2 { 3 bool right = false; 4 camerax = player.x - WIDTH/2; 5 cameray = player.y - HEIGHT/2; 6 if(keys[RIGHT]) 7 right = true; 8 if (right) 9 { 10 if(++player.frameCount >= player.frameDelay) 11 { 12 player.curFrame++; 13 player.x += keys[RIGHT] * 5; 14 player.frameCount = 0; 15 } 16 } 17 18 player.x -= keys[LEFT] * 5; 19 20 if (camerax < 0) {camerax = 0;} 21 if (camerax >= mapwidth*mapblockwidth - WIDTH) {camerax = mapwidth*mapblockwidth - WIDTH;} 22 if (cameray < 0) {cameray = 0;} 23 if (cameray >= mapheight*mapblockheight - HEIGHT) {cameray = mapheight*mapblockheight - HEIGHT;} 24 25 render = true; 26 }

The problem is the the if-statement that checks the ++frameCount with frameDelay. I used this to control the speed of the animation; unfortunately it causes the strange stutter between camera and animation. When I remove it, everything works smoothly but then the animation moves too quickly. Do you maybe have a better algorithm I can use for animation speed? Because my current one causes too much of a stutter; not too sure why ???

weapon_S
Member #7,859
October 2006
avatar

I didn't understand your code. But now that you mention there's an error there, it all makes sense ;D
You need to run along the code in your head to see what it is supposed to do, and then look at your source to see what it is actually doing...

kdevil
Member #1,075
March 2001
avatar

Try separating your player's movement from its animation. Maybe something like:

if (right)
{
  if(++player.frameCount >= player.frameDelay)
  {
    player.curFrame++;
    player.frameCount = 0;
  }
  player.x += 5.0 / player.frameDelay;
}

-----
"I am the Black Mage! I casts the spells that makes the peoples fall down!"

Dizzy Egg
Member #10,824
March 2009
avatar

camerax = player.x - WIDTH/2;

...should you be setting this after working out the player.x?

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

allegronewb
Member #13,942
January 2012

Wow I'm a moron. Thanks for the tips guys. I ended up separating the movement and animation, and then re-valuing the camerax AFTER the movement...now its working pretty smoothly ;D

Go to: