• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How do you make the character (sprite) jump

Hurr

Lurker
Hi! I'm trying to learn on how to make a 2D game on android right now I already created a background looping which is great now my objective is on how I'm going to make my character jump. I already created and put the character in the background now I don't have any ideas how I'm going to make it jump here's my code


Java:
package com.example.kenneth.rusa;

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;


/**
* Created by Kenneth on 8/12/2016.
*/
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;
    private Traps traps;


    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.deerc), 48,60,3);
        bg.setVector(-5);


        thread = new MainThread(getHolder(), this);
        thread.setRunning(true);
        thread.start();
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction()==MotionEvent.ACTION_UP) {
            deer.Jump(false);
            return true;
        }
        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);

        }
    }
}
 
Back
Top Bottom