How Do I Make an Object Blink When It Touches Another Object? I don't want the object to just blink once, but continuously until the switch gets toggled off when another object touches said switch. I know I have to use a loop, but I don't know how/where to implement it.
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Obstacle that gets switched on and off by being hit. */ public class Switch extends Actor { private boolean touched = false ; private boolean switchedOn = false ; private GreenfootSound sound = new GreenfootSound ( "ping.wav" ); private int i = 0 ; public Switch() { } public void act() { checkSwitch(); } private void checkSwitch() { if (!touched && isTouching()) { touched = true ; switchedOn = !switchedOn; if (switchedOn) { sound.play(); setImage( "block-light.png" ); } else { setImage( "block.png" ); sound.stop(); i = 0 ; } } if (touched && !isTouching()) { touched = false ; } if (switchedOn && !sound.isPlaying()) { sound.play(); } } private boolean isTouching() { return (getOneIntersectingObject(Body. class ) != null ); } } |