为什么这个简单的代码在手机上运行太慢? [英] Why this simple code running too slowly on mobile phone?
本文介绍了为什么这个简单的代码在手机上运行太慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
package MyGame;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.*;
///////
public class clsCanvas extends GameCanvas implements Runnable {
private boolean isRunning = true;
private Graphics g;
private midMain fParent;
////////
public Sprite cursor;
public GameDesign gameDesign;
////
public clsCanvas(midMain m) {
super(true);
fParent = m;
}
public void start(){
Thread runner = new Thread(this);
runner.start();
}
public void run() {
this.gameDesign = new GameDesign();
try{
this.cursor = this.gameDesign.getCurs();
this.cursor.defineReferencePixel(cursor.getWidth(), cursor.getHeight());
}
catch(Exception x){}
int iKey = 0;
g = getGraphics();
g.setColor(200);
g.fillRect(0, 0, 50, 50);
cursor.paint(g);
while (isRunning) {
iKey = getKeyStates();
if ((iKey & GameCanvas.FIRE_PRESSED) != 0) {
isRunning = false;
}
if ((iKey & GameCanvas.RIGHT_PRESSED) != 0) {
g.setColor(200);
g.fillRect(0, 0, getWidth(), getHeight());
cursor.setPosition(cursor.getX()+5, cursor.getY());
cursor.paint(g);
}
if ((iKey & GameCanvas.LEFT_PRESSED) != 0) {
g.setColor(200);
g.fillRect(0, 0, getWidth(), getHeight());
cursor.setPosition(cursor.getX()-5, cursor.getY());
cursor.paint(g);
}
if ((iKey & GameCanvas.DOWN_PRESSED) != 0) {
g.setColor(200);
g.fillRect(0, 0, getWidth(), getHeight());
cursor.setPosition(cursor.getX(), cursor.getY()+5);
cursor.paint(g);
}
if ((iKey & GameCanvas.UP_PRESSED) != 0) {
g.setColor(200);
g.fillRect(0, 0, getWidth(), getHeight());
cursor.setPosition(cursor.getX(), cursor.getY()-5);
cursor.paint(g);
}
flushGraphics();
try {
Thread.sleep(10);
} catch (Exception ex) {
}
}
g = null;
fParent.destroyApp(false);
fParent = null;
}
}
移动光标我正在等待〜每1步250ms.哪里有问题?
我尝试使用Thread.sleep(50),30和10-都一样:(
(全屏填充不会影响速度)
请帮忙.
我将IF替换为所有其他区别.
Moving cursor I''m waiting ~ 250ms per 1 step. Where is a problem?
I tried with Thread.sleep(50), 30 and 10 - all the same:(
(Filling full screen not affect on speed)
Please, help.
I replace IFs with this and any difference.
while ((iKey & GameCanvas.RIGHT_PRESSED) != 0) {
g.setColor(200);
g.fillRect(0, 0, width, height);
cursor.setPosition(cursor.getX()+5, cursor.getY());
cursor.paint(g);
flushGraphics();
try{
Thread.sleep(10);}
catch(Exception x){}
iKey = getKeyStates();
}
而且,如果我快速按RIGHT,光标移动的速度将比按住RIGHT键的速度快.
And if I quickly pressing RIGHT, cursor moving faster than I press and hold RIGHT key.
推荐答案
您的代码是否依赖于异常?
为什么Thread.Sleep会抛出异常?
我建议您对代码的每个部分进行一些分析,以找出每个部分有多长时间.
循环中的Sleep(10)应该足够.
Is you code relying on an exception?
Why would Thread.Sleep ever throw an exception?
I suggest you do some analysis on each section of your code to findout how long it is in each section.
Sleep(10) in a loop should be enough.
我将重构代码:
I would refactor the code:
int height = getHeight();
int width = getWidth();
while (isRunning)
{
iKey = getKeyStates();
isRunning = ((iKey & GameCanvas.FIRE_PRESSED) != 0);
if (isRunning)
{
int cursorX = cursor.GetX();
int cursorY = cursor.GetY();
bool refresh = false;
if ((iKey & GameCanvas.RIGHT_PRESSED) != 0)
{
refresh = true;
cursorX += 5;
}
if ((iKey & GameCanvas.LEFT_PRESSED) != 0)
{
refresh = true;
cursorX -= 5;
}
if ((iKey & GameCanvas.DOWN_PRESSED) != 0)
{
refresh = true;
cursorY += 5;
}
if ((iKey & GameCanvas.UP_PRESSED) != 0)
{
refresh = true;
cursorY -= 5;
}
if (refresh)
{
g.setColor(200);
g.fillRect(0, 0, width, height);
cursor.setPosition(cursorX, cursorY);
cursor.paint(g);
flushGraphics();
}
}
Thread.sleep(10);
}
这篇关于为什么这个简单的代码在手机上运行太慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文