从保存文件加载活动(Load activity from save file)

系统教程 行业动态 更新时间:2024-06-14 16:57:17
从保存文件加载活动(Load activity from save file)

直截了当地说,我做了一个游戏,一旦用户完成一个级别,他们就会转移到下一个活动。 所以基本上Activity1完成然后Activity 2加载等等。 我想保存他们获得的活动然后在主菜单上有一个继续按钮,它将在下次播放时加载它。 到目前为止,我所做的是使用Int将进度保存到共享首选项。 但我无法确定如何调用值,以便继续按钮知道加载哪个活动,如果这样做。 下面是我用来保存用户进度的代码,我以这种方式使用Int的感觉可能是错误的方式,所以任何建议都会很棒。 我对android很新,所以很好的明确答案会让我抓挠我的头哈哈:)

这是我成功的启动画面,我在这里进行了保存:

public class success1 extends Activity { private boolean mIsBackButtonPressed; private static final int SPLASH_DURATION = 2000; // 2 seconds public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SharedPreferences sp = this.getSharedPreferences("GameProgress",0); SharedPreferences.Editor editor = sp.edit(); editor.putInt("scene1",1); editor.commit(); setContentView(R.layout.level_success); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); MediaPlayer player; AssetFileDescriptor afd; try { // Read the music file from the asset folder afd = getAssets().openFd("correct.mp3"); // Creation of new media player; player = new MediaPlayer(); // Set the player music source. player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),afd.getLength()); // Set the looping and play the music. player.setLooping(false); player.prepare(); player.start(); } catch (IOException e) { e.printStackTrace(); finish(); } Handler handler = new Handler(); handler.postDelayed(new Runnable() { public void run() { finish(); if (!mIsBackButtonPressed) { // start the home screen if the back button wasn't pressed already Intent intent = new Intent(success1.this, scene2.class); success1.this.startActivity(intent); android.os.Process.killProcess(android.os.Process.myPid()); finish(); } } }, SPLASH_DURATION); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called } @Override public void onBackPressed() { // set the flag to true so the next activity won't start up mIsBackButtonPressed = true; super.onBackPressed(); } }

这只是一个带有继续按钮的调试主菜单:

public class DebugMain extends Activity { private Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.debugmenu); addListenerOnButton(); } public void addListenerOnButton() { //Select a specific button to bundle it with the action you want button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { } }); } }

Straight to the point guys, I have made a game where once the user completes a level they are moved to next activity. So basically Activity1 complete then Activity 2 loads and so on. I want to save the activity they get to then have a continue button on the main menu which will load it the next time they play. So far what i have done is save the progress to shared preferences using Int. But i cant work out how to call the values back so that the continue button knows which activity to load if that makes sence. Below is the code i am using to save the users progress, Im getting the feeling using Ints in this way may be the wrong way to do this so any advice would be great. I am fairly new to android so nice clear answers would save me scratching my head haha :)

This is my success splash screen where i do the saving:

public class success1 extends Activity { private boolean mIsBackButtonPressed; private static final int SPLASH_DURATION = 2000; // 2 seconds public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SharedPreferences sp = this.getSharedPreferences("GameProgress",0); SharedPreferences.Editor editor = sp.edit(); editor.putInt("scene1",1); editor.commit(); setContentView(R.layout.level_success); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); MediaPlayer player; AssetFileDescriptor afd; try { // Read the music file from the asset folder afd = getAssets().openFd("correct.mp3"); // Creation of new media player; player = new MediaPlayer(); // Set the player music source. player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),afd.getLength()); // Set the looping and play the music. player.setLooping(false); player.prepare(); player.start(); } catch (IOException e) { e.printStackTrace(); finish(); } Handler handler = new Handler(); handler.postDelayed(new Runnable() { public void run() { finish(); if (!mIsBackButtonPressed) { // start the home screen if the back button wasn't pressed already Intent intent = new Intent(success1.this, scene2.class); success1.this.startActivity(intent); android.os.Process.killProcess(android.os.Process.myPid()); finish(); } } }, SPLASH_DURATION); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called } @Override public void onBackPressed() { // set the flag to true so the next activity won't start up mIsBackButtonPressed = true; super.onBackPressed(); } }

This is just a debug main menu with a continue button:

public class DebugMain extends Activity { private Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.debugmenu); addListenerOnButton(); } public void addListenerOnButton() { //Select a specific button to bundle it with the action you want button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { } }); } }

最满意答案

但是我无法确定如何调用这些值,以便继续按钮知道加载哪个活动。

在加载主菜单时,您应该打开共享首选项文件并读取您的int值。 如果您的游戏中有10个级别,则可以存储一个键/值对。 level:numOfLevel。

当按下继续按钮时,您可以编写一个开关(userLevel){案例1:...案例2:....其中每个案例将根据您在加载时读取的级别启动不同的活动。

真的不是很多:)如果它是一个简单的级别结构,就像你说的那样。

"But i cant work out how to call the values back so that the continue button knows which activity to load if that makes sence."

On start when your Main Menu is loading, you should open your shared preferences file and read your int value. If you have for example 10 levels in your game, you can store one key/value pair. level: numOfLevel.

And when pressing continue button, you can write a switch(userLevel) { case 1: ... case 2:.... where each case would start a different activity based on the level you read when loading.

There really isnt much to it :) if it is a simple level structure like you said.

更多推荐

本文发布于:2023-04-12 20:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/fb013593a67675681c74e07d456287d1.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   保存文件   Load   file   save

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!