I'm trying to make a 2D game without a game engine, how do I make my 
character go left and right by swipping?

Here's my code:

public class Game extends SurfaceView implements SurfaceHolder.Callback {

    private Background bg;
    private Gameloop thread;
    public static final int MOVESPEED = -3;
    public static final int WIDTH = 225;
    public static final int HEIGHT = 225;
    private Deer deer;


    public Game(Context context) {
        super(context);
        getHolder().addCallback(this);
        thread = new Gameloop(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));
        deer = new Deer(BitmapFactory.decodeResource(getResources(), 
R.drawable.deers),38,65,3);

        thread.setRunning(true);
        thread.start();

    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        return super.onTouchEvent(event);
    }
    public void update() {

        bg.update();

    }
    @SuppressLint("MissingSupercall")
    @Override
    public void draw(Canvas canvas) {

        final float scaleFactorX = getWidth()/(WIDTH*1.f);
        final float scaleFactorY = getHeight()/(HEIGHT*1.f);


        if(canvas!=null) {
            final int savedState = canvas.save();
            canvas.scale(scaleFactorX, scaleFactorY);
            bg.draw(canvas);
            deer.draw(canvas);
            canvas.restoreToCount(savedState);
        }
    }
}




<https://lh3.googleusercontent.com/-p9_kQlv3L64/V775l9RZtmI/AAAAAAAAABI/t8AdTpOhUCwUfdCJshn8WY2JenVY6ZBSgCLcB/s1600/Screenshot_2016-08-25-22-27-11.png>


-- 
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/56c23ad1-f6df-43fa-8f6c-2ad1a7a6ca54%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to