[java] j2me

Started by
1 comment, last by Thygrrr 15 years, 9 months ago
I every body.Please i need help,i have problem whith sprite.This is a part of my code.When i start the run,it build successfull but when i launch the midlet in the emulator,nothing happen.When i remove the part in brackets and launch again the midlet every thing's okay i don't understand where there is an error on that part. Thanks to answer,nice day."I use netbeans 6.1 and all import method are added" public class Jeu extends GameCanvas implements Runnable { private int width; private int height; rivate Image minImg; private Sprite minSprite; private int minX; private int minY; public Jeu() { super(false); width=getWidth(); height=getHeight(); minX = (width*5)/100; minY = (height * 75) / 100; } public void run() { while (true) { updateScreen(getGraphics()); try { Thread.sleep(sleepTime); } catch (Exception e) { } } } public void start() { >>((( try { minImg = Image.createImage("/min.png"); } catch (IOException ioex) { System.out.println(ioex); } minSprite = new Sprite(minImg, 3, 3); minSprite.setRefPixelPosition(minX, minY); )))<< Thread runner = new Thread(this); runner.start(); } private void createBackground(Graphics g) { g.setColor(0xFFFFFF); g.fillRect(0, 0, getWidth(), getHeight()); } private void updateScreen(Graphics g) { createBackground(g); minSprite.setRefPixelPosition(minX, minY); minSprite.paint(g); flushGraphics(); } private int sleepTime = 30; }
Advertisement
Is it failing to load the image? Is anything being sent to the console?

Also i notice the slash in front of the file name, should it be there?

---
Visit ssjx.co.uk for Java, Windows and Cybiko games, programs and source code!

Use getGraphics() only once! Otherwise, it will allocate a new graphics context every time the main loop executes (that means the loop in your run() method).

Also, add a Thread.yield() at the end of the main loop to make the app more responsive.

I am not sure, but you have a "start()" method in your canvas. This method is NOT called in the code snippet you gave. Call this start() method in your MIDlet's startApp() method. Also make sure you instantiate a Canvas object in the startApp() method, and set it as the current Displayable ... something along the lines of Display.getDisplay(mymidletinstance).setCurrent(mycanvasinstance)

Otherwise, what will happen is, you image is not loaded, and you will be causing NullPointerExceptions before flushGraphics() will be called, meaning in your case that the thread will die before the screen is updated.

Quote:Original post by ssjx
Also i notice the slash in front of the file name, should it be there?

Yes, it's most likely correct, assuming the image is inside the root of the JAR file.

This topic is closed to new replies.

Advertisement