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

2019/6/6

Rescaling entire scenario

Wasupmacuz Wasupmacuz

2019/6/6

#
I'm trying to make a game work on different resolutions, but I can't figure out how. I've tried rescaling all Actors in the world constructor like this:
List<Actor> actors = getObjects(null);
        for(Actor a : actors)
        {
            a.getImage().scale((int)(a.getImage().getWidth() * (scrnWidth/1670.0)), (int)(a.getImage().getHeight() * (scrnHeight/1050.0))); 
            // scrnWidth/scrnHeight is the resolution, 1680/1050 is the resolution that everything was tested on.
        }
which didn't work. But this technique wouldn't work anyway, since images will be updated later in the code. I'm wondering if there's a way to stretch or compress the scenario right before painting. Any ideas?
danpost danpost

2019/6/6

#
You could override the addObject method:
public void addObject(Actor actor, int x, int y)
{
    GreenfootImage img = actor.getImage();
    img.scale(img.getWidth()*scrnWdth/1670, img.getHeight()*scrnHeight/1050);
    actor.setImage(img);
    super.addObject(actor, x, y);
}
Wasupmacuz Wasupmacuz

2019/6/6

#
danpost wrote...
You could override the addObject method
Thanks Dan! A clever solution.
Wasupmacuz Wasupmacuz

2019/6/6

#
This works perfectly for setting image sizes in the constructor. I'm still lost as to how I could accomplish the same effect every act cycle without making each image infinitely larger/smaller. Edit: I suppose I could add the Actor to a list of needsUpdate every time the image is scaled or updated
Super_Hippo Super_Hippo

2019/6/6

#
I guess the alternative would be to override the setImage method instead. (Probably doesn't work with default images, so make sure every constructor has a setImage call.) Or you end up overriding both methods (then without line 4 above).
danpost danpost

2019/6/7

#
Wasupmacuz wrote...
This works perfectly for setting image sizes in the constructor.
In the constructor? No. The only image the constructor needs to deal with is, if you have one, the background image.
Wasupmacuz Wasupmacuz

2019/6/7

#
danpost wrote...
Wasupmacuz wrote...
This works perfectly for setting image sizes in the constructor.
In the constructor? No. The only image the constructor needs to deal with is, if you have one, the background image.
Right, I meant when the constructor creates the objects. Sorry, confusing the way I worded that.
danpost danpost

2019/6/7

#
Wasupmacuz wrote...
Right, I meant when the constructor creates the objects. Sorry, confusing the way I worded that.
I do not get it. The constructor is free from dealing with actor images -- totally. I do not understand why you want to "update" the images or why you would need a list. Maybe you should explain why you need to do this.
Wasupmacuz Wasupmacuz

2019/6/7

#
danpost wrote...
Maybe you should explain why you need to do this.
The Overridden method that you provided only seems to rescale when the objects are first added to the world. In cases where the images were never touched again, this would work. However, I update/rescale certain actors' images while Greenfoot is running. Since I do this, the images revert to the original size. That's why I figured a java.util.ArrayList may be helpful, because I could keep track of any image that has been reset and rectify that reset.
danpost danpost

2019/6/7

#
Wasupmacuz wrote...
The Overridden method that you provided only seems to rescale when the objects are first added to the world. In cases where the images were never touched again, this would work. However, I update/rescale certain actors' images while Greenfoot is running. Since I do this, the images revert to the original size. That's why I figured a java.util.ArrayList may be helpful, because I could keep track of any image that has been reset and rectify that reset.
Can you not just apply the same factor when "rescaling" (using 'scrnWdtth/1670' and 'scrnHeight/1050') along with the new factor?
Wasupmacuz Wasupmacuz

2019/6/7

#
danpost wrote...
Can you not just apply the same factor when "rescaling" (using 'scrnWdtth/1670' and 'scrnHeight/1050') along with the new factor?
I suppose I could by overriding each of the actor's scale and setImage methods. Thanks again Dan.
danpost danpost

2019/6/7

#
Wasupmacuz wrote...
I suppose I could by overriding each of the actor's scale and setImage methods. Thanks again Dan.
You may be able to just use the following line after updating the image:
getWorld().addObject(this, getX(), getY());
You need to login to post a reply.