如何处理在一个游戏循环不同的看法?如何处理、看法、不同、游戏

2023-09-12 22:26:06 作者:讨心酸

我在做一个游戏在Android中,我有两个clases延伸SurfaceView(titleview的和GameView)。太我有我初始化,吸引了我所有的位图,该视图中的titleview的一个GameLoopThread,但它只能在这一观点。所以,当我想换到GameView,怎么我只初始化GameLoopThread在titleview的,而不是在GameView,最后是一个黑色的屏幕,因为没有什么是drawed就可以了。

有人能帮助我吗?

这是我GameLoopThread类:

  @燮pressLint(WrongCall)
公共类GameLoopThread继承Thread {

    静态最后长的FPS = 25;
    //私人GameView视图;
    私人titleview的tview;
    私人布尔运行= FALSE;

    公共GameLoopThread(titleview的titleview的){
        this.tview = titleview的;
    }

    公共无效setRunning(布尔运行){
        运行=运行;
    }

    @覆盖
    公共无效的run(){
        长ticksPS = 1000 / FPS;
        长的startTime;
        长期睡眠时间;
        而(运行){
            帆布C = NULL;
            的startTime = System.currentTimeMillis的();
            尝试 {
                C = tview.getHolder()lockCanvas()。
                同步(tview.getHolder()){
                    tview.onDraw(C);
                }
            } 最后 {
                如果(C!= NULL){
                    tview.getHolder()unlockCanvasAndPost(三);
                }
            }
            睡眠时间= ticksPS  - (System.currentTimeMillis的() -  startTime时);
            尝试 {
                如果(休眠时间大于0){
                    睡眠(睡眠时间);
                } 其他 {
                    睡眠(25);
                }
            }赶上(例外五){
                // TODO:处理异常
            }
        }
    }

}
 

解决方案

@Sergio,我有一个做类似的应用程序,视频查看,然后想象的看法,但我有每一个不同的活动课,主要活动是视频查看那么当影片完成后启动的图片活动。如果这是你正在尝试做的,

通过调用sartActivity开始你的标题来看,

私有静态最终诠释TITILE_ACTIVITY_REQUEST_ code = 1;

 意图titleIntent =新的意图(GameViewActivity.this,TitleViewActivity.class);
    startActivityForResult(imageIntent,TITLE_ACTIVITY_REQUEST_ code);
 
循环男孩好玩吗 怎么玩 循环男孩游戏介绍

在标题活动完成后,请致电您的瓦片活动以下,即应终止你的标题的活动,并重新启动你的游戏来看,

 的setResult(Activity.RESULT_OK,意图);
          完();
 

在您的游戏活动视图使用此方法来接收通知的标题视图完成后,

  @覆盖
  保护无效onActivityResult(INT申请code,INT结果code,意图数据)
  {
    如果(要求code == TITLE_ACTIVITY_REQUEST_ code)// *从titleview的通知
    {
      开关(结果code)
      {
          案例Activity.RESULT_OK:
              //* 做一点事,
              打破;
 

这code完全适用于我的应用程序,让我知道,如果您有任何其他问题。

I'm making a game in android, and I have two clases which extends SurfaceView (TitleView and GameView). Too I have a GameLoopThread that I initialize in the TitleView that draws all my bitmaps in this View, but it only works in this view. So when I want to change to the GameView, how I only initialize the GameLoopThread in the TitleView and not in the GameView, this last is a black screen because nothing is drawed on it.

Someone can help me?

This is my GameLoopThread class:

@SuppressLint("WrongCall")
public class GameLoopThread extends Thread {

    static final long FPS = 25;
    //private GameView view;
    private TitleView tview;
    private boolean running = false;

    public GameLoopThread(TitleView titleView) {
        this.tview = titleView;
    }

    public void setRunning(Boolean run) {
        running = run;
    }

    @Override
    public void run() {
        long ticksPS = 1000 / FPS;
        long startTime;
        long sleepTime;
        while (running) {
            Canvas c = null;
            startTime = System.currentTimeMillis();
            try {
                c = tview.getHolder().lockCanvas();
                synchronized (tview.getHolder()) {
                    tview.onDraw(c);
                }
            } finally {
                if (c != null) {
                    tview.getHolder().unlockCanvasAndPost(c);
                }
            }
            sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
            try {
                if (sleepTime > 0) {
                    sleep(sleepTime);
                } else {
                    sleep(25);
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    }

}

解决方案

@Sergio, I have an application that is doing something similar, video view then picture view but I have each in a different activity class, the main activity is the video view then when videos are done it starts the picture activity. if this is what you are trying to do,

start you title view by calling sartActivity,

private static final int TITILE_ACTIVITY_REQUEST_CODE = 1;

    Intent titleIntent = new Intent(GameViewActivity.this, TitleViewActivity.class);
    startActivityForResult(imageIntent, TITLE_ACTIVITY_REQUEST_CODE);

once the title activity is done, call the following from your tile activity, that should terminate your title activity and restart your game view,

          setResult(Activity.RESULT_OK, intent);
          finish();

in your game activity view use this method to receive the notification that the title view is done,

 @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) 
  {
    if (requestCode == TITLE_ACTIVITY_REQUEST_CODE) //* notification from titleview 
    {
      switch (resultCode) 
      {
          case Activity.RESULT_OK:
              //* do something,
              break;

This code works perfectly for my application, let me know if you have any other questions.

 
精彩推荐