android - Andengene get the closest sprite from a touchpoint -


this how adding sprites randomly on screen , moving them function randomly. question how can find closest sprite(sperm in case) touch-point when user touches screen. have scene touchevent, can find distance between 2 sprites not getting how can compare them dont have instance of them list or that...

please me

@override public scene oncreatescene() {     this.mengine.registerupdatehandler(new fpslogger());     this.mscene = new scene();     this.mscene.setbackground(new background(color.blue));     this.mscene.setonscenetouchlistener(this);     // ----------------------------------------------------------------     createhandlertoaddsperms();     // -----------------------------------------------------------------     return this.mscene; }   private void createhandlertoaddsperms() {     // -------------------------------------------------     mtimerclass = new timerclass(0.7f, new timerclass.itimercallback() {          @override         public void ontick() {              if (!isgameover) {                 spermsprite sperm;                 if (mrandom == null) {                     mrandom = new random();                 }                 switch (mrandom.nextint(down + 1)) {                 case mainactivity.left:                     sperm = addnewspermtoscreen(0, mrandom.nextint(camera_height), true);                     // sperm.setcolor(color.green);                      break;                 case mainactivity.right:                     sperm = addnewspermtoscreen(camera_width, mrandom.nextint(camera_height), true);                     // sperm.setcolor(color.white);                      break;                  case mainactivity.up:                     sperm = addnewspermtoscreen(mrandom.nextint(camera_width), 0, true);                     // sperm.setcolor(color.blue);                      break;                 case mainactivity.down:                     sperm = addnewspermtoscreen(mrandom.nextint(camera_width), camera_height, true);                     // sperm.setcolor(color.cyan);                      break;                  default:                     system.out.println("default case==============================");                     break;                  }              } else {                  mscene.unregisterupdatehandler(mtimerclass);                 system.out.println("unregister ptimerhandler =========================");              }          }     });     // -------------------------------------------------      this.mscene.registerupdatehandler(mtimerclass);  }  private spermsprite addnewspermtoscreen(final float px, final float py, final boolean modifypath) {     this.spermcount++;     debug.d("spermcount: " + this.spermcount);      final spermsprite sperm;     sperm = new spermsprite(px, py, this.spermtextureregion, mbufferobjectmanager) {          @override         public boolean onareatouched(touchevent pscenetouchevent, float ptoucharealocalx, float ptoucharealocaly) {             // addwaterdropblast(this.getx(), this.gety(), this);             return super.onareatouched(pscenetouchevent, ptoucharealocalx, ptoucharealocaly);         }     };      if (modifypath) {         addpathmodifiretodrp(sperm);     }      this.mscene.attachchild(sperm);     this.mscene.registertoucharea(sperm);     rotatespirmtowardscentre(sperm);      return sperm;  }   @override public boolean onscenetouchevent(scene pscene, touchevent pscenetouchevent) {     final float touchx = pscenetouchevent.getx();     final float touchy = pscenetouchevent.gety();      switch (pscenetouchevent.getaction()) {     case touchevent.action_down:         // -----------------------------------------------------------         break;      case touchevent.action_move:          break;     case touchevent.action_up:         break;      }      return true; }; 

you not having method sprites of entity.so can 1 thing add sprites list.then find distance between other sprites sprite.

int predis; int index;

for(sprite other: listofsprites){  distance(otherspritepos, yourtargetpos);    // compare sprites in way      index of sprite in list having min distance   }    distance(otherspritepos, targetpos){         int  distance  = math.pow(x1 - x2, 2) + math.pow(y1 - y2, 2);           if(distance< predis){           predis = distance;              // list index of sprite      } 

you can implement logic in way


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -