Snake problem

Problems with Canvas, OpenGL, etc...

Snake problem

Postby Axeves » Fri Dec 31, 2010 2:46 pm

I tried to change the KEYPAD_UP from the Snake example, as i don't have a keypad/keyboard.

i just changed the
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2. public boolean onKeyDown(int keyCode, KeyEvent msg) {
  3.  
  4. if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
  5.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


lines to
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2. public boolean onTouchEvent(MotionEvent event) {
  3.  
  4. if (event.getAction() == MotionEvent.ACTION_DOWN)
Parsed in 0.031 seconds, using GeSHi 1.0.8.4




But i still wanted to control the snake from the on-screen keyboard, so i moved

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2. public boolean onKeyDown(int keyCode, KeyEvent msg) {
  3.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


to when the motionevent ended,

so i still could use the keyCodes to control the snake. but i get errors:

override cannot be resolved to a type

This method must return a result of type boolean (onTouchEvent(MotionEvent event)

this got confusing, sry, the lines i tried to change below.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.     public boolean onTouchEvent(MotionEvent event) {
  3.         if (event.getAction() == MotionEvent.ACTION_DOWN) {
  4.             if (mMode == READY | mMode == LOSE) {
  5.             }
  6.         }
  7.             }
  8.                 /*
  9.                  * At the beginning of the game, or the end of a previous one,
  10.                  * we should start a new game.
  11.                  */
  12.                 @override
  13.                 public boolean onKeyDown(int keyCode, KeyEvent msg) {
  14.                    
  15.                 initNewGame();
  16.                 setMode(RUNNING);
  17.                 update();
  18.                 return (true);
  19.  
  20.             if (mMode == PAUSE) {
  21.                 /*
  22.                  * If the game is merely paused, we should just continue where
  23.                  * we left off.
  24.                  */
  25.                 setMode(RUNNING);
  26.                 update();
  27.                 return (true);
  28.             }
  29.  
  30.             if (mDirection != SOUTH) {
  31.                 mNextDirection = NORTH;
  32.             }
  33.             return (true);
  34.  
  35.         if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
  36.             if (mDirection != NORTH) {
  37.                 mNextDirection = SOUTH;
  38.             }
  39.             return (true);
  40.         }
  41.  
  42.         if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
  43.             if (mDirection != EAST) {
  44.                 mNextDirection = WEST;
  45.             }
  46.             return (true);
  47.         }
  48.  
  49.         if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
  50.             if (mDirection != WEST) {
  51.                 mNextDirection = EAST;
  52.             }
  53.             return (true);
  54.         }
  55.  
  56.         return super.onKeyDown(keyCode, msg);
  57.     }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4
Axeves
Freshman
Freshman
 
Posts: 8
Joined: Tue Dec 28, 2010 10:10 pm

Top

Re: Snake problem

Postby Axeves » Fri Dec 31, 2010 2:48 pm

Happy new year's eve btw :)
Axeves
Freshman
Freshman
 
Posts: 8
Joined: Tue Dec 28, 2010 10:10 pm

Re: Snake problem

Postby Jems » Fri Dec 31, 2010 3:00 pm

At line 12: You need to go capitalise the o, so change it to @Override

A few other things:
The indentation is a bit weird, though maybe that was from pasting into html.
You don't need to put your return value in brackets, just say return true;
Isn't everything below line 18 unreachable code? You are returning true unconditionally there. And do you really want thing like initNewGame(); on every keyDown ?
Jems
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Thu Dec 09, 2010 7:42 pm

Re: Snake problem

Postby Axeves » Fri Dec 31, 2010 3:39 pm

When changing as you suggested (why didn't i see that?) the lines:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.            
  2. if (mMode == PAUSE) {
  3.                 /*
  4.                  * If the game is merely paused, we should just continue where
  5.                  * we left off.
  6.                  */
  7.                 setMode(RUNNING);
  8.                 update();
  9.                 return true;
  10.             }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

became unreachable, but only to the end of mMode

Suggestions? I kinda need this, as i'm working on making a main menu for it, and making this only for when you lose.
Axeves
Freshman
Freshman
 
Posts: 8
Joined: Tue Dec 28, 2010 10:10 pm

Re: Snake problem

Postby Jems » Fri Dec 31, 2010 3:52 pm

Everything below line 18 is unreachable because everything below there is inside the

public boolean onKeyDown(int keyCode, KeyEvent msg)

method, but you will always exit the method (returning true) at line 18. Presumably you don't want to be returning here. You are also doing it at line 33 if you can get to there.
Jems
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Thu Dec 09, 2010 7:42 pm

Re: Snake problem

Postby Axeves » Fri Dec 31, 2010 4:07 pm

Thanks for your help Jems, i got it working :)
Axeves
Freshman
Freshman
 
Posts: 8
Joined: Tue Dec 28, 2010 10:10 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 4 guests