Call a method in android - practical examples

SCOTTM

New Member
I'm new to Android / Java. I want to modify a simple open source Android game. It's much easier for me to learn programming with real examples.Goal: I want to generate random backgrounds when the game is going to the next level or when the player is starting a new game.Already: I found a way to generate random backgrounds when the user is starting the application, replacing the following code:\[code\] mBackgroundOrig = BitmapFactory.decodeResource(res, R.drawable.background, options);\[/code\]with:\[code\] 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);\[/code\]in: https://code.google.com/p/bubble-shoot/source/browse/trunk/bubble-shooter-pro/src/com/likeapp/game/bubbleshooter/GameView.javaAnd create a XML file with a string array in values/rand_bkgnd.xml:\[code\]<?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>\[/code\]Request: Please help me to create a method with the above randomize background code. I'd like to have this code in a separate java file and to be able to call it from goToNextLevel() method when the player is finishing one level and is going to the next level:\[code\]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; }}\[/code\]in: https://code.google.com/p/bubble-shoot/source/browse/trunk/bubble-shooter-pro/src/com/likeapp/game/bubbleshooter/LevelManager.javaI'm assuming this must be very easy for somebody with at least average Java skills. Please provide me examples based on my code / game and step by step instructions or explanation.
 
Top