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

2013/1/30

On scaling problem

danpost danpost

2013/1/30

#
@Super_Hippo, When using getObjectsAtOffset(x,y,cls), the x and y offsets are by cells (not by pixels). If you are using 51 for either the x or the y when you mean 1, it will not work the way you think. You can use the following for scaling the elephant:
GreenfootImage image = new GreenfootImage("elephant.png");
// (knowing the width is larger than the height;, else I would have used an 'if' to determine which to use and coded the rest with x values replaced with similar values as y values are using)
double ratio = (double)51/image.getWidth();
image.scale(51, (int)(ratio*image.getHeight()));
GreenfootImage newImg = new GreenfootImage(51, 51);
int y = (newImg.getHeight() - image.getHeight())/2;
newImg.drawImage(image, 0, y);
setImage(newImg);
danpost danpost

2013/1/30

#
If you want the actual image to be a little smaller than the cell size (with the image still being 51x51), then try this:
GreenfootImage image = new GreenfootImage("elephant.png");
double ratio = (double)45/image.getWidth();
image.scale(45, (int)(ratio*image.getHeight()));
GreenfootImage newImg = new GreenfootImage(51, 51);
int x = (newImg.getWidth() - image.getWidth())/2;
int y = (newImg.getHeight() - image.getHeight())/2;
newImg.drawImage(image, x, y);
setImage(newImg);
Super_Hippo Super_Hippo

2013/1/30

#
My bad again. My English is apparantly not perfect.
super(1275, 867, 1) // 25*51, 17*51, 1
So my world had 25*17 cells. But then I decided to devide every cell in 51*51 cells (which I probably called pixels because here: cells=pixels). The problem stays the same. Btw, the problem is was, that when an elephant (or the other enemies) only have one "big cell" (51*51), because I placed a dynamite, they were always walking back and forth, but I wanted them to stand there and do nothing.
It works now! I had to do the same with the dynamite. Thank you very much, you are my hero! :)
You need to login to post a reply.