-1

我是 Android / Java 的新手。我想修改一个简单的开源安卓游戏。用真实的例子来学习编程对我来说要容易得多。

目标:我想在游戏进入下一关或玩家开始新游戏时生成随机背景。

已经:我找到了一种在用户启动应用程序时生成随机背景的方法,替换以下代码:

  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);

在:https ://code.google.com/p/bubble-shoot/source/browse/trunk/bubble-shooter-pro/src/com/likeapp/game/bubbleshooter/GameView.java

并在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;
    }
}

在:https ://code.google.com/p/bubble-shoot/source/browse/trunk/bubble-shooter-pro/src/com/likeapp/game/bubbleshooter/LevelManager.java

我假设这对于至少具有平均 Java 技能的人来说一定很容易。请根据我的代码/游戏和分步说明或解释为我提供示例。

4

1 回答 1

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() 方法之前

于 2013-04-10T03:14:51.050 回答