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

2011/11/27

Problem with setTransparency Background

juanchi_kiuxus juanchi_kiuxus

2011/11/27

#
Hi there, I'm having problems with the setTransparency function. When I call to an actor, I need apply transparency to the World background. I have tried this: (Actor code): public class menu_if_else extends menu { private int transparencia = 255; public void act() { if (transparencia > 128){ GreenfootImage imagen = getWorld().getBackground(); imagen.setTransparency(transparencia); transparencia = transparencia -1; System.out.println("Actual transparency: " + imagen.getTransparency()); getWorld().setBackground(imagen); } } } But I don't get any result,, hovewer in the log I see the correct values for the "transparencia" variable. I 'am forgetting something else? Thanks in advance. Regards, Juan.
kiarocks kiarocks

2011/11/27

#
I do believe you have to cast your world.
(YourWorld)getWorld().setBackground(imagen);
Replace YourWorld with the world name you want.
juanchi_kiuxus juanchi_kiuxus

2011/11/27

#
Hi kiarocks, I have tried your solution but I get compile errors... "Not a stament" Regards, Juan.
davmac davmac

2011/11/27

#
You want: ((YourWorld) getWorld()).setBackground(imagen); (i.e. an extra set of parentheses).
danpost danpost

2011/11/27

#
I think what kiarocks meant was-- instead of 'GreenfootImage imagen = getWorld().getBackground();', use 'GreenfootImage imagen = ((YourWorld) getWorld()).getBackground();'.
davmac davmac

2011/11/27

#
danpost - Yes, well spotted, that is the change that's actually required. *slaps own forehead*
nccb nccb

2011/11/28

#
I'm not sure these posts address the original problem. I think the code was compiling and working but not having a visible result -- is that right, juanchi_kiuxus? There is a bug/feature of Greenfoot that making the world's background image transparent doesn't do anything other than appear to cause graphics corruption. The problem is that there is nothing painted behind the world's background, so making it transparent will just display whatever happens to be in the graphics buffer. This can produce some garbled graphics. It's questionable as to whether this is a bug: the whole point of the background is to be the background, so usually it's better to redesign the scenario to have an actor (potentially an actor with an image as big as the world) which is transparent, and keep the world's background opaque.
davmac davmac

2011/11/28

#
*slaps own forehead again* Yes, both setBackground() and getBackground() methods exist in the World class, there is no need to cast the world to a particular type in order to call those two methods.
juanchi_kiuxus juanchi_kiuxus

2011/11/29

#
You have all the reason "nccb" with your explication. I followed your advice redesigning my scenario and it works perfectly. Thanks a lot. Regards, Juan.
You need to login to post a reply.