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

2012/6/10

Drawing image of Actor on another actor

SPower SPower

2012/6/10

#
Hi all, I have a problem with this method:
public void addImageOfActor(Actor a)
{
    GreenfootImage image = new GreenfootImage(getImage());
    int width = Math.max(image.getWidth(), a.getImage().getWidth());
    int height = Math.max(image.getHeight(), a.getImage().getHeight());
    if (image.getWidth() < width || image.getHeight() < height) {
         image.scale(width, height);
    }
    image.drawImage(a.getImage(), 0, 0);
    setImage(image);
}
This code should add the image of another Actor to another object, but it doesn't work. Can anyone tell me what's wrong? Thanks!
davmac davmac

2012/6/10

#
Define 'doesn't work'?
SPower SPower

2012/6/10

#
I don't see the image of the actor on the other actor: the image stays the same.
davmac davmac

2012/6/10

#
Your code works for me.
danpost danpost

2012/6/10

#
I do not see any problems with the code you supplied. Are you sure that the image is not changing (back) somewhere else in your code? Or that the image you are working on is the one that you see on the screen? Is there any apparent change in the size of the image at all? You could test it by adding the following code at the end of the method:
int myX = getX();
int myY = getY();
World w = getWorld();
w.removeObject(this);
w.addObject(this, myX, myY);
If your world is unbounded, you might want to verify that myX and myY are in the viewing window with:
if (myX < 0 || myX >= getWorld().getWidth()) System.out.println("X value outside world edge");
if (myY < 0 || myY >= getWorld().getHeight()) System.out.println("Y value outside world edge");
If you get an error at 'w.removeObject(this);' saying NullPointerException, then your object was not inserted into the world to begin with. If you see the image change with this code, then you probably had the actor hiding behind another actor of the same type.
SPower SPower

2012/6/10

#
What I meant is that the image of my Actor before I called this method, is the same as the image after this method was called. I'll test the code you gave me right now.
SPower SPower

2012/6/10

#
Still doesn't work: I think the error is somewhere else, but thanks for the help.
You need to login to post a reply.