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

2012/4/21

Blinking Image

jpkenned jpkenned

2012/4/21

#
hey there, I'm working on a project for school. It wants me make a stationary blink between two images when it is intersected by another moving object.. The problem I need help with is getting the image to blink. I'm not sure how to get it to continuously switch between images. Any help would be appreciated, thanks.
2patrickMurphy 2patrickMurphy

2012/4/21

#
there are two methods in greenfoot API look at it for i think Actor class. oneIntersectingObject() and setImage(). Just make it so when it gets intersected by a certain object it changes its image to something that is a blank picture (or a pic of the backround) then go back in 1 second or less. Since its for school i wont help you with code btw i think i might be wrong on the names of the methods but if you look at the API you will notice them
jpkenned jpkenned

2012/4/21

#
yeah the problem I have is getting it to blink at a slower rate. I put this code in a loop but it repeats too fast to be noticeable, the only reason I know its working is because the sound is constant. I'm not sure what to put in the condition part if the loop to make it repeat more slowly. thanks. if(getImage()==image1) { setImage(image2); Greenfoot.playSound(sound); } else { setImage(image1); }
2patrickMurphy 2patrickMurphy

2012/4/22

#
public void BLINK()
{
long t1 = System.currentTimeMillis();
if(System.currentTimeMillis() <t1 + 'your amount *one secound = 1000 
milli second*')
{
----- BLINK ------
}
}
Thats some psuedo code for you. It will blink for x amount of milliseconds
danpost danpost

2012/4/22

#
I think what you need is two variable, one is a boolean flag to indicate that the image is blinking, the other to count the cycles between the changing of the images. That way, if it is intersecting another object, then if the flag is not set, set it, change the image and reset the count; if intersecting and the flag is set, then count down and if the count is zero, change the image and reset the count; if not intersecting and the flag is set, unset the flag. The only thing that would need work is finding the right count to make it blink the right speed.
danpost danpost

2012/4/22

#
You might want to put the images in an array 'GreenfootImage images = { "firstImageName.png", "secondImageName.png" };' and have another integer variable to track which image its on (imageNumber). Then you can change the image with ease 'setImage(images);'
JaniceChan JaniceChan

2012/4/26

#
This "Christmas tree with blinking lights" animation helped me a lot :) Just click the Open in Greenfoot button, and you'll see the code with comments from the programmer: http://www.greenfoot.org/scenarios/521
You need to login to post a reply.