Hi! How do I create a character that will look like moving to the right

Started by
-1 comments, last by Kenneth Abuel 7 years, 8 months ago

Hello guys! Out of my curiosity I started to learn Android, my current objective right now is to create a endless running game. So I already created a background scroll with background music which is nice, my objective right now is to put a character in the background scroll and make it look like running. I don't have idea how am I going to do it.

Here's my Game code:




import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;



public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {

    public static final int WIDTH = 856;
    public static final int HEIGHT = 480;
    public static int Score = 0;
    public static int Highscore;
    private MainThread thread;
    private Background bg;


    public GamePanel(Context context) {
        super(context);
        getHolder().addCallback(this);
        thread = new MainThread(getHolder(), this);
        setFocusable(true);

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

        boolean retry = true;
        while (retry) {
            try {
                thread.setRunning(false);
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
                retry = false;
            }
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.gamebg));
        bg.setVector(-5);
        thread.setRunning(true);
        thread.start();
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }
    public void update() {
        Score += 2;
        if (Score > Highscore) {
            Highscore = Score;
        }
        bg.update();
    }

    @SuppressLint("MissingSuperCall")
    @Override
    public void draw(Canvas canvas) {
        final float scaleFactorX = (float)getWidth()/WIDTH;
        final float scaleFactorY = (float)getHeight()/HEIGHT;
        if(canvas !=null) {
            final int savedState = canvas.save();
            canvas.scale(scaleFactorX, scaleFactorY);
            bg.draw(canvas);


            canvas.restoreToCount(savedState);

            Paint textpaint = new Paint();
            textpaint.setTextSize(30);
            canvas.drawText("Score:" +String.valueOf(Score), 0, 32, textpaint);
            canvas.drawText("High Score: "+String.valueOf(Highscore), 0, 64, textpaint);

        }
    }
}

Am I going to create another class? or just put it on the game code?

This topic is closed to new replies.

Advertisement