简体   繁体   English

从非活动类/项目开始活动

[英]Start Activity from non activity class/project

I've been developing a game for android but it is very important to know that I didn't use a single Activity until now. 我一直在为Android开发游戏,但重要的是要知道到目前为止我没有使用过一个Activity。 My game is working quite good until now but I want to make use of one activity for some settings because this is way more easy to change.. I've been reading some similar topics but I didn't succeed in my purpose yet. 到目前为止,我的游戏运行良好,但是我想在某些设置中使用一项活动,因为这样更容易更改。.我一直在阅读一些类似的主题,但我尚未达到目的。 So i need to get an intent to the SettingsActivity coming from a tap on the screen in bounds of 5,5,75,75 as you see in the code.. As I said I've been reading some similar topics and use the code but it didn't work.. Thanks for helping 因此,我需要了解一下SettingsActivity的含义,就像在代码中看到的那样,在屏幕上轻按5,5,75,75即可。如我所说,我一直在阅读一些类似的主题并使用代码但是没有用..谢谢你的帮助

public class MainMenuScreen extends Screen  {


    public MainMenuScreen(Game game) {
        super(game);
    }

    @Override
    public void update(float deltaTime) {
        Graphics g = game.getGraphics();

        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();

        int len = touchEvents.size();
        for (int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            if (event.type == TouchEvent.TOUCH_UP) {



                    if(inBounds(event,5,5,75,75)){

                        // Get an Intent to open the SettingsActivity


                    }if(inBounds(event,85,350,200,75)){

                        System.out.println("Start Last level");
                        if(SampleGame.progress==0)
                        Settings.currentLevel=0;

                        if(SampleGame.progress==1)
                            Settings.currentLevel=1;

                        if(SampleGame.progress==2)
                            Settings.currentLevel=2;

                        if(SampleGame.progress==3)
                            Settings.currentLevel=3;
                        if(SampleGame.progress==4)
                            Settings.currentLevel=4;

                        Settings.save(game.getFileIO());
                        GameScreen.state=GameState.Ready;

                        game.setScreen(new GameScreen(game));
                    }if(inBounds(event,500,350,275,75)){

                        game.setScreen(new LevelSelectionScreen(game));

                    }



                }



            }



    }

    private boolean inBounds(TouchEvent event, int x, int y, int width,
            int height) {
        if (event.x > x && event.x < x + width - 1 && event.y > y
                && event.y < y + height - 1)
            return true;
        else
            return false;
    }

    @Override
    public void paint(float deltaTime) {
        Graphics g = game.getGraphics();
        g.drawImage(Assets.menu, 0, 0);



    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {

    }

    @Override
    public void backButton() {
        android.os.Process.killProcess(android.os.Process.myPid());

    }
}

From the code I can see you are following the book Beginning Android Games . 从代码中,我可以看到您正在关注《 Beginning Android Games 》一书。 This method (single activity with "screens") indeed works great for game development. 这种方法(带有“屏幕”的单个活动)确实非常适合游戏开发。

If you do want to add extra activities it would be best to implement their starting/handling inside the Game ( AndroidGame ) class, which implements the main activity. 如果您确实想添加额外的活动,则最好在实现主要活动的GameAndroidGame )类内实现其开始/处理。

In order to do this you could add a newActivity method inside this (in the same place as the setScreen methods). 为此,您可以在其中添加一个newActivity方法(与setScreen方法位于同一位置)。 It's parameters can vary depending on what you need: you could pass an Intent directly, or you could pass a Class instead. 它的参数可以根据您的需要而变化:您可以直接传递Intent ,也可以传递Class It all depends on how much freedom you want when re-using the framework. 这完全取决于您在重用框架时需要多少自由度。

Here is an basic example of what you can add: 这是您可以添加的基本示例:

public abstract class AndroidGame extends Activity implements Game {

   public void newActivity(Intent intent)  {
      startActivity( intent ); 
   }

   public void newActivity(Class<?> cls)  {
      startActivity( new Intent( this, cls ) );
   }
}

These are just two ways to achieve your goal, and they are both very basic. 这只是实现目标的两种方法,而且都是非常基本的。 Have a look at the Activity and Intent documentation for the various ways in which you can launch activities. 请查看活动意图文档,以了解启动活动的各种方式。 Additionally, if you want to receive info back from the activity you can use startActivityForResult and implement onActivityResult in the Game class as well (maybe with the option to store the last result so it can be processed by your game. 另外,如果您想从活动中接收信息,则可以使用startActivityForResult并在Game类中实现onActivityResult (也许可以选择存储最后一个结果,以便游戏可以处理它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM