- Forum posts: 7
Jun 14, 2015, 10:03:29 PM via Website
Jun 14, 2015 10:03:29 PM via Website
Hallo,
ich habe ein Problem nämlich mein Spiel reagiert im Hintergrund aber es zeigt kein
Hintergrundbild an. Hier die Codes, bitte um Hilfe. Danke schonmal vorraus.
GamePanel:
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
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;
private MainThread thread;
private Background bg;
public GamePanel(Context context)
{
super(context);
//add the callback to the surfaceholder to intercept events
getHolder().addCallback(this);
thread = new MainThread(getHolder(), this);
//make gamePanel focusable so it can handle events
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.grassbg1));
bg.setVector(-5);
//we can safely start the game loop
thread.setRunning(true);
thread.start();
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
return super.onTouchEvent(event);
}
public void update()
{
bg.update();
}
@Override
public void draw(Canvas canvas)
{
final float scaleFactorX = getWidth()/WIDTH;
final float scaleFactorY = getHeight()/HEIGHT;
if(canvas!=null) {
final int savedState = canvas.save();
canvas.scale(scaleFactorX, scaleFactorY);
bg.draw(canvas);
canvas.restoreToCount(savedState);
}
}
}
Background:
import android.graphics.Bitmap;
import android.graphics.Canvas;
public class Background {
private Bitmap image;
private int x, y, dx;
public Background(Bitmap res)
{
image = res;
}
public void update()
{
x+=dx;
if(x<-GamePanel.WIDTH){
x=0;
}
}
public void draw(Canvas canvas)
{
canvas.drawBitmap(image, x, y,null);
if(x<0)
{
canvas.drawBitmap(image, x+GamePanel.WIDTH, y, null);
}
}
public void setVector(int dx)
{
this.dx = dx;
}
}
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.