0
\$\begingroup\$

I'm trying to learn Android, so I'm creating a basic 2D game which using a background scrolling image, I've already made it and the character is already walking with animation my current goal is to make it jump. I've also created the character class btw am I going to put it on ontouchevent?

  import android.annotation.SuppressLint;
  import android.app.Activity;
  import android.content.Context;
  import android.content.SharedPreferences;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.graphics.Canvas;
  import android.graphics.Paint;
  import android.graphics.Rect;
  import android.view.Display;
  import android.view.MotionEvent;
  import android.view.SurfaceHolder;
  import android.view.SurfaceView;

  import java.util.ArrayList;
  import java.util.Random;



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;
private Deer deer;


public GamePanel (Context context) {
    super(context);
    getHolder().addCallback(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.deer), 48,60,3);
    bg.setVector(-5);


    thread = new MainThread(getHolder(), this);
    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();
    deer.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);
        deer.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);

    }
}
}
\$\endgroup\$
1
  • \$\begingroup\$ I am confused regarding what your trying to ask. "Trying to make a character jump".. and? "am I going to put it ontouchevent?" you tell me. Perhaps you mean to ask should I put this in ontouchevent? Regardless, you may want to consider editing your question to make the actual question clearer. \$\endgroup\$
    – Gnemlock
    Aug 21, 2016 at 0:31

1 Answer 1

2
\$\begingroup\$

I've already made it and the character is already walking with animation my current goal is to make it jump. I've also created the character class btw am I going to put it on ontouchevent?

To clarify (correct me if I'm wrong):
- You have the character moving horizontally perfectly fine
- You want to make it jump and want to know how.
- You are wondering if you should put the "jump" code in ontouchevent.

Jumping behavior generally goes like this: "Jump" (object given upwards velocity), a bunch of update ticks (move object according to velocity & reduce upward velocity), and a landing -- detecting that your object has collided / will collide with another level element, and thus setting vertical velocity back to zero.

The way I'd implement jump functionality would be to:

  • Give the player (and enemies too, if you want them to jump) two new fields: "onGround" (boolean) and "velocity" (double).

  • When you want the character to jump, set onGround to false and set the velocity to some initial value. If onGround is already false, don't do anything (unless you want double+ jump, in which case you need to keep track of how many jumps have been made since the last landing).

  • Each update step, check if onGround is false, and if so move the character a bit (according to the velocity) and decrease the velocity by some amount to simulate gravity.

  • When you detect that the player is about to land on the ground, set the vertical velocity to 0 and onGround to true. If needed, adjust the player's position such that he/she is no longer colliding with the ground.

As for putting this code directly into onTouchEvent: I'd say to call a method to initiate the jump from onTouchEvent, for 2 reasons.

  1. Most of the code requires to be run every frame, so you can't put it in a function that (I would assume) is only called when you tap the screen.

  2. By encapsulating a startJump() method away from the ontouchevent you gain one layer of abstraction between game code and UI code. If the game needs to make the player jump for some reason besides a screen-tap, it can by simply calling that method.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .