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

2013/4/28

How do you gradually increase image size?

QWERTY894 QWERTY894

2013/4/28

#
I used the scale() method and I tried creating variables for the width and height, then putting it in a for loop so width and height increase by one, but the scale does not change. int width= 30; int height= 30; for(y= getY(); y>=358; y++) { width+=2; img.scale(width, height); }
Gevater_Tod4711 Gevater_Tod4711

2013/4/28

#
Maybe you condition is wrong. I thing y >= doesn't make much sence when you increment y. This will either be never executed or executed as a infinite loop.
QWERTY894 QWERTY894

2013/4/28

#
I want it to gradually increase as it moves down the screen so I figured to use the y values
erdelf erdelf

2013/4/28

#
then you should write that
int width= 30;
int height= 30; 
for(y= getY(); y<=358; y++) 
{ 
   width+=2; 
   img.scale(width, height); 
}
davmac davmac

2013/4/28

#
One problem here is you're doing the scaling in a tight loop. The whole loop will run in a blink, and the scaling won't be visible. You could use Greenfoot.delay() to insert a delay in each iteration, or you could remove the loop and just do one step in each step of the simulation (i.e each time act() is called).
danpost danpost

2013/4/28

#
You will probably find that the image will distort as it grows in size. This is because you are scaling the image multiple times. To avoid this, scale the original image each time.
GreenfootImage img = new GreenfootImage("imageFilename.png");
img.scale(getY()+1, getY()+1)
setImage(img);
I added one to the y-location to prevent error creating an image of zero size. If you feel that the image is too large by the time it reaches the bottom of the screen, you can modify the code by using a ratio. In the above the ratio is one, which makes it full height of screen at the bottom. If you wanted two-thirds of the height of the screen, the second line would become:
img.scale((getY()+3/2)/(3/2), (getY()+3/2)/(3/2));
You need to login to post a reply.