为什么不view.invalidate立即重画在我的Andr​​oid游戏画面 [英] Why isn't view.invalidate immediately redrawing the screen in my android game

查看:149
本文介绍了为什么不view.invalidate立即重画在我的Andr​​oid游戏画面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个机器人比赛。

我有延伸活性和处理所有用户输入一个游戏类。然后,我有一个扩展视图missionView类,它在屏幕上绘制的水平。

当用户点击一个门口,我想添加一些动画。

什么情况是:该游戏要求door.open。更改状态,这样的view.onDraw函数将绘制门半开着。本场比赛调用view.invalidate,应重绘屏幕。随后的比赛休眠半秒。然后调用door.open了。第二次调用该函数时它改变了状态,所以view.onDraw函数绘制门完全打开。随后的比赛再次呼吁view.invalidate。

的问题是,它不重绘屏幕当它到达view.invalidate。如果我设置就行了一个破发点,并运行调试器,然后单击步入它不踏进我的view.onDraw功能。它甚至不能告诉我这是在执行code。

我拥有的是: 门类:

 公共布尔的open()
{
   如果(doorState == DoorState.Closed)
   {
       doorState = DoorState.Opening;

       返回true;
   }
   否则,如果(doorState == DoorState.Opening)
   {
        doorState = doorState.Open;
        返回true;
   }
   其他
   {
      返回false;
   }
}
 

游戏类:

 如果(瓦的instanceof DoorTile)
{
    DoorTile doorTile =(DoorTile)瓦;
    门门= doorTile.getDoor();

    如果(door.isClosed())
    {
        door.open();
        selectedEntity.openDoor();
        view.invalidate(); //此行不行

        尝试
        {
            视频下载(500);
        }
        赶上(InterruptedException的E1)
        {
             // TODO自动生成的catch块
             e1.printStackTrace();
        }
        door.open();

        //处理的触摸事件,以便打破switch语句
        打破;
     }
}
 

解决方案

查看#无效告诉系统重新绘制(通过的OnDraw)认为,一旦主线程进入空闲状态。也就是说,调用无效时间表视图毕竟其他眼前的工作已经完成重绘。如果code在你的游戏类被称为主线程,并把线程睡觉,你不只是暂停渲染,你是暂停输入处理一起(通常是一个坏主意)。作为一个经验法则,从来没有睡一个线程,你没有产卵自己,除非你知道自己在做什么。

如果你想有你的游戏逻辑定期耽误使用线程#睡眠,然后在一个单独的线程中运行它,并使用view.postInvalidate()信号的主线程唤醒,并调用OnDraw的。

I am trying to make an android game.

I have a game class that extends activity and handles all the user input. Then I have a missionView class that extends view and it draws the level on the screen.

When the user clicks on a door I want to add some animation.

What happens is: The game calls door.open. Changes the state so the view.onDraw function will draw the door half open. The game calls view.invalidate, which should redraw the screen. Then the game sleeps for half a second. Then it calls door.open again. The second time the function is called it changes the state so the view.onDraw function draws the door completely open. Then the game calls view.invalidate again.

The problem is that it does not redraw the screen when it gets to view.invalidate. If I set a break point on the line and run the debugger and click step into it does not step into my view.onDraw function. It can't even show me the code it is executing.

What I have is: Door class:

public boolean open()
{
   if (doorState == DoorState.Closed)
   {
       doorState = DoorState.Opening;

       return true;
   }
   else if (doorState == DoorState.Opening)
   {
        doorState = doorState.Open;
        return true;
   }
   else
   {
      return false;
   }
}

Game class:

if (tile instanceof DoorTile)
{
    DoorTile doorTile = (DoorTile) tile;
    Door door = doorTile.getDoor();

    if (door.isClosed())
    {
        door.open();
        selectedEntity.openDoor();
        view.invalidate();    // This line does not work

        try 
        {
            Thread.sleep(500);
        } 
        catch (InterruptedException e1) 
        {
             // TODO Auto-generated catch block
             e1.printStackTrace();
        }
        door.open();

        // Handled touch event so break switch statement
        break;
     }
}

解决方案

View#invalidate tells the system to redraw (via onDraw) the view as soon as the main thread goes idle. That is, calling invalidate schedules your view to be redrawn after all other immediate work has finished. If the code in your Game class is being called from the main thread and it puts that thread to sleep, you aren't just pausing rendering, you are pausing input processing all together (usually a bad idea). As a rule of thumb, never sleep a thread that you didn't spawn yourself unless you know what you are doing.

If you'd like to have your game logic periodically delay using Thread#sleep then run it in a separate thread and use view.postInvalidate() to signal the main thread to wake up and call onDraw.

这篇关于为什么不view.invalidate立即重画在我的Andr​​oid游戏画面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