Question

First, this is my first week at android programming, I am trying to conceptualize how games are built. I have a setup, where I've drawn Several views, I'm just wondering how do I put all the views together, each to its thread, but in such a way they all can interact.

MainActivity: flash screen and invokes GameMenuActivity on Load

GameMenuActivity: calls GamveMenuView

GameMenuView: Keeps Track of scores/names/and launches StationActivity

StationActivity: calls StationViewsManager

StationViewsManager: hoping to be the class where I draw all objects and keep them together

/*
gameView = new GameBackgroundView(this);
movingMeteors = new MovingMeteors(this);
movingSpaceShip = new movingSpaceShip(this);
gameView.setClickable(true);
gameView.setFocusable(true);

gameView.addView(movingMeteors);
gameView.addView(movingSpaceShip);
setContentView(gameView);
this is where I'm not sure how to combine the views and draw them. Perhaps i'm not taking the problem from a right angle. any help will be appreciated 
*/

GameThreadManager: setups up threads of type StationViews here's a quick snippet, on its instantiation GameThreadManager(StationViews view) it accepts views, that can be run by the method bellow

@SuppressLint("WrongCall")
@Override
public void run() {
      long ticksPS = 1000 / FPS;
      long startTime;
      long sleepTime;
      while (running) {
             Canvas c = null;
             startTime = System.currentTimeMillis();
             try {
                    c = view.getHolder().lockCanvas();
                    synchronized (view.getHolder()) {
                           view.onDraw(c);
                    }
             } finally {
                    if (c != null) {
                           view.getHolder().unlockCanvasAndPost(c);
                    }
             }
             sleepTime = ticksPS-(System.currentTimeMillis() - startTime);
             try {
                    if (sleepTime > 0)
                           sleep(sleepTime);
                    else
                           sleep(10);
             } catch (Exception e) {}
      }
}

StationViews: superclass of views, draws transparent background,to handle other views

StationMeteors: raining meteors

StationBackground: revolving background

StationDefender: static, left side of screen and shoots bullets

Was it helpful?

Solution

If you want to show varius views over each other you need a layout to add them. Basicly you can create a RelativeLayout and set it as your content view by using setContentView() and add all of your views into the relative layout by using it's addView() method.

By the way, why do you wanna use views? I mean you already used SurfaceView for backgroud. You can also draw meteroids and the spaceship over it. That way performance will be better.

OTHER TIPS

// try this way,hope this will help you...

gameView = new GameBackgroundView(this);
movingMeteors = new MovingMeteors(this);
movingSpaceShip = new movingSpaceShip(this);
gameView.setClickable(true);
gameView.setFocusable(true);

gameView.addView(movingMeteors);
gameView.addView(movingSpaceShip);
setContentView(gameView);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top