0

I'm a completely newbie to android programming, having done some java for my computing levels but nothing too complex!

I'm working on a game where an object falls down the screen and has to be sorted into the relevant 'box' when it reaches the bottom. I've got a surface view running with a thread etc, using canvas draw methods, however, i can't for the life of me see how i will be able to make the falling object reach a speed where it'll present a challenge to the user.

Running the thread with a change of 1 in the y direction causes the object to crawl down the screen. Greater changes in Y lead to jumpy graphics.

Would OpenGL make any difference or are there other canvas methods i can implement?

Hope that makes sense!

Thanks in advance ----Thread------

public void run()
{
    Canvas canvas;
    while(running)
    {
        canvas = null;
        try{
        canvas = this.surfaceholder.lockCanvas();
        synchronized(surfaceholder)
        {
            gamepanel.Check();
            this.gamepanel.onDraw(canvas);

        }

        }finally
        {

            if(canvas != null)
            {
                surfaceholder.unlockCanvasAndPost(canvas);
            }
        }

    }
}

----SurfaceView-------

protected void onDraw(Canvas canvas){
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.gamebackground), 0, 0, null);
SortItOut.sortitout.Letter.draw(canvas);
}

-----Letter----- (Each object is a different letter)

public static void draw(Canvas canvas)
{
y += 1;
canvas.drawBitmap(LetterObject, x, y, null);
}

Those are the methods i would believe are relevant (The Check method is simply to check whether the object has reached the bottom of the screen).

antlersoft
  • 14,636
  • 4
  • 35
  • 55
Ben Kneebone
  • 77
  • 2
  • 11

2 Answers2

1

You must load all your bitmaps in the constructor for the SurfaceView, never in onDraw()

Lumis
  • 21,517
  • 8
  • 63
  • 67
  • Check this example of a custom view, the same principle: http://stackoverflow.com/questions/4938822/how-can-i-use-the-animation-framework-inside-the-canvas/4946893#4946893 – Lumis Jul 12 '11 at 13:14
0

Aside from the bitmap loading problem, you can make it fall faster my increasing the rate of the y change. If you do it too much, the box will appear to jump, but I bet you could get away with up to 10 pixel changes before that would happen (experiment).

You would only need to do OpenGL in this case if performance was slowing you down. I don't think that's the case. Although, I would stop loading the bitmap in the onDraw method and put it in the onCreate or some constructor. onDraw gets called hundreds of times and that's killing your app.

DeeV
  • 35,865
  • 9
  • 108
  • 95
  • yeah i realised this just after i posted it! i've increased it up to 50 to get near the speed i desire but you can imagine the jumping! If you don't mind me asking, do you have any idea why canvas.drawText would crash the app, its placed in the on draw method (trying to display a score). – Ben Kneebone Jul 12 '11 at 13:25
  • You may be able to make it REALLY fast by calling invalidate() in the onDraw() method instead of calling it in that loop. – DeeV Jul 12 '11 at 13:41
  • He DeeV, i called invalidate but it causes the app to crash! I put it in the onDraw method. It's not called in the loop is it? – Ben Kneebone Jul 12 '11 at 13:59
  • You should only be updating the UI in the UI (main) thread. That is *probably* what's causing it. invalidate() tells the OS to redraw that view. Thus, if you put it in the onDraw() method, it's called continuously. What *might* work, is call postInvalidate() instead which is meant to update the UI oustide the UI thread. – DeeV Jul 12 '11 at 14:07
  • So do i even need the thread? (Sorry for my lack of knowledge and frequent questions!) – Ben Kneebone Jul 12 '11 at 14:20
  • Not for drawing, no. Threads are generally meant for very high operations or blocking operations like file I/O. – DeeV Jul 12 '11 at 14:34
  • You need a thread for SurfaceView not invalidate(). Your drawText may be crashing because you need to use Integer.toString(score) or because your paint is not a proper Paint object, like you defined the varibale "Paint paint;" but did not create the object "paint = new Paint();" and set text size and color to it. – Lumis Jul 12 '11 at 15:22