java - Rectangle intersects not working while jumping -


i making game android. player rectangle has jump on obstacles. when jump decrease height , y coordinates. when run game rectangle jumping when try jump on obstacle still hitting it. think forget change value have no idea value. sorry bad english , beginner @ coding.

this player class:

package com.example.niek.speelveld;  import android.graphics.canvas; import android.graphics.paint; /**  * created niek on 15-8-2017.  */  public class player extends gameobject {     private int score;     private boolean up;     private boolean playing;     private long starttime;     private int h = 0;     private boolean jump;     public player(int x , int y){         super.x = x;         super.y = y;          width = 100;         height = gamepanel.height;          score = 0;         starttime = system.nanotime();     }     public void setup(boolean b){up = b;}      public void update(){         long elapsed = (system.nanotime()-starttime)/1000000;         if(elapsed>100){             score++;             starttime = system.nanotime();         }         if(up && h == 0){             jump = true;             = false;         }         if (jump) {             if (h < 120) {                 h = h + 7;                 height = height - 7;                 y = y - 7;             } else {                 jump = false;             }         } else {             if (h > 0) {                 h = h - 7;                 height = height + 7;                 y = y + 7;             }         }     }      public void draw(canvas canvas){         paint mypaint = new paint();         mypaint.setstyle(paint.style.stroke);         mypaint.setstrokewidth(7);         canvas.drawrect(x, y, width + x, height, mypaint );         mypaint.setstrokewidth(1);         canvas.drawtext("highscore  " + score, gamepanel.width - 100 , 0+mypaint.gettextsize(), mypaint);     }     public int getscore(){return score;}     public boolean getplaying(){return playing;}     public void setplaying(boolean b){playing = b;}      //public void resetscore(){score = 0;}      public boolean getjump() {         return jump;     } } 

and in gamepanel class use these 2 methods. when call class obstacle.get(i).update(); updating x value moving towards player.

 public void update(){         if(player.getplaying()) {             bg.update();             player.update();              //add obstacles timer             long obstacleelapsed = (system.nanotime()-obstaclestarttime)/1000000;              if(obstacleelapsed >(2000 - player.getscore()/4)){                 obstacles.add(new obstacle(bitmapfactory.decoderesource(getresources(), r.drawable.rsz_spike1), width + 10, height - 51, 14, 51, player.getscore()));                  //reset timer                 obstaclestarttime = system.nanotime();             }           }         //loop through every obstacle , check collision         for(int = 0; i<obstacles.size(); i++){             obstacles.get(i).update();              if (collision(obstacles.get(i), player)) {                  obstacles.remove(i);                  player.setplaying(false);                   intent intent = new intent(c, result.class);                  intent.putextra("score", player.getscore());                  c.startactivity(intent);                  break;              }              //remove obstacles out of screen              if (obstacles.get(i).getx() < -100) {                  obstacles.remove(i);                  break;              }          }     }      public boolean collision(gameobject o, gameobject p){         if(rect.intersects(o.getrectangle(), p.getrectangle())){             return true;         }         return false;     }  @override     public void draw(canvas canvas){         //get width , height screen         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);             player.draw(canvas);             //draw obstacles             for(obstacle o : obstacles ){                 o.draw(canvas);             }             canvas.restoretocount(savedstate);         }     } 

and player , obstacle class extends gameobject have getrectangle method.

 public rect getrectangle(){         return new rect(x, y, x+width, y+height);     } 


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -