0

i have bitmap moves to right inside canvas i want when this bitmap closes to the right border of screen i.e : x position < getWidth() with certain small value i want to translate canvas co-ordinates so that the canvas itself moves also along side the movement of the bitmap inside it

another meaning ; i want the bitmap continue in moving to right permanently and of course when it closes to the right border of screen it will disappear soon so i want the canvas itself to move with me to make the bitmap visible always and i want to know how to set the x position value after translate canvas that

i.e : something such as in all games that has a character moves to right and the screen moves also with it .

this is the code that found here

http://www.edu4java.com/en/androidgame/androidgame3.html

and i edited it and now the bitmap repeats its movement when it closes to the right border of the screen .

MyActivity.java


   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


       setContentView(new GameView(this));


  }

and MyGameView (the main class of my problem )

 import android.content.Context;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.util.Log;
  import android.view.SurfaceHolder;
 import android.view.SurfaceView;

public class GameView extends SurfaceView {
 private Bitmap bmp;
private SurfaceHolder holder;
private GameLoopThread gameLoopThread;
private int x = 0;
private int xSpeed = 2;

public GameView(Context context) {
    super(context);
    gameLoopThread = new GameLoopThread(this);
    holder = getHolder();
    holder.addCallback(new SurfaceHolder.Callback() {

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            boolean retry = true;
            gameLoopThread.setRunning(false);
            while (retry) {
                try {
                    gameLoopThread.join();
                    retry = false;
                } catch (InterruptedException e) {
                }
            }
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            gameLoopThread.setRunning(true);
            gameLoopThread.start();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format,
                                   int width, int height) {
        }
    });
    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.attention);
}

@Override
protected void onDraw(Canvas canvas) {

    super.onDraw(canvas);


    if(x==getWidth())
    {
        Log.e("cds","cdca");
        canvas.translate(x, 0);

        x=10;
    }
    else
    {
        x = x + xSpeed;

    }

    if(canvas!=null) {
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(bmp, x, 10, null);
    }

}

}

GameLoopThread .java

    import android.graphics.Canvas;

 public class GameLoopThread extends Thread {
static final long FPS = 10;
private GameView view;
private boolean running = false;

public GameLoopThread(GameView view) {
    this.view = view;
}

public void setRunning(boolean run) {
    running = run;
}

@Override
public void run() {
    long ticksPS = 1000 / FPS;
    long startTime;
    long sleepTime;
    while (running) {
        Canvas c = null;
        startTime = System.currentTimeMillis();
        try {
            c = view.getHolder().lockCanvas();
            synchronized (view.getHolder()) {
                view.onDraw(c);
            }
        } finally {
            if (c != null) {
                view.getHolder().unlockCanvasAndPost(c);
            }
        }
        sleepTime = ticksPS-(System.currentTimeMillis() - startTime);
        try {
            if (sleepTime > 0)
                sleep(sleepTime);
            else
                sleep(10);
        } catch (Exception e) {}
    }
  }
 } 
omar
  • 41
  • 1
  • 9

2 Answers2

0

As you mentioned "something such as in all games that has a character moves to right and the screen moves also with it".

you need to define a size of camera viewport (let it be size of the screen 640X480) and world (1280X960)

Also you need to define min and max offset of camera

offsetMax_X= WorldSize_X-ViewPort_X; //(1280-640)
offsetMax_Y= WorldSize_Y-ViewPort_Y; //(960-480)
offsetMin_X=0;
offsetMin_Y=0;

Now you need to calculate camera position relative to player(Sprites) so that player always at the center of screen.

cam_x=player_X-ViewPort_X/2; 
cam_y=player_Y-ViewPort_Y/2;

check camera position so that it should not exceed offsets.

if(cam_x>offsetMax_X)
    cam_x=offsetMax_X;
else if(cam_x<offsetMin_X)
    cam_x=offsetMin_X;
if(cam_y> offsetMax_Y)
    cam_y=offsetMax_Y;
else if(cam_y<offsetMin_Y)
    cam_y=offsetMin_Y;

Also you need to tanslate the canvas

canvas.translate(-cam_x,-cam_y);

