The user-input is handled on the main message thread, the thread in
which your activity and your view report keypresses, touches, etc to
your activity. This will be done by the system calling your onKeyDown,
onTouchEvent, etc. methods. I think that surfaceCreated method is
called by the main message thread as well.

Never execute a 'wait' or a 'sleep' in the main message thread.

Instead, start the mPacManThread and let it run its course, i.e.
remove the 'Thread.sleep(...)' and the call to 'setRunning'
Then add the call 'postDelay(Runnable action, long delay)' with delay
set to 10 seconds. The Runnable 'action' should stop the
mPacManThread:

public void surfaceCreated(SurfaceHolder holder) {
                mPacManThread.setRunning(true);
                mPacManThread.start();

                //this thread will kill the mPacManThread thread after
10 seconds.
                someView.postDelay(new Runnable() {
                    public void run() {
                        // Stop the mPacManThread asap.
                        mPacManThread.setRunning(false);
                    }
                }, 10000);
}

Sending user-input to the mPacManThread needs to be done in your
view's onKeyDown, onTouchEvent implementations.
Use the methods Object.wait() (called by the mPacManThread) and
Object.notify() (called by the main message thread) to synchronize the
calls between the main message thread and the mPacManThread.

If you're not sure how to do this, read up on Thread handling and
synchronization in Java :-)


On Jan 17, 5:55 am, steve_macleod <[email protected]> wrote:
> Hi,
> I am writing an application to get a grip of basic sprite animation. I
> have a surfaceCreated method which looks like this:
>
> public void surfaceCreated(SurfaceHolder holder) {
>                 mPacManThread.setRunning(true);
>                 mPacManThread.start();
>                 //this thread will wait for 10 seconds, and then kill the
> mPacManThread thread
>
>                 try {
>                         Thread.sleep(10000);
>                         mPacManThread.setRunning(false);
>                         Log.d("THREAD STOPPED","The animation thread has been 
> stopped, as
> 10 seconds has elapsed.");
>
>                 } catch (InterruptedException e) {
>                         // TODO Auto-generated catch block
>                         e.printStackTrace();
>                 }
>
>         }
>
> The intention is to start the thread which manages the user input and
> animation, and make the main thread (that is the thread that started
> the view) sleep for 10 seconds, before stopping the animation loop.
>
> My understanding is that the Thread.sleep(10000) will sleep the thread
> from which the request originated (ie the main thread) for 10 seconds.
>
> However, it looks like the Thread.sleep() is actually working on the
> animation (mPacManThread) instead. Am I missing something fundamental
> here?
>
> Thanks,

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to