java - Android Rect Collision -


I want to make a small game for Android devices. All works ... Did I do something wrong except for the collision with the players and the objects? I have tried several ways to test the collision. E. G.

Edit: My problem is that nothing happens :)

  DisplayMetrics metrics = getContext (). GetResources () ) .getDisplayMetrics (); Int width = matrix.width pixel; Int height = metrics High pixel; Private bitmap player, enemy; Private int speed = 1, x = 0, y = height - height / 5, i = 1, yg = height - height / 5 + 40, xe = 920, xp = width / 2 - width / 4; Private reset P, E; Public static speed = 0; // Other code @ Override draws on protected void (canvas C) {c.drawcolor (Color.CYAN); HandelPlayer (); P = new rect (XP, Y, 0, 0); Route = new react (x, yg, 0, 0); Wayrect2 = New Rect (X + width, yg, 0, 0); E = new rect (xe, yg - 250, 0, 0); C.drawBitmap (enemy, E. left, E.top, tap); C.drawBitmap (player, p. left, p.top, tap); } Public Zero handelPlayer () {x - speed = speedrun; Xe - speed = speed; If (x + width> lt; 0) x = 0; If (xe & lt; -100) xe = 920; If (MainActivity.touch == 1) {y - = 100; // jump MainActivity.touch = 0; I = 1; } If (y & lt; = height - height / 5) y + = 3 * i / 10; // gravity i ++; If (p.intersect (e)) // collosision speedrun = 0; First, your rectangle from the top of your player bitmap (0,0) , Upper left corner of your device The image I meant was:  p = new rect (xp, y, xp + player.getWidth), y + player.getHeight ());  and  e , see the code below. 

Second, p.intersect (e) turns into rectangular p in intersection if they penetrate each other, then you should use Rect.intersects ( P, E) instead of .

Thirdly, you are checking conflicts on old status values, as you have not updated the rectangle after changing the position.

There may be a quick fix that the intersection test may be moved to the top of the handelPlayer (minor note: handplayer write it Would be the right way), such as:

  secure entry onda (canvas c) {c.drawcolor (Color.CYAN); HandelPlayer (); P = New Rect (XP, Y, XP + Player .getWidth (), Y + Player.GateHight ()) ;; Route = new react (x, yg, 0, 0); // These rectangles also have lower lower corners (0,0), which can cause problems 2 = new rectangle (x + width, yg, 0, 0); E = new racket (XE, GG-250, XE + enemy .getWidth (), yg - 250+ enemies. Gohite ()); C.drawBitmap (enemy, E. left, E.top, tap); C.drawBitmap (player, p. left, p.top, tap); } Public Zero Handle Player (if (Rectus reciprocal (P, E)) {// Speed ​​of collision = 0;} X - = speed * Speedren; Xe - = speed * Speederine; if (X + width & lt ; 0) x = 0; if (xe & lt; -100) xe = 920; if (MainActivity.touch == 1) {y - = 100; // jump MainActivity.touch = 0; I = 1;} (Y & lt; = height - height / 5) y + = 3 * i / 10; // gravity i ++;}  

Still there may be another problem, as You have not yet told what is really happening.


Comments