FLMobiGame

I have been following an excellent course on FutureLearn. This is a course designed to teach you to write mobile games for the Android platform. Find information on the next run of the course at Begin programming: build your first mobile game.  Official hashtag for the course : #FLMobiGame

I’m using this page as a place to keep notes, that I can edit easily and maybe pass along some of the information I have gained from the course.


Sounds effects

I added sound to the game with the help of the MediaPlayer library but have since found a better system using SoundPool. Thanks go to Mark Henning for introducing the method used. You can find Mark’s blog here,  He has posted on adding sound to the game and other topics related to the MOOC.

I have a file with a few sound clips and some instructions on how to use SoundPool to add sound to your game. That can be found here :- sound_pool.zip.

This is how I did it.


// I have used the method explained by Mark Henning.
// You can find his profile here :-
// https://www.futurelearn.com/profiles/97755
//
// You can find a copy of my code with sound effects here
// https://gist.github.com/robsbots/9516874
//
// To add sound clips to the game as I did, follow these steps.
//
// In the file GameThread.java change
private Context mContext;
// to
protected Context mContext;

/* Create a folder "raw" under res
Place the sound clips you wish to add to your game in that folder
*/

/*
In TheGame.java add the following to the imports section
*/
import android.media.AudioManager;
import android.media.SoundPool;

/*
Also in TheGame.java file add the following lines bellow the rest of the
variable declerations.
*/

//Create a Soundpool object
private SoundPool mSoundPool;

// Create 1 int variable for each sound clip you wish to play in the game
private int mSoundBounce;
private int mSoundLose;
private int mSoundScore;
private int mSoundPaddle;
private int mSoundNewgame;

/*
In TheGame(GameView gameVeiw) method add the following
after loading the images
*/
// Load a sound clip for each sound event you want
// Setup the new soundpool object
// I have chosen 4 streams. This means you can play 4 sounds
// at any one time
mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);

// Load the sounds into soundpool and assign each to an int defined above
mSoundBounce = mSoundPool.load(mContext, R.raw.click, 1);
mSoundLose = mSoundPool.load(mContext, R.raw.sadtrombone, 1);
mSoundScore = mSoundPool.load(mContext, R.raw.score, 1);
mSoundPaddle = mSoundPool.load(mContext, R.raw.bounce, 1);
mSoundNewgame = mSoundPool.load(mContext, R.raw.start, 1);
// These are the sounds I use. Yours will be different.
// The variable names are the ones I declared above.

/*
Add this code where you wish to play a sound
*/
// Play a sound
mSoundPool.play(mSoundNewgame, 100, 100, 1, 0, 1.0f);

// The variables you defined earlier tell soundpool which clip to play
// This one is played when the game starts.
// I placed this command at the end of the public void setupBeginning() method

// Play a sound
mSoundPool.play(mSoundLose, 100, 100, 1, 0, 1.0f);
// This sound I placed just before the end game code

// setState(GameThread.STATE_LOSE);
// Near the end of the TheGame.java file.

I have placed all the sound clips I have used along with a text file explaining this method in this file. sound_pool.zip


Graphics

To change the graphical elements of the game is quite a simple task. All the elements are stored as simple graphics files. They are kept in the res/drawable folder. You can edit these files or replace them with your own versions. I use a program called GIMP to edit graphics files. It is free and open source (and completely family friendly. Don’t panic). When you edit a file, remember the file is square or rectangular. In the case of the files for the large and small balls, the background is transparent. You can only see the foreground. This is achieved by setting the Alpha channel. To do this in GIMP, you set a certain colour to Alpha and that is then made transparent. To do this select from the menu Layer->Transparency->Colour to Alpha, and choose the colour you are currently using as a background. (This is normally white).

You can then export the image as a png. This format will allow the transparency and you do not end up with a large block around your graphics.

If you keep to the same file names the game should load up the files as normal. If you wish to add elements, you will need to load that element in the public TheGame(GameView gameView) method along with the other elements. Don’t forget to create a new bitmap object to refer to your new graphic.


