Zippy
Zippy

Reputation: 3887

Using a new class? Android Game-dev

I have a game that I'm developing using surfaceview.

It has the normal methods that surfaceview would employ.

onDraw(){

Drawing here

 }

updateGame(){

Update logic here

}

run() {

while (isRunning){

onDraw()
updateGame()
}

}

So at the moment I have a splash screen, an options screen and the game itself. Each one is a separate activity (and therefore has it's own class).

But each one is crammed into one class (even the game) and it's getting difficult to see what's going on.

With my options screen, there is a few things the user can do (like click a help button, or a settings button) and I'm not sure how to fire off another class but still keeping within the same activity.

ie, I would like to use the canvas that I have set up in my current class and draw to it in a different class. Is this possible? I can't find any info about doing this.

I understand the concept of java classes, I'm just not sure how to apply them to my situation.

Or is it a better idea to do what I'm doing and just create new activity for each 'section'?

Upvotes: 0

Views: 310

Answers (3)

julien dumortier
julien dumortier

Reputation: 1303

it is difficult to make a game without using classes.

You should contact to keep the code that updates the view in the activity. But you should completely abstract the code you have in "updateGame ()" in another class. We will call this class, business class (Controller). She takes care of organizing the course of the game

Then, if you have information to back up you should use another class to access the data (Data Access Layer / Model).

I suggest you read this: http://www.leepoint.net/notes-java/GUI/structure/40mvc.html

and if you have a lot of animation and a lot of video, you should use a framework like http://www.andengine.org/

Example menu setting with new Activity And an example of game management:

    public class MainActivity extends Activity {

    private static final int CODE_MENU_SETTING = 5;
    private int mTimeGame;
    private Thread mGameThread;
    private boolean isCurrentGame, isPause;
    private Button mStartGameButton;

    private ClassBusinessGame mBusiness;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //start button if you want to start the game with a button
        mStartGameButton = (Button) findViewById(/*id button start game*/);
        isCurrentGame = false;
        isPause = false;
        //mTimeGame counts the number of seconds of game
        mTimeGame = 0;
        //the class that controls the game
        mBusiness = new ClassBusinessGame(...); 
        //example thread timer
        mGameThread = new Thread("game thread") {
            public void run() {
                while(isCurrentGame) {
                    //here, your code to update your game, view
                    mTimeGame++;
                    try {
                        Thread.sleep(1000); //pause 1 sec
                    } catch(InterruptedException e) {}
                    while(isPause) {/*load*/} //this is not a good practice to break; loop while paused
                }
            }
        };
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        // here, directly call the Menu Setting Activity
        // attention: here, stop the thread which manage your game
        isPause = true;
        Intent menuIntent = new Intent(this, MenuSettingActivity.class);
        startActivityForResult(menuIntent, CODE_MENU_SETTING); //startActivityForResult to resume game
        return true;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==CODE_MENU_SETTING) {
            //resume game
            isCurrentGame = false;
        }
    }
}

This is a basic example, many improvements are possible, but it is the idea (of a game without framework)

Upvotes: 0

yichouangle
yichouangle

Reputation: 61

Hi man I think you need a game Engine ,or you can develop a sample one like

class Screen {
    public void onShow();
    public void onHide();
    public void onUpdate();
    public void onDraw(Canvas canvas);
}

class Engine {
    Screen mScreen;

    public void setScreen(Screen s) {
        mScreen = s;
    }

    ...
}

by the way Libgdx is a good choice http://code.google.com/p/libgdx/

Upvotes: 0

brainmurphy1
brainmurphy1

Reputation: 1112

Use Fragments: http://developer.android.com/guide/components/fragments.html

They will allow you to turn your Activity into a bunch of smaller, nestable pieces. Fragments represent one part of the screen, and can govern their own behavior, like an Activity. They were added in Honeycomb, but Google provides a support package for earlier versions. Google is trying to make them standard practice from what I can tell.

Upvotes: 2

Related Questions