How to put the image in the background

  • Replies:0
  • OpenNot stickiedUnanswered
  • Forum posts: 2

Aug 13, 2016 7:39:27 PM via Website

Right now I currently have a scrolling background and I want to put the deer character that needs to be facing right and looks like running. How am I going to do it

I already have my deer sprite I cannot post link right now but if you want to take a look at it you can pm me

Here's my game code

package com.example.kenneth.rusa;

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);



    }
}

}