Is there any possibility to check, if the Image of an Object touchs another image?
I don't mean "getOneIntersectingObject(Whatever.class)".
I mean, if that, what is drawed on the Image, touches anything, that is drawed on another Objects Image?


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public boolean checkTouch(Actor a) { if (checkOneTouch(- 20 , 20 , - 40 , a)) return true ; if (checkOneTouch(- 17 , 17 , - 39 , a)) return true ; if (checkOneTouch(- 18 , 18 , - 38 , a)) return true ; if (checkOneTouch(- 14 , 14 , - 37 , a)) return true ; if (checkOneTouch(- 15 , 15 , - 36 , a)) return true ; //... if (checkOneTouch(- 14 , 14 , 40 , a)) return true ; return false ; } public boolean checkOneTouch( int xStart, int xEnd, y, Actor a) { for ( int x=start; x<end; x++) { if (getOneObjectAtOffset(x,y, a. class )!= null ) return true ; } return false ; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | public List getTouchedObjects(Class clss) { List<Actor> list = getIntersectingObjects(clss); List<Actor> list2 = getIntersectingObjects(clss); list2.clear(); GreenfootImage i= new GreenfootImage(getImage()); i.rotate(getRotation()); int w=i.getWidth(),h=i.getHeight(),x=getX(),y=getY(); for (Actor A : list) { GreenfootImage Ai = new GreenfootImage(A.getImage()), i2 = new GreenfootImage(w,h); Ai.rotate(A.getRotation()); int Aw=Ai.getWidth(),Ah=Ai.getHeight(),Ax=A.getX(),Ay=A.getY(); i2.drawImage(Ai,Ax-x-(Aw/ 2 -w/ 2 ),Ay-y-(Ah/ 2 -h/ 2 )); boolean b = true ; for ( int yi = 0 ; yi<h && b; yi++) for ( int xi = 0 ; xi<w && b; xi++) if (i2.getColorAt(xi,yi).getAlpha()> 0 && i.getColorAt(xi,yi).getAlpha()> 0 ) { list2.add(A); b= false ; } } return list2; } public Actor getOneTouchedObject(Class clss) { List<Actor> list = getIntersectingObjects(clss); List<Actor> list2 = getIntersectingObjects(clss); list2.clear(); GreenfootImage i= new GreenfootImage(getImage()); i.rotate(getRotation()); int w=i.getWidth(),h=i.getHeight(),x=getX(),y=getY(); for (Actor A : list) { GreenfootImage Ai = new GreenfootImage(A.getImage()), i2 = new GreenfootImage(w,h); Ai.rotate(A.getRotation()); int Aw=Ai.getWidth(),Ah=Ai.getHeight(),Ax=A.getX(),Ay=A.getY(); i2.drawImage(Ai,Ax-x-(Aw/ 2 -w/ 2 ),Ay-y-(Ah/ 2 -h/ 2 )); for ( int yi = 0 ; yi<h; yi++) for ( int xi = 0 ; xi<w; xi++) if (i2.getColorAt(xi,yi).getAlpha()> 0 && i.getColorAt(xi,yi).getAlpha()> 0 ) return A; } return null ; } |
1 | import java.util.List; |
1 | import greenfoot.*; |