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

2015/5/8

Please help. I am having trouble making an object transparent

1
2
GreenfootNewbie GreenfootNewbie

2015/5/8

#
I'm need to create a fade method fort he LadyBug to "fade" or lose some of its transparency each time a Crab crosses over it . I put this in the crabs code & got no errors but its not giving me what I want. public void setTransparency (int t) { if(canSee(Bug.class)) { getImage().setTransparency(0); } }
Super_Hippo Super_Hippo

2015/5/8

#
I guess the crab "disappears" when touching an bug object now. Try out this.
Actor bug = getOneIntersectingObject(Bug.class)
if (bug != null)
{
    int t = bug.getImage().getTransparency()-1;
    if (t<1) getWorld().removeObject(bug);
    else bug.getImage().setTransparency(t);
}
GreenfootNewbie GreenfootNewbie

2015/5/8

#
Unfortunately this didn't work
azazeel azazeel

2015/5/8

#
@gfnewbie : argument t in method setTransparency is not used, so what is the idea to put an argument there ?
public void setTransparency (int t)
    {
        if(canSee(Bug.class))
        {
            getImage().setTransparency(0);
        }
    }
anyway how ssuperrhipo's solution didnot work ?
danpost danpost

2015/5/8

#
Your method declaration should probably just be this:
public void setTransparency()
as you will only know what to set the new transparency to within the method using the code Super_Hippo supplied (and change the call to 'setTransparency();' in the act method).
GreenfootNewbie GreenfootNewbie

2015/5/8

#
@azazeel "argument?" ...IDK what you're talking about. Nevertheless, this is the code that worked. Thanks to all for the input public void act() { if(canSee(Crab.class)) { i=getImage(); t=i.getTransparency()-5; if(t<0) { t=0; } i.setTransparency(t); setImage(i); } }
danpost danpost

2015/5/8

#
GreenfootNewbie wrote...
this is the code that worked. < Code Omitted >
You might also note that you relocated where the code was -- that you moved it from the Crab class to the Bug class.
GreenfootNewbie GreenfootNewbie

2015/5/9

#
@danpost correct, wasnt sure which class would I have to apply it to for it to be effective
danpost danpost

2015/5/9

#
GreenfootNewbie wrote...
@danpost correct, wasnt sure which class would I have to apply it to for it to be effective
It could actually be coded in either location; however, since it is the Bug object whose state is changing (the appearance, image transparency, and removal, existence in world), it is easier to code it in the Bug class.
SOUPNAZI SOUPNAZI

2015/5/10

#
Good evening fellow greenfooter, I too am having the same issue with transparency too (actually I'm trying to do something similar to what you are by making the turtle class blink continuously AND I am also trying to make the snake class fade) but when I use the code you listed it gives me the error that "cannot find symbol - variable i & t" How did you identify the i & t so the code would work?
danpost danpost

2015/5/10

#
SOUPNAZI wrote...
Good evening fellow greenfooter, I too am having the same issue with transparency too (actually I'm trying to do something similar to what you are by making the turtle class blink continuously AND I am also trying to make the snake class fade) but when I use the code you listed it gives me the error that "cannot find symbol - variable i & t" How did you identify the i & t so the code would work?
Use this:
GreenfootImage i = getImage();
int t = i.getTransparency()-5;
ipeefreely ipeefreely

2015/5/10

#
how did you get your turtle to blink continuously? instead of a turtle I need a flower to blink nonstop but my code is only making it fade away a little bit & it stands still or it goes away all the way but never comes back
    public void act() 
    { GreenfootImage i = getImage();
        int t = i.getTransparency();
        getImage().setTransparency(0);  
        if(getImage()== i)
        { getImage().setTransparency(0); }
        else
        {setImage(i); }
    }
}  
Super_Hippo Super_Hippo

2015/5/10

#
Your code doesn't really make sense. Think about what every line you wrote does. To make it "blink", you can use something like this:
private int blinkTimer=5, t=1;

public void act()
{
    blinkTimer--;
    if (blinkTimer == 0)
    {
        blinkTimer = 5; //smaller number => faster blinking
        t = (t+1)%2;
        getImage().setTranparency(t*255);
    }
}
ipeefreely ipeefreely

2015/5/10

#
Why did you put it on a timer? I dont understand
danpost danpost

2015/5/10

#
Act cycles occur very quickly (a scenario running at standard speeds would run about 55 act cycles per second). If you have the image change on and off at that frequency, the human eye would not perceive the change or it would look more like a flickering than a blink. Even at 5 act intervals, it would be awfully quick, 15 to 30 might be a fair range for the number of act cycles between changes (for a blink rate similar to that of a cursor).
There are more replies on the next page.
1
2