Text and Text Colour

To change the texts in the game to suit your local language, or if you just wish to replace the standard texts, take a look at the file res/values/strings.xml.

This file stores the string values, or texts used by the game. I prefer to see the file as standard text rather than use the IDE funky wizard type interface. Just click the tab at the bottom of the page to switch between the two looks.

The string definitions are made up of two parts. The name of the string. This is what the software uses to refer to the strings, and the string it’s self. To change the strings, just edit the second part. ie:-


<string name="mode_lose">Lose</string>

changed to taunt your player


<string name="mode_lose">Ha Ha. You Lose</string>

To change text colour, open the res/layout/activity_main.xml file. In there, along with the layout, you will find the definitions for all the text elements used in the game. This is the score element with all the information required to place it on the screen in the right place. To change the colour, edit the android:textColor=”value”. The value is a hexadecimal number. The colour is built from 4 values. ARGB, Alpha Red Green and Blue. Alpha is the transparency of the colour. A value of 00 is transparent, or invisible, FF is solid or non transparent. a value in between this will vary the transparency of the colour.

You can use this online colour picker to choose your colour. http://www.w3schools.com/tags/ref_colorpicker.asp.

The value you receive from the colour picker is 6 digits. We need to add the alpha value to the number. I usually use FF for a solid colour, so #FF 00 00 FF would be solid blue, #FF 00 FF 00 would be solid Green, #FF FF FF 00 would be solid yellow, A mixture of blue and green.


<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="3px"
android:gravity="top"
android:text="@string/score_text"
android:textColor="#FFFFFF00"
android:textSize="24sp"
android:visibility="visible" />

Currently the score appears as a nice yellow colour in my game.

Files for week 5

These are the only files that differ from the originals supplied by Karsten. I have changed various parts for different features. Everything should be commented. Right click to save the files or take a look at the Github Gists.


Some handy links

What does Public/Private etc mean :-

http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private

Tharindu’s Blog
http://openeducationmooc.blogspot.co.uk/

Tharindu’s How-to’s
Pastebin guide:
http://openeducationmooc.blogspot.co.uk/2013/10/flmobimooc-using-pastebin-to-share-your.html

Github Gist guide:
http://openeducationmooc.blogspot.co.uk/2013/11/flmobigame-using-github-to-share-your.html

Guide to linking posts:
http://openeducationmooc.blogspot.co.uk/2014/03/flmobigame-using-futurelearn-linking-to.html

Video download guide
http://openeducationmooc.blogspot.co.uk/2013/10/download-begin-programming-moocs-videos.html

Shaing screenshots
http://openeducationmooc.blogspot.co.uk/2013/11/flmobigame-sharing-screenshots-or.html

The course FAQ
http://www.reading.ac.uk/web/files/futurelearn/begin-programming-faq.pdf

Comments 6

  1. Philip Saunders Reply
    14/03/24

    I get a error :-

    The field GameThread.mcontent is not visible

    Please help.

  2. Rob Rob Reply
    14/03/25

    Hi Philip.

    I thought I had replied to this comment, but apparently not. I appologies.

    I’m not sure where that variable is from, but I think it should read :-
    ‘mContext’ rather than ‘mContent’
    Let me know which file and what line it is on and I’ll take a look.

    Thanks

    Rob.

  3. Stevie Reply
    14/03/29

    Ta-Da! thanks for helping our progress.

    • admin admin Reply
      14/03/30

      Sorry for silly sounds. WAH Wah wah…. 🙂

  4. Quraish Reply
    14/04/12

    Hello there.

    I was wondering, MUST I use the name “raw” for my sounds folder? Why?

    • Rob Rob Reply
      14/04/12

      I belive you do. I think this is a convention from Eclipse ADT. It is also used if you wish to add video to your software. The files are taken from this folder and compressed, then stored in the APK file when the software is built.

Leave a Reply

Your email address will not be published. Required fields are marked *

*