This site requires JavaScript, please enable it in your browser!
Greenfoot back
blair
blair wrote ...

2012/4/26

ANY IDEAS?!

blair blair

2012/4/26

#
how would you make the block blink (toggle) when you have a body hit them. I got the block to turn on and off if an object hits them, but i am required to make it so when a body hits a block it flashes on and off and also plays a sound periodically. view plaincopy to clipboardprint? public class Obstacle extends Actor { private String sound; private boolean touched = false; private boolean toggledOn = false; /** * Create an obstacle with an associated sound file. */ public Obstacle(String soundFile) { sound = soundFile; } /** * Each act cycle, check whether we were hit. If we were, play our sound. */ public void act() { checkToggle(); } private void checkToggle() { if (!touched && isTouching()) { touched = true; toggledOn = !toggledOn; if (toggledOn) { setImage ("block-light.png"); Greenfoot.playSound(sound); } else { setImage ("block.png"); } } if (touched && !isTouching()) touched = false; } public void playSound() { Greenfoot.playSound(sound); } private boolean isTouching() { return (getOneIntersectingObject(Body.class) !=null); } }
DHoffman94 DHoffman94

2012/4/26

#
Haha, i literally did the project a couple weeks ago. Newton's lab? Check my scenerio out on my page. You will get the basic idea on how to do it there. I believe i have the code posted.
davmac davmac

2012/4/26

#
view plaincopy to clipboardprint?
What were you doing here? To insert code, click the 'code' link, and paste your code into the box which then pops up, and then click the green tick.
danpost danpost

2012/4/26

#
Or just type immediately before the code and immediately after the code.
You need to login to post a reply.