Hay all i want to fix a problem that is my background is not moving i dont
know what is problem can some one found or tell me how to fix it
here i am going to give all code of three class that i code which are main
class for moving background using SurfaceHolder, Thread and bitmapFactory
here is the code for GamePanal class:
public class GamePanal1 extends SurfaceView implements SurfaceHolder.Callback{
public MThread mainThread;
public boolean Pause_Game;
private BackGroud backGroud;
public float fishSpeed;
public GamePanal1(Context context, Game game, int screenwidth) {
super(context);
getHolder().addCallback(this);
mainThread = new MThread(getHolder(), this);
backGroud = new BackGroud(BitmapFactory.decodeResource(getResources(),
R.drawable.bc), screenwidth, this);
setFocusable(true);
fishSpeed = screenwidth/2.f;
}
@Override
public boolean onTouchEvent(MotionEvent event){
return super.onTouchEvent(event);
}
void Draw(Canvas canvas){
if(!Pause_Game){
if(canvas!=null){
backGroud.draw(canvas);
}
}
}
void Update(float dt){
backGroud.update(dt);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// here i am confuse will look in future if any problem occurs
mainThread.runningMethod(true);
mainThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int
height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
while (retry){
try {
mainThread.join();
retry= false;
}catch (InterruptedException ex){
}
}
}
}
Code for background class:
public class BackGroud {
Bitmap BackbitMap;
int x,y;
int ScreenWth;
int Count_backgroud;
GamePanal1 rootGamePanal1;
public BackGroud(Bitmap bitmap, int screen_w, GamePanal1 gamePanal1){
this.BackbitMap = bitmap;
this.x = 0;
this.y = 0;
this.ScreenWth = screen_w;
Count_backgroud = ScreenWth/BackbitMap.getWidth()+1;
this.rootGamePanal1 = gamePanal1;
}
public void draw(Canvas canvas){
for(int i = 0; i<Count_backgroud+1; i++){
if(canvas!=null){
canvas.drawBitmap(BackbitMap, BackbitMap.getWidth()*i++, y,
null);
}
if (Math.abs(x)>BackbitMap.getWidth()){
x = x + BackbitMap.getWidth();
}
}
}
public void update(float dt){
x = (int)(x - rootGamePanal1.fishSpeed*dt);
}
}
And code of Thread Class:
public class MThread extends Thread {
private SurfaceHolder surfaceHolder;
private GamePanal1 gamePanal1;
private boolean runningVar;
float dt;
public MThread(SurfaceHolder holder, GamePanal1 gamePanal1){
this.surfaceHolder = holder;
this.gamePanal1 = gamePanal1;
dt = 0;
}
void runningMethod(boolean runining){
this.runningVar = runining;
}
@Override
public void run(){
Canvas canvas;
while (runningVar){
if(!gamePanal1.Pause_Game){
long startDraw = System.currentTimeMillis();
canvas = null;
try{
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder){
gamePanal1.Update(dt);
gamePanal1.Draw(canvas);
}
}
finally {
if (canvas!=null){
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
long endDraw = System.currentTimeMillis();
dt = (endDraw-startDraw)/10000.f;
}
}
//super.run();
}
}
Can some one tell me what is problem in it
why my back ground is not moving left from right
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/d86526a1-0f96-4207-bd3e-a1ee5536c22e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.