i want make the actor stop move if touch the grass


1 2 3 4 5 6 7 | public void act() { if ( "atas" .equals(mode)) imun.jalan_atas(); if ( "bawah" .equals(mode)) imun.jalan_bawah(); if ( "kanan" .equals(mode)) imun.jalan_kanan(); if ( "kiri" .equals(mode)) imun.jalan_kiri(); } |
1 2 3 4 5 6 7 8 9 10 11 | public void jalan_atas() { setLocation(getX(), getY()- 3 ); if (isTouching(Rumput. class )) setLocation(getX(), getY()+ 3 ); waktu = ++waktu % 6 ; if (waktu) == 0 ) { frame = ++frame % 2 ; setImage(frame == 0 ? atas1 : atas2); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public void act() { int dx = 0 , dy = 0 ; if (Greenfoot.isKeyDown( "left" )) dx--; if (Greenfoot.isKeyDown( "right" )) dx++; if (Greenfoot.isKeyDown( "up" )) dy--; if (Greenfoot.isKeyDown( "down" )) dy++; if (dx*dy == 0 && dx+dy != 0 ) // this ensures one is zero and the other is not { if (dx == - 1 ) imun.jalan_kiri(); if (dx == 1 ) imun.jalan_kanan(); if (dy == - 1 ) imun.jalan_atas(); if (dy == 1 ) imun.jalan_bawah(); } } |