简体   繁体   中英

How do I stop the object from going outside the screen?

public class MovingBagView extends View {

    private Bitmap bag[] = new Bitmap[2];
    private int bagX;
    private int bagY = 1000;
    private int bagSpeed;
    private Boolean touch = false;
    private int canvasWidth, canvasHeight;
    private Bitmap backgroundImage;
    private Paint scorePaint = new Paint();
    private Bitmap life[] = new Bitmap[2];

    public MovingBagView(Context context) {
        super(context);
        bag[0] = BitmapFactory.decodeResource(getResources(), R.drawable.bag1);
        bag[1] = BitmapFactory.decodeResource(getResources(), R.drawable.bag2);
        backgroundImage = BitmapFactory.decodeResource(getResources(), R.drawable.background);
        scorePaint.setColor(Color.BLACK);
        scorePaint.setTextSize(40);
        scorePaint.setTypeface(Typeface.DEFAULT_BOLD);
        scorePaint.setAntiAlias(true);
        life[0] = BitmapFactory.decodeResource(getResources(), R.drawable.heart);
        life[1] = BitmapFactory.decodeResource(getResources(), R.drawable.heart_grey);
        bagX = 10;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvasWidth = canvas.getWidth();
        canvasHeight = canvas.getHeight();
        canvas.drawBitmap(backgroundImage, 0, 0, null);
        int minBagX = bag[0].getWidth();
        int maxBagX = canvasWidth - bag[0].getWidth() * 3;
        bagX = bagX + bagSpeed;
        if (bagX < minBagX) {
            bagX = minBagX;
        }
        if (bagX < maxBagX) {
            bagX = maxBagX;
        }
        bagSpeed = bagSpeed + 2;
        if (touch) {
            canvas.drawBitmap(bag[1], bagX, bagY, null);
        }
        else {
            canvas.drawBitmap(bag[0], bagX, bagY, null);
        }
        canvas.drawText("Score : ", 20, 60, scorePaint);
        canvas.drawBitmap(life[0], 500, 10, null);
        canvas.drawBitmap(life[0], 570, 10, null);
        canvas.drawBitmap(life[0], 640, 10, null);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            touch = true;
            bagSpeed = -12;
        }
        return true;
    }
}

I can tap on the screen to move the object to the left, and the more I tap on it, the further it should move to the left. However, the problem is that the object moves too much to the right, and goes off the screen. I want it to stop at the edge, so its still visible on the screen. Furthermore, the object is positioned in the center bottom of the screen, how do I position it to the bottom left corner? You can see how it works by looking at the GIF below.

游戏应用

This condition is never applied on the code you've provided. It continues to exceed the max value.

if (bagX < maxBagX) {
       bagX = maxBagX;
   }

It should look like this:

if (bagX >= maxBagX) {
       bagX = maxBagX;
   }

And the maxBagX value should be canvasWidth - bag[0].getWidth() in order to achieve the right edge of the bag itself. (Unless you're using the multiply 3 times for a different reason, this should be the solution.)

创建您使用线性、相对或约束的布局对象,然后使用该对象获取屏幕宽度

object.getWidth();

I did it like this //the maintainoffscreen function will maintain the path to donot move off screen, and return true ,, else if path does not collide to any side this will return false, and you can move the path by setting offset.

    private boolean maintainOffScreen(Path path, float px, float py){

    boolean done = true;
    RectF rectF = new RectF();
    path.computeBounds(rectF,true);
    float l = getPathWidth(path) - rectF.right;
    float r = getPathWidth(path) + rectF.left;
    float t = getPathHeight(path) - rectF.bottom;
    float b = getPathHeight(path)+ rectF.top;

    if(l < (-1) && r < getWidth() && b < getHeight() && t< (-1)){
        //if path does not collide to any side//you can move your path here as well
       //by setting Path.offset(px,py)
        done = false;
        offScreen = OffScreen.NOOFF; //im using these as enum according to my need, 
//as this is saving which side of screen collides

    }
    else if(l >= (-1)){
        path.offset(px+(l),py);
        offScreen = OffScreen.LEFT;
        offscrenn_xl = 1;
        done = true;

    }
    else if(r >= getWidth()){
        float s = getWidth() - r;
        path.offset(px+(s),py);
        offScreen = OffScreen.RIGHT;
        offscrenn_xr = s;
        done = true;
    }
    else if(b >= getHeight()){
        float s = getHeight() - b;
        path.offset(px,py+s);
        offScreen = OffScreen.BOTTOM;
        offscrenn_yb = s;
        done = true;
    }
    else if(t >= (-1)){
        path.offset(px,py+(t));
        offScreen = OffScreen.TOP;
        offscrenn_yt = t;
        done = true;
    }
    return done;
}

confuse about px,py ? this is the movement of you finger with respect to path

`
final float px = event.getX() - this.startX;
final float py = event.getY() - this.startY;
if(!maintainOffScreen(path,px,py)){path.offset(px,py); }` //you should call
//this function here ,this will move your path to finger if there is no collision.
//and donot forgot to set the startx,and starty yo new positon like this
this.startX = event.getX();
this.startY = event.getY();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM