如何在我的Android游戏中分离UI和逻辑?
我的游戏中的动画是起伏的,我发现这是因为游戏的逻辑阻止了我的GameView中的onDraw() 但是,我不知道如何将游戏操作与GameView分开 以下是我的活动正在操作的课程的相关部分:如何在我的Android游戏中分离UI和逻辑?,android,Android,我的游戏中的动画是起伏的,我发现这是因为游戏的逻辑阻止了我的GameView中的onDraw() 但是,我不知道如何将游戏操作与GameView分开 以下是我的活动正在操作的课程的相关部分: public class Maze { ... public boolean keyDown(Object obj, int key) { switch (state) { // if screen shows title page, keys describe level of
public class Maze {
...
public boolean keyDown(Object obj, int key) {
switch (state) {
// if screen shows title page, keys describe level of expertise
// create a maze according to the user's selected level
case Constants.STATE_TITLE:
if (key >= '0' && key <= '9') {
break;
}
if (key >= 'a' && key <= 'f') {
break;
}
break;
// if we are currently generating a maze, recognize interrupt signal (ESCAPE key)
// to stop generation of current maze
case Constants.STATE_GENERATING:
if (key == ESCAPE) {
mazebuilder.interrupt();
buildInterrupted();
}
break;
// if user explores maze,
// react to input for directions and interrupt signal (ESCAPE key)
// react to input for displaying a map of the current path or of the overall maze (on/off toggle switch)
// react to input to display solution (on/off toggle switch)
// react to input to increase/reduce map scale
case Constants.STATE_PLAY:
switch (key) {
case 'k': case '8':
walk(1);
break;
case 'h': case '4':
rotate(1);
break;
case 'l': case '6':
rotate(-1);
break;
case 'j': case '2':
walk(-1);
break;
case ESCAPE: case 65385:
if (solving)
solving = false;
else
state = Constants.STATE_TITLE;
break;
case ('w' & 0x1f):
{
setCurrentPosition(px + dx, py + dy) ;
notifyViewerRedraw() ;
break;
}
case '\t': case 'm':
mapMode = !mapMode;
notifyViewerRedraw() ;
break;
case 'z':
showMaze = !showMaze;
notifyViewerRedraw() ;
break;
case 's':
showSolution = !showSolution;
notifyViewerRedraw() ;
break;
case ('s' & 0x1f):
if (solving)
solving = false;
else {
solving = true;
}
break;
case '+': case '=':
{
notifyViewerIncrementMapScale() ;
notifyViewerRedraw() ; // seems useless but it is necessary to make the screen update
break ;
}
case '-':
notifyViewerDecrementMapScale() ;
notifyViewerRedraw() ; // seems useless but it is necessary to make the screen update
break ;
}
break;
// if we are finished, return to initial state with title screen
case Constants.STATE_FINISH:
state = Constants.STATE_TITLE;
break;
}
return true;
}
在循环的中,假定屏幕被绘制4次,但实际上只有最后一帧被绘制在屏幕上
我如何转换我的迷宫
,使其单独操作所有逻辑。我曾尝试使用Asynctask
,但由于每次用户输入char值时,我的Maze
对象都会更新GameView
,因此我收到一个错误消息,说视图是从错误的线程调用的
很抱歉发布了这么多代码。我已经试着问了很多次这个问题,但不幸的是没有人帮我。如果您需要更多的上下文代码,请发表评论。任何帮助都将不胜感激 恐怕gameView.updateGraphics()方法的循环速度太快了。因为updateGraphics发生在后台,所以在第一次调用有机会正确添加之前,您将触发它4次。对处理程序增加一点延迟怎么样
int step;
for (step = 0; step != 4; step++)
{
new Hanlder.postDelayed(new Runnable()
{
@Override
public void run()
{
walkStep += dir;
gameView.updateGraphics();
}
}, 100 * (step + 1));
}
new Hanlder.postDelayed(new Runnable()
{
@Override
public void run()
{
walkFinish(dir);
}
}, 100 * (step + 1));
另外,不要忘记将dir参数修改为final:
synchronized private void walk(final int dir)
您好,经过一些测试后,仍然有跳过onDraw()的相同结果。啊,我发现了一个微妙的问题。walkFinished()方法获取了错误的值。有没有办法让一切都同步?也许在执行walkFinish()之前让迷宫线程休眠指定的时间?太棒了!你治好了我的头痛!英雄联盟请允许我建议您学习如何在android中使用处理程序。。。那会节省你很多时间。。是的,我打算这么做。创建这个游戏的目的是为了看看我有多需要了解哈哈。所以我肯定会学到一些东西。
synchronized private void walk(final int dir)