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

2015/2/16

I'm inserting an array, but where do I put it?

1
2
davmac davmac

2015/2/23

#
danpost wrote...
I think davmac was correct in this being an XY problem.
Actually, maybe not. If you have a list of each type of object, and you choose from them in a random order, you get an object of each type. If you have each object choose its type randomly, on the other hand, you may end up with multiples of one type and none of some other type. It depends which is the case that is really wanted.
Kendall Kendall

2015/2/26

#
m
Kendall Kendall

2015/2/26

#
Thank you! This worked, danpost! However, I included code to scale the images and change the image when it hit the ground in each of the subclasses of FallingObject. I realize why the images won't scale or change with the code I have written, because the classes are basically unused due to how I had to put the objects in the world the way I wanted. Is there a way to scale each separate image now that I am using an array to randomly put them in the world? Or to change the image?
danpost danpost

2015/2/26

#
With the added complication, it would be better to save the random number chosen rather than the object text string. That is, instead of;
//  instance field in FallingObject class
private String obj;
// in constructor of FallingObject class
obj = OBJS[Greenfoot.getRandomNumber(OBJS.length)];
setImage(obj+".png");
// extra method
public String getObjectName()
{
    return obj;
}
you would use:
// instance field in FallingObject class
private int objNum;
// in constructor of FallingObject class
objNum = Greenfoot.getRandomNumber(OBJS.length);
setImage(OBJS[objNum]+".png");
// extra method
public int getObjectNumber()
{
    return objNum;
}
By saving its type as an int value, you can use a switch statement to scale depending on that value; or, as an alternative, you can add a double array of int constants to hold the dimensions at which to scale each type. Starting something like this;
public static final int[][] SIZE = { { 60, 30 }, { 80, 40 }, /** etc. */ };
and follow setting the image above with:
getImage().scale(SIZE[objNum][0], SIZE[objNum][1]);
Also, you can change the value of 'objNum' to an alternate value (even outside the initial range) when changing its image when hitting the ground, if needed.
Kendall Kendall

2015/2/27

#
danpost, if I use the integer array, what do I populate it with to associate the integers with the images?
danpost danpost

2015/2/27

#
Kendall wrote...
danpost, if I use the integer array, what do I populate it with to associate the integers with the images?
You put your scaling values in it: ' { /* width */, /* height */ } '.
You need to login to post a reply.
1
2