2
\$\begingroup\$

I am trying to build this game where the player hops between a number of ropes.

For now, in addition to my main activity that I kept empty, I built two additional classes:

  • Ropes.java, storing the coordinates of the ropes and their movement

  • Player.java, storing the player's details

Can you advise me about the next step to join them? For instance, where to include the "attach function" that links the player to the rope in case he reaches it?

And how would I populate the main activity to organize the game?

To further explain my problem, I will join my three activities below: the first is the rope class; the second is player class:

Rope class:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.app.Activity;
import android.media.SoundPool;
import android.graphics.Paint;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;


import java.util.List;

import static androidx.core.graphics.drawable.IconCompat.getResources;

public class ROPE extends Activity {
    public List<Float> ab=[,,,];
    public List <Float> ord;
    public float speed_up=-1;
    public final TARZAN tarzan;

    public static final float WIDTH         = 67;
    public static final float HEIGHT        = 416;
    public static final float ROPE_DISTANCE = 50;
    private static final float SPEED        = -2;


    public ROPE (Context context, List ab, List ord, TARZAN tarzan){
        this.tarzan=tarzan;


    }
    public void move_rope(){
        for(int i=0;i<4;i++){

        }
    }

    public void draw(Canvas canvas) {
        Drawable rope= getResources().getDrawable(R.drawable.rope);
        rope.draw(canvas);
    }    
}

Player class:

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.media.SoundPool;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;


public abstract class TARZAN extends Activity implements View.OnClickListener{
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;
    private  boolean  isAlive=true;
    public final float K=20;
    public final int WIDTH       = 44;
    public final int HEIGHT      = 31;
    private final int BASE_COLLISION  = 521 - HEIGHT - 5;
    public float X;
    public float Y;
    public float vitesse=0;
    static final int gravity = -10;   // constant downward acceleration
    static final int flapping = 25;   // upward acceleration whenever isFlapping is true
    Boolean isFlapping = false;   // Is TARZAN flapping

    public TARZAN(float X,float Y) {
        this.X=X;
        this.Y=Y;

    }
    public float getX () {
        return X;
    }
    public float getY () {
        return Y;
    }
    public boolean isAlive () {
        return isAlive;
    }

    /**
     * Kills
     */
    public void kill () {
        isAlive = false;
    }

    /**
     * Set new coordinates when starting game
     */
    public void setGameStartPos () {
        X = 90;
        Y = 500;
    }
    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    X-=K;
                    Y=
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    X+=K;
                    Y=
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }
    }

    public void stick_to_rope(){

    }

    public void draw(Canvas canvas) {
        Drawable tarzan=getResources().getDrawable(R.drawable.mario);
        tarzan.draw(canvas); 
    }

}

My Main_Activity is currently empty.

\$\endgroup\$
2
\$\begingroup\$

Depends how your engine works.

Your player could be responsible for checking for ropes, or your ropes could be responsible for checking for the player.

Either way, I think both your player and your rope are going to need to know that they're attached, so whichever way you choose, you're going to need to have an 'attach' function in both classes.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ This answer would be even better if it included a code example for how this might be implemented. It looks like the asker is new to programming, and might need a bit more explicit guidance to put this advice into practice. \$\endgroup\$ – DMGregory Apr 6 at 19:46
  • \$\begingroup\$ Very true @DMGregory I think perhaps I misread the context of the question in terms of technical help required - I'm not experienced in java android studio - so I can only help here as far as concept and design is concerned \$\endgroup\$ – Natalo77 Apr 7 at 8:57
  • 1
    \$\begingroup\$ Well, they've marked the answer as "accepted," so I guess it solved their problem. :) \$\endgroup\$ – DMGregory Apr 7 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.