I am writing part of the code. create a camera class

 class Camera{
  int ViewPort_X;
  int ViewPort_Y;
  int WorldSize_X;
  int WorldSize_Y;
  int offsetMax_X;
  int offsetMax_Y;
  int offsetMin_X;
  int offsetMin_Y;
  int CameraPos_X;
  int CameraPos_Y;
   Camera(int _ViewPort_X,int _ViewPort_Y,int _WorldSize_X,int _WorldSize_Y)
   {
   ViewPort_X=_ViewPort_X;
   ViewPort_Y=_ViewPort_Y;
   WorldSize_X=_WorldSize_X;
   WorldSize_Y=_WorldSize_Y;
   offsetMax_X= WorldSize_X- ViewPort_X;
   offsetMax_Y= WorldSize_Y- ViewPort_Y;
   offsetMin_X=0;
   offsetMin_Y=0;
   }
  void updateCamera(int player_X,int player_Y)
  {
  CameraPos_X=player_X-ViewPort_X/2;
  CameraPos_Y=player_Y-ViewPort_Y/2;
  if(CameraPos_X>offsetMax_X)
     CameraPos_X=offsetMax_X;
  else if(CameraPos_X<offsetMin_X)
     CameraPos_X=offsetMin_X;
  if(CameraPos_Y> offsetMax_Y)
     CameraPos_Y=offsetMax_Y;
  else if(CameraPos_Y<offsetMin_Y)
    CameraPos_Y=offsetMin_Y;
   }
 }

and create camera Instance in GameView

Camera cam;
int playerX;
int playerY;
public GameView(Context context) {
   super(context);
   cam=new Camera(640,480,1280,960);
   playerX=width/2;
   playerY=height/2;
   .....
   .....
  ......
 }

now update canvas

 @Override
protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 canvas.translate(-cam.CameraPos_X,-cam.CameraPos_Y);
 cam.updateCamera(playerX,playerY); 

 if(playerX>cam. WorldSize_X)
  playerX= cam.WorldSize_X;
 else if(playerX<0)
  playerX=0;
 if(playerY>cam.WorldSize_Y)
  playerY=cam.WorldSize_Y;
 else if(playerY<0)
  playerY=0;
 .....
 ....
 //draw bitmap
  .....
 }

Add touchEvents to control Sprite's motion (http://developer.android.com/reference/android/view/View.html#onTouchEvent(android.view.MotionEvent)

playerY-=5;//move towards up
playerY+=5;// move towards down
playerX-=5;// move toward left
playerX+=5; //move towards right
phani_rohith
  • 136
  • 5
  • very thanks for your effort for help but i want to make the character running from left to right and the right border of the mobile screen doesn't stop it from running and also doesn't make it(the character ) invisible so i think to make for example the character running with speed about +10 and the canvas it self running with speed + 7 ( i.e : less than the character speed ) to only track its movement and also make it visible always – omar Sep 10 '15 at 23:06
  • when the character reach the right border it is invisble as it runs outside the dimensions of screen so how to translate the co ordinates of the screen to make it visible always – omar Sep 10 '15 at 23:07
  • and also always running – omar Sep 10 '15 at 23:31
  • ok..you need to move the canvas towards left, but you are moving canvas toward right, I wrote new answer once you check it. – phani_rohith Sep 11 '15 at 05:33
0

ok, you need to translate canvas toward left but you are shifting canvas towards right.

Take a variable moveCanvasX for translating canvas towards left. Also you need to move canvas and sprite at same speed, move the sprite toward right with speed "xSpeed", move the canvas towards left with speed "xSpeed" so that sprite will always stay towards right. I hope this will solve your problem.

int moveCanvasX=0;

@Override
protected void onDraw(Canvas canvas) {

super.onDraw(canvas);


if(x>=getWidth()-bmp.getWidth())
{
    Log.e("cds","cdca");
    canvas.translate(-moveCanvasX, 0);
    moveCanvasX=moveCanvasX+xSpeed;
    x=x + xSpeed;
}
else
{
    x = x + xSpeed;

}

if(canvas!=null) {
    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap(bmp, x, 10, null);
 }
}
phani_rohith
  • 136
  • 5
  • i am extremely sorry for late of thanking really thanking you for your help but this didn't succeed for me so i ended up using libgdx and asked this question http://stackoverflow.com/questions/32595413/libgdx-cant-make-camera-follow-player-neither-by-camera-position-nor-unproject may be it is useful for you or any person have a problem like this – omar Sep 15 '15 at 20:59