?Delay for keyboard which is independent from drawing

Problems with Canvas, OpenGL, etc...

?Delay for keyboard which is independent from drawing

Postby Gargo » Sun Mar 27, 2011 10:36 pm

Code: Select all
public void run() {
         while(mRun) {
            Canvas c = null;
            if(mPassedTime <= 0) {
               updateGameState();
               mPassedTime = System.currentTimeMillis();
            }
            if(System.currentTimeMillis() - mPassedTime >= mKeyIntervalInMillis)
               mPassedTime = 0;
            if(mTimerTask == null) {
               mTimerTask = new TimerTask() {
                  public void run() {
                  }
               };
               mTimer.schedule(mTimerTask, mTaskIntervalInMillis);
            }
            try {
                    c = mSurfaceHolder.lockCanvas(null);
                    draw(c);
                } finally {
                    if (c != null) {
                        mSurfaceHolder.unlockCanvasAndPost(c);
                    }
                }
         }
      }
      
      private void updateGameState() {
         for(;;) {
            GameEvent event = mEventQueue.poll();
            //Если список событий пуст
            if(event == null)
               break;
            //Для нажатия клавиш
            if(event instanceof KeyGameEvent) {
               processKeyEvent((KeyGameEvent)event, null);
            }
         }
      }

      private void processKeyEvent(KeyGameEvent event, Object context) {
         if(event.keyCode == KeyEvent.KEYCODE_DPAD_UP) {
            bottle.figure.up();
         }
         else if(event.keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
            bottle.figure.down();
         }
         else if(event.keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
            bottle.figure.left();
         }
         else if(event.keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
            bottle.figure.right();
         }
      }


I try to write Tetris. I have tried a lot of code, but it still doen't work properly. The figure rotates too slow or two times at once. Can anybody help with this?
User avatar
Gargo
Freshman
Freshman
 
Posts: 3
Joined: Sat Aug 29, 2009 3:39 pm

Top

Re: ?Delay for keyboard which is independent from drawing

Postby raj_J2ME » Tue Mar 29, 2011 8:15 am

Gargo, I have review your code and have couple of suggestion. Please correct me if I went wrong somewhere.

1. updateGameState(); is already on the thread and hence inside the loop, so there is no point for infinite for loop in it definition (I am talking about the infinite for loop inside the updateGameState();)

2. Your code inside the outer run() seems to be much complicated, I would recommend you to share that what exactly you are trying to do?
Thanks with Regards,
Raj - The Kernel
raj_J2ME
Master Developer
Master Developer
 
Posts: 311
Joined: Tue Feb 15, 2011 7:40 am
Location: The Capital of India,

Top

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

Who is online

Users browsing this forum: No registered users and 3 guests