【问题标题】:Call a method in android - practical examples在android中调用一个方法——实例
【发布时间】:2013-04-01 17:22:21
【问题描述】:

我是 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 技能的人来说,这一定很容易。请根据我的代码/游戏和分步说明或解释提供示例。

【问题讨论】:

  • 我只是想知道,为什么我在没有任何解释的情况下对我的问题投了反对票?那么,我是否应该认为这个论坛的一个可怜成员的恶作剧?或者我应该认为这是一个沮丧的 java / android 程序员的过度反应,他们不理解一个非常简单的怪诞事实,那里有急需帮助的菜鸟?

标签: java android xml class methods


【解决方案1】:

只需创建一个类(例如,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() 方法之前

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 2011-12-30
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    相关资源
    最近更新 更多