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

2014/5/12

making a Blink method

whovian93 whovian93

2014/5/12

#
I need to make a flower blink whenever a crab walks by. Here is the directions from the teacher: Flower - you should create a Blink method...the Flower will blink on and off continuously (Hint...save the Flower's current transparency in a variable, set the transparency to zero, and then set the transparency back to the value you have saved in the variable...you must also call Greenfoot's delay() method after setting/re-setting the transparency). In a week or two I will give you more help with the Flower's blinking, but I would like you to try this challenge first by yourself. Here is what I have written: public void act() { blink(); } public void blink() { if (canSee(Crab.class)) { GreenfootImage i = getImage(); int t = i.getTransparency(); t = 0; i.setTransparency(t); setImage(i); } if (setTransparency(0)) { t = 100; } I know it's probably not correct, but I just can't figure it out
davmac davmac

2014/5/12

#
So I'll go over the first few lines you've written and try to talk you through this. But in the future, please use code tags when you post code (click the 'code' link just below the text entry box!)
1
2
GreenfootImage i = getImage();
int t = i.getTransparency();
This looks fine so far. You get the image and save it to a variable 'i', then you get the transparency of the image and save it in another variable 't'. Then you have this:
1
2
t = 0;
i.setTransparency(t);
Uh-oh! you are throwing away the value of 't' that you had and replacing it with 0! Then you are setting the transparency of the image to 't' (which is 0). Instead: you need to set the transparency to 0, without changing the value of 't'. Once you've done this, you can call the delay() method (as your teacher instructed). Then, set the transparency of the image back to 't'. You do not need this line:
1
setImage(i);
or these lines:
1
2
3
4
if (setTransparency(0))
{
    t = 100;
}
aromero2 aromero2

2015/7/17

#
It should look something like this
1
2
3
4
5
6
7
8
9
10
public void act()
   {
       GreenfootImage i = getImage();
       int t = i.getTransparency();
       i.setTransparency(0);
       Greenfoot.delay(1);
       i.setTransparency(t);
       Greenfoot.delay(5);
 
   }
davmac davmac

2015/7/17

#
aromero2 wrote...
It should look something like this: ...
Indeed it should, but since the discussion is about a year old I'm sure your assistance comes too late.
You need to login to post a reply.