我是Android / Java的新手。我想修改一个简单的开源Android游戏。用真实的例子来学习编程要容易得多。
目标:当游戏进入下一关或玩家开始新游戏时,我想生成随机背景。
已经:我找到了一种在用户启动应用程序时生成随机背景的方法,替换以下代码:
mBackgroundOrig =
BitmapFactory.decodeResource(res, R.drawable.background, options);
使用:
TypedArray imgs = getResources().obtainTypedArray(R.array.random_background);
Random rand = new Random();
int rndInt = rand.nextInt(imgs.length());
int resID = imgs.getResourceId(rndInt, 0);
mBackgroundOrig = BitmapFactory.decodeResource(res, randBackground.resID, options);
在 values / rand_bkgnd.xml 中创建一个包含字符串数组的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="random_background">
<item name="background_01">@drawable/background01</item>
<item name="background_02">@drawable/background02</item>
<item name="background_03">@drawable/background03</item>
<item name="background_04">@drawable/background04</item>
<item name="background_05">@drawable/background05</item>
<item name="background_06">@drawable/background06</item>
<item name="background_07">@drawable/background07</item>
<item name="background_08">@drawable/background08</item>
<item name="background_09">@drawable/background09</item>
<item name="background_10">@drawable/background10</item>
</string-array>
</resources>
请求:请帮我创建一个包含上述随机化后台代码的方法。我希望将此代码放在一个单独的java文件中,并且当玩家完成一个级别并进入下一级别时,能够从 goToNextLevel()方法调用它:
public void goToNextLevel() {
SharedPreferences sp =this.mContext.getSharedPreferences(
BubbleShooterActivity.PREFS_NAME, Context.MODE_PRIVATE);
currentLevel = sp.getInt(BubbleShooterActivity.PREFS_LEVEL_KEY_NAME, 0);
int maxLevel = sp.getInt(BubbleShooterActivity.PREFS_UNLOCK_LEVEL_KEY_NAME, 0);
currentLevel++;
if(maxLevel<=currentLevel){
maxLevel=currentLevel;
}
sp.edit().putInt(BubbleShooterActivity.PREFS_LEVEL_KEY_NAME, currentLevel).putInt(BubbleShooterActivity.PREFS_UNLOCK_LEVEL_KEY_NAME, maxLevel).commit();
if (currentLevel >= MAX_LEVEL_NUM) {
currentLevel = 0;
}
}
我认为对于至少具有普通Java技能的人来说,这一定非常容易。请根据我的代码/游戏以及分步说明或解释向我提供示例。
答案 0 :(得分:0)
只需创建一个类(例如,Background.class)
package com.yourpackage.name;
public class Background{
public <type of the image> RandomBackground(){
TypedArray imgs = getResources().obtainTypedArray(R.array.random_background);
Random rand = new Random();
int rndInt = rand.nextInt(imgs.length());
int resID = imgs.getResourceId(rndInt, 0);
mBackgroundOrig = BitmapFactory.decodeResource(res, randBackground.resID, options);
return mBackgroundOrig;
}
}
并在你的goToNextLevel()方法中
mBackgroundOrig = background.RandomBackground();//add this
public void goToNextLevel() {
mBackgroundOrig = background.RandomBackground(); //to this
SharedPreferences sp =this.mContext.getSharedPreferences(
BubbleShooterActivity.PREFS_NAME, Context.MODE_PRIVATE);
currentLevel = sp.getInt(BubbleShooterActivity.PREFS_LEVEL_KEY_NAME, 0);
int maxLevel = sp.getInt(BubbleShooterActivity.PREFS_UNLOCK_LEVEL_KEY_NAME, 0);
currentLevel++;
if(maxLevel<=currentLevel){
maxLevel=currentLevel;
}
sp.edit().putInt(BubbleShooterActivity.PREFS_LEVEL_KEY_NAME, currentLevel).putInt(BubbleShooterActivity.PREFS_UNLOCK_LEVEL_KEY_NAME, maxLevel).commit();
if (currentLevel >= MAX_LEVEL_NUM) {
currentLevel = 0;
}
}
您还需要在主类中包含该类,以便调用RandomBackground()方法。
例如:
Background background;
在onCreate()方法之前