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

2012/4/14

How do I populate an actor with different images?

lgbrf lgbrf

2012/4/14

#
I am trying to populate a world with objects with different images. I'm using this kind of code.
   private void populate()
    {
        Player player = new Player();
        addObject(player, 250, 510);
    }
you should put down the image name of all the different characters.
SPower SPower

2012/4/14

#
In your player() method in the class Player, you have to put a parameter. With that, you can set the kind of image. Then, do this:
AddObject(new Player(Greenfoot.getRandomNumber(limit), somexcoordinate, someycoordinate);
In your world.
lgbrf lgbrf

2012/4/14

#
I'm not sure what the greenfoot random number is doing, what i want is 4 different players with different images, but set images appearing in set places - object 1 has image 1 at x1,y1 etc, i dont want anything to be random?
danpost danpost

2012/4/14

#
You can create the four images in a static array in the player class and assign each player a number (that would determine the image to use)
static GreenfootImage[] images = { new GreenfootImage("image1.png"),
                                                         new GreenfootImage("image2.png"),
                                                         new GreenfootImage("image3.png"),
                                                         new GreenfootImage("image4.png") };
int playerNumber;

public Player(int pNum)
{
    playerNumber = pNum;
    setImage(images[pNum]);
}
Then in the world, you can create them with
Player player1 = new Player(1);
addObject(player1, 250, 510);
Player player2 = new Player(2);
// etc.
lgbrf lgbrf

2012/4/15

#
Ive gone through and done all that, but now I've come across a problem in terminal java.lang.ArrayIndexOutOfBoundsException: 4 what does this mean?
matt.milan matt.milan

2012/4/15

#
arrays start from 0 so array has 0-9 your array shuold have 0-3 player1 shuold be 0, 2 should be 1, etc i think.
lgbrf lgbrf

2012/4/15

#
ahh ok thanks so the above example should be
Player player0 = new Player(0);
addObject(player0, 250, 510);
Player player1 = new Player(1);
// etc.
matt.milan matt.milan

2012/4/15

#
yep and as long as you dont have more players than the length of the array, you'll be fine remember u can also use array.length to get the int value of the array's index (4 if it's 0-3, for example)
lgbrf lgbrf

2012/4/15

#
alright thanks, would i do a similar thing for different speeds for the objects???
matt.milan matt.milan

2012/4/15

#
when i want to give different actors different speeds, i do something like this inside the object's class:
private int speed = 3

public void act()
{
     //...
    move(speed);
    //...
}
and then i make a method like this
public void setSpeed(int newSpeed)
{
    speed = newSpeed;
}
and when i create a new player i do this
Player player0 = new Player(0);  
player0.setSpeed(5);
addObject(player0, 250, 510);  
or you could make a new constructor for player class inside the player class, put something like this
public Player(int newSpeed)
{
    speed = newSpeed;
}
and then when you create a new player you can do this
Player player0 = new Player(5);
danpost danpost

2012/4/15

#
You could create another static variable (this time int) for the different speeds; and use the same pNum (playerNumber) value to determine the correct speed for that object.
private static int[] speeds= {3, 4, 2, 5};
private int speed;
Then, in the constructor, add
speed = speeds[pNum];
or (with a little sarcasm), why not just pass everything in through the parameters of the Player class constructor.
Player player0 = new Player("image0.png", 3);
addObject(player0, 250, 510);
Player player1 = new Player("image1.png", 4);
addObject(player1, ...
// etc.
and in the Player class, make the constructor as so:
public Player(String imgName, int rate)
{
    setImage(new GreenfootImage(imgName));
    speed =rate;
}
This will work, as long as, the speed and image is constant for all Player class objects; and you do not need the static fields declared in the Player class; but, the main problem with this, is you will not be able to determine which of the four each object in the world is which. That is why I suggested keeping a playerNumber variable in the class.
lgbrf lgbrf

2012/4/19

#
Alright, I tried that out and it worked great, thanks
StoveAteMe StoveAteMe

2016/5/11

#
danpost wrote...
You can create the four images in a static array in the player class and assign each player a number (that would determine the image to use)
static GreenfootImage[] images = { new GreenfootImage("image1.png"),
                                                         new GreenfootImage("image2.png"),
                                                         new GreenfootImage("image3.png"),
                                                         new GreenfootImage("image4.png") };
int playerNumber;

public Player(int pNum)
{
    playerNumber = pNum;
    setImage(images[pNum]);
}
Then in the world, you can create them with
Player player1 = new Player(1);
addObject(player1, 250, 510);
Player player2 = new Player(2);
// etc.
Hey, I'm looking at this code, and I'm confused on what the playerNumber and pNum variables are for. And when line 10 allows you to access the images of the array when need be, correct?
danpost danpost

2016/5/11

#
@StoveAtMe, the 'playerNumber' field gives each player a unique identity (like a name). The 'pNum' variable is just used to hold the value passed until it can be saved in the 'playerNumber' field. Line 10 could just as well have been:
setImage(images[playerNumber]);
since both 'pNum' and 'playerNumber' hold the same value at that point. The 'pNum' variable is lost when the contructor end it execution where the 'playerNumber' persists as long the object created exists. By the way, the parameters used should be '0' and '1' -- not '1' and '2' since counting in java starts at zero. Maybe a better way to set this up would be this:
static final GreenfootImage[] IMAGES =
{
    new GreenfootImage("image1.png"),
    new GreenfootImage("image2.png"),
    new GreenfootImage("image3.png"),
    new GreenfootImage("image4.png")
};
static int imageNum = 0; // index (pointer) for 'images' array

public int playerNumber; // player identification

public Player()
{
    setImage(IMAGES[imageNum]);
    imageNum++;
}
Then, you will not have to mess with passing a value to the class when creating a player. You should then, however, add the following line to the world constructor:
Player.imageNum = 0;
to reset the value during resets of the game. Line 15 can be changed to the following if ever more than four players were created (to recycle the images)
imageNum = (imageNum+1)%IMAGES.length;
You need to login to post a reply.