I am working on the greenfoot book tutorial and i am under the Newton's Lab tutorial. In it, i must have an actor body pass through an obstruction and toggle it on. However, if it passes through it again it should toggle it off. Also, if it is on then the obstruction should play a sound. Oh, mine plays a sound alright. Like 10 at one time coming from the same obstruction. Then it wont toggle off for some reason and will just flicker there if a body object passes through. Here is my code for my Blinker.class (the class that should toggle) :
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Blinker here. * * @author (your name) * @version (a version number or a date) */ public class Blinker extends SmoothMover { private boolean touched = false; private int i = 0; /** * Act - do whatever the Blinker wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { isTouched(); } private void isTouched() { checkIsTouched(); GreenfootSound sound = new GreenfootSound("Bass2.wav"); if (touched && i == 0) { i = 1; setImage("Blinker-Light.png"); if (!sound.isPlaying()) { sound.play(); } } else if (!touched && i == 1) { i = 1; setImage("Blinker-Light.png"); if (!sound.isPlaying()) { sound.play(); } } else if (touched && i == 1) { i = 0; setImage("Blinker.png"); sound.stop(); } else { i = 0; setImage("Blinker.png"); sound.stop(); } } private boolean checkIsTouched() { Actor body = getOneIntersectingObject(Body.class); if(body != null && touched == false ) { touched = true; } else if (body != null && touched == true) { touched = false; } return touched; } }