Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 159353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:50:38+00:00 2026-05-11T10:50:38+00:00

Edit: This makes alot more sense to me now that i’ve taken a step

  • 0

Edit: This makes alot more sense to me now that i’ve taken a step away from the code, thanks for the help.

Just found stack overflow the other day through Coding Horror and it looks awesome. Figure that i’d ask the community about a problem i’m currently trying to work out.

I’m developing a roguelike sortof game using j2me for midp 2.0 phones. The project is still in the basic stages of development as I figure out how it’s going to work. The part i’m currently stuck on has to do with threading.

The game has a custom HaxCanvas class which extends GameCanvas and Implements runnable. It’s run method calls repaint() and then sleeps for 50 ms, resulting in a frame rate of 20 FPS. This allows me to write the rest of the game without having to put repaint everywhere and should make animations and effects easier to do later on. (at least in theory).

The flow of the game is controlled by a GameManager class, which loops through all the NPC’s on the map, taking their turns, until it’s the player’s turn. At this point I need to get input to allow the player to move around and/or attack things. I originally was calling gameManager.runUntilHeroTurn() in the keyPressed method of my HaxCanvas. However after reading up on j2me system threads I realized that putting a method with the potential to run for a while in a callback is a bad idea. However I must used keyPressed to do input handeling, since i need access to the number keys, and getKeyStates() does not support this.

Sofar my attempts to put my gameloop in it’s own thread have resulted in disaster. A strange ‘uncaught ArrayIndexOutOfBoundsException’ with no stack trace shows up after the game has run for several turns .

So i suppose my question is this:

For a ‘turn based’ game in j2me, what’s the best way to implement the game loop, allowing for input handeling only when it’s the player’s turn?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:50:38+00:00Added an answer on May 11, 2026 at 10:50 am

    I would avoid threading for the game logic as J2ME threading, depending on manufacturer of course, does not do a great job of sharing the limited resources. You will often see pauses while a thread does heavy processing. I would only recommend threads for loading or network connectivity features as in this case you will just be giving the user basic ‘Loading…’ feedback.

    To handle this, I would not have sub-loops to update each of the AI in one frame. I would do something like following in the run function:

    public void run() {     while(true) {         // Update the Game         if(gameManager.isUsersTurn()) {             // Collect User Input             // Process User Input             // Update User's State         }         else {             // Update the active NPC based on their current state             gameManager.updateCurrentNPC();         }          // Do your drawing     } } 

    You want to avoid having everything updated in one frame as 1) the updating might be slow, resulting in no immediate visual feedback for the user 2) you can’t animate each individual NPC as they make their action. With this setup you could have NPC states, NPC_DECIDE_MOVE and NPC_ANIMATING, that would allow you further control of what the NPC is doing. NPC_ANIMATING would basically put the game in a waiting state for the animation to take place, avoiding any further processing until the animation is complete. Then it could move on to the next NPC’s turn.

    Also, I would just have a gameManager.update() and gameManager.paint(g) (paint would be called from paint) that would handle everything and keep the run method thin.

    Finally, did you look into flushGraphics()? With the GameCanvas you usually create a Graphics object, draw everything to that and then call flushGraphics(), then wait. The method you mention is the way of tackling it for the Canvas class. Just thought I would mention this and post a link: Game Canvas Basics

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You might want to take a look here and here:… May 11, 2026 at 1:57 pm
  • added an answer Levenshtein Python extension and C library. https://github.com/ztane/python-Levenshtein/ The Levenshtein Python… May 11, 2026 at 1:57 pm
  • added an answer You might not have Java SDK installed in your machine,… May 11, 2026 at 1:57 pm

Related Questions

Edit: This question was written in 2008, which was like 3 internet ages ago.
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
EDIT: This question is more about language engineering than C++ itself. I used C++
Edit: This was accidentally posted twice. Original: VB.NET Importing Classes I've seen some code
EDIT : This question duplicates How to access the current Subversion build number? (Thanks
Edit: This makes alot more sense to me now that i've taken a step
I'm currently refactoring some code on a project that is wrapping up, and I
I've had this intermittent issue when using asp.net. My site is dynamically compiled. Sometimes
Assuming self-registration is used to install components as part of a larger installer program,
Somewhat related to my question about integers instead of decimals; my vendor provides a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.