Character Animation

All your problems with Audio, Video and Images.

Character Animation

Postby BrutoJPO » Thu Oct 29, 2009 8:27 pm

Hi guys, I need to do an animation of a character walking, like a game. When the player press forward the animation of the character walking to the right works and when the character goes left the same thing occurs.

I already tried a lot of things. The animation I can do, but putting movement on it is hard, because you need to correctly treat the user keystrokes.

Some sugestion?

John Peter
BrutoJPO
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Oct 21, 2009 7:41 pm
Location: Brazil

Top

Postby chimaera » Wed Nov 04, 2009 12:47 am

I'm not sure I correctly understand your question, so sorry if I reply way off.

To handle keystrokes in games, I'd use two threads:
- the UI thread catches the key strokes and forwards them to the rendering thread
- the rendering thread displays the animation at the correct timing. I'd also use a state variable in the rendering thread to handle the key states.

Something like this
In the main activity, so in the UI thread:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public boolean onKeyDown(int keyCode, KeyEvent event)
  2.  
  3. {
  4.  
  5. if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT)
  6.  
  7.         {
  8.  
  9.             view.queueEvent(new Runnable()
  10.  
  11.             {
  12.  
  13.                 // This method will be called on the rendering
  14.  
  15.                 // thread:
  16.  
  17.                 public void run()
  18.  
  19.                 {
  20.  
  21.                     renderer.goLeft(true);
  22.  
  23.                 }});
  24.  
  25.             return true;
  26.  
  27.         }
  28.  
  29.         return super.onKeyDown(keyCode, event);
  30.  
  31.     }
  32.  
  33. }
  34.  
  35.  
  36.  
  37. public boolean onKeyUp(int keyCode, KeyEvent event)
  38.  
  39. {
  40.  
  41. if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT)
  42.  
  43.         {
  44.  
  45.             view.queueEvent(new Runnable()
  46.  
  47.             {
  48.  
  49.                 // This method will be called on the rendering
  50.  
  51.                 // thread:
  52.  
  53.                 public void run()
  54.  
  55.                 {
  56.  
  57.                     renderer.goLeft(false);
  58.  
  59.                 }});
  60.  
  61.             return true;
  62.  
  63.         }
  64.  
  65.         return super.onKeyDown(keyCode, event);
  66.  
  67.     }
  68.  
  69. }
  70.  
  71.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

in the rendering thread:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class renderer
  2.  
  3. {
  4.  
  5. private  boolean go_left = false;
  6.  
  7. public void goLeft(boolean value)
  8.  
  9.         {
  10.  
  11.                 go_left = value;
  12.  
  13.         }
  14.  
  15.  
  16.  
  17. public void render()
  18.  
  19.   while(true)
  20.  
  21. {
  22.  
  23.    // do timing
  24.  
  25.   if (go_left)
  26.  
  27.   {
  28.  
  29.     // do left animation
  30.  
  31.   }
  32.  
  33. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
chimaera
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Jun 19, 2009 1:42 am

Postby BrutoJPO » Wed Nov 04, 2009 5:13 pm

Thanks for the reply chimaera. My problem is to do a character walking animation, the documentation in the android community is poor and I'm suffering a lot to do it. Do you know beat'em up games? Like Streets of Rage, Final Fight, Cadillac and Dinosaurs? My game is about this, and I need to do the animation when the character is walking, I'll try to use your code because I sense that the idea will help me, but I'm not sure if this will fix all the problem.

One more time, thank you anddev community. It's because of people like you that the android community is becoming stronger day by day.

John Peter
BrutoJPO
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Oct 21, 2009 7:41 pm
Location: Brazil

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Google [Bot] and 15 guests