1. Download our Official Android App: Forums for Android!

Apps How do you make the character (sprite) jump

Discussion in 'Android Development' started by Hurr, Aug 20, 2016.

  1. Hurr

    Hurr Lurker
    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    5
    Joined:
    Apr 28, 2014

    Apr 28, 2014
    5
    0
    25
    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


    Code (Java):
    1. package com.example.kenneth.rusa;
    2.  
    3. import android.annotation.SuppressLint;
    4. import android.app.Activity;
    5. import android.content.Context;
    6. import android.content.SharedPreferences;
    7. import android.graphics.Bitmap;
    8. import android.graphics.BitmapFactory;
    9. import android.graphics.Canvas;
    10. import android.graphics.Paint;
    11. import android.graphics.Rect;
    12. import android.view.Display;
    13. import android.view.MotionEvent;
    14. import android.view.SurfaceHolder;
    15. import android.view.SurfaceView;
    16.  
    17. import java.util.ArrayList;
    18. import java.util.Random;
    19.  
    20.  
    21. /**
    22. * Created by Kenneth on 8/12/2016.
    23. */
    24. public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {
    25.  
    26.     public static final int WIDTH = 856;
    27.     public static final int HEIGHT = 480;
    28.     public static int Score = 0;
    29.     public static int Highscore;
    30.     private MainThread thread;
    31.     private Background bg;
    32.     private Deer deer;
    33.     private Traps traps;
    34.  
    35.  
    36.     public GamePanel (Context context) {
    37.         super(context);
    38.         getHolder().addCallback(this);
    39.         setFocusable(true);
    40.  
    41.     }
    42.  
    43.     @Override
    44.     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    45.  
    46.     }
    47.  
    48.     @Override
    49.     public void surfaceDestroyed(SurfaceHolder holder) {
    50.  
    51.         boolean retry = true;
    52.         while (retry) {
    53.             try {
    54.                 thread.setRunning(false);
    55.                 thread.join();
    56.             } catch (InterruptedException e) {
    57.                 e.printStackTrace();
    58.                 retry = false;
    59.             }
    60.         }
    61.     }
    62.  
    63.     @Override
    64.     public void surfaceCreated(SurfaceHolder holder) {
    65.         bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.gamebg));
    66.         deer = new Deer(BitmapFactory.decodeResource(getResources(), R.drawable.deerc), 48,60,3);
    67.         bg.setVector(-5);
    68.  
    69.  
    70.         thread = new MainThread(getHolder(), this);
    71.         thread.setRunning(true);
    72.         thread.start();
    73.     }
    74.     @Override
    75.     public boolean onTouchEvent(MotionEvent event) {
    76.         if(event.getAction()==MotionEvent.ACTION_UP) {
    77.             deer.Jump(false);
    78.             return true;
    79.         }
    80.         return super.onTouchEvent(event);
    81.     }
    82.     public void update() {
    83.         Score += 2;
    84.         if (Score > Highscore) {
    85.             Highscore = Score;
    86.         }
    87.         bg.update();
    88.         deer.update();
    89.     }
    90.  
    91.     @SuppressLint("MissingSuperCall")
    92.     @Override
    93.     public void draw (Canvas canvas)  {
    94.  
    95.         final float scaleFactorX = (float)getWidth()/WIDTH;
    96.         final float scaleFactorY = (float)getHeight()/HEIGHT;
    97.         if(canvas !=null) {
    98.             final int savedState = canvas.save();
    99.             canvas.scale(scaleFactorX, scaleFactorY);
    100.             bg.draw(canvas);
    101.             deer.draw(canvas);
    102.  
    103.             canvas.restoreToCount(savedState);
    104.  
    105.             Paint textpaint = new Paint();
    106.             textpaint.setTextSize(30);
    107.             canvas.drawText("Score:" +String.valueOf(Score), 0, 32, textpaint);
    108.             canvas.drawText("High Score: "+String.valueOf(Highscore), 0, 64, textpaint);
    109.  
    110.         }
    111.     }
    112. }
    113.  
     

    Advertisement

Share This Page

Loading...