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

2014/1/24

Switch images

casper4444 casper4444

2014/1/24

#
Hello, I'm trying to switch the image of an object but it won't work.. When Enemy1 touches Castle, the Castle has to change image My method in the Enemy1 class looks like this
    public void tryToEat()
    {
        Actor Castle;

        Castle = getOneObjectAtOffset(0, 0, Castle.class);

        if (Castle != null)
        {
            World world;
            world = getWorld();
            world.removeObject(this);
        }
    }
And my Castle method is:
    private GreenfootImage image1;
    private GreenfootImage image2;
    public Castle()
    { 
        image1 = new GreenfootImage("kasteel.PNG");
        image2 = new GreenfootImage("kasteel2.PNG");
        setImage(image1);
    }
    public void act()
    {
        updateImage();
    }

    public void updateImage()
    {
        Actor Enemy1;
        Enemy1 = getOneObjectAtOffset(0, 0, Enemy1.class);
        if (Enemy1 != null && ( getImage() == image1 )) 
        {
            setImage(image2);
        }

    }
Can somebody help?
danpost danpost

2014/1/24

#
Try changing the field name to something other than that of the class.
public void updateImage()
{
    Actor enemy1
    enemy1 = getOneObjectAtOffset(0, 0, Enemy.class);
    if (enemy1 != null && getImage() == image1) setImage(image2);
}
The first world in field names by convention should start with a lowercase letter (all following words begin with uppercase letters).
casper4444 casper4444

2014/1/24

#
It still isn't working.. It works without the
        Actor enemy1;
        enemy1 = getOneObjectAtOffset(0, 0, Enemy1.class);
        if (enemy1 != null)
        {
            setImage(image2);
        }
So is it something with the fact that my Enemy1 class has the "world.removeObject(this); " ?
bourne bourne

2014/1/24

#
Looks like the problem is when your enemy acts before castle, so the enemy removes itself, and the castle then doesn't get the enemy from the call to getOneObjectAtOffset
casper4444 casper4444

2014/1/24

#
Already fixed it, just edited the x & y coordinates of the getOneObjectAtOffset Thanks both !
casper4444 casper4444

2014/1/25

#
I have another problem. My code is now:
        Actor enemy;
        enemy = getOneObjectAtOffset(55, 45, Enemy1.class);
        if (enemy != null && getImage() == image1)
        {
            setImage(image2);
        }
But because of the y = 45, it only works if the Enemy1 is 0-45 degrees below the center I want to make the image change if its =0-45 degrees above or below the image I also tried
        Actor enemy;
        enemy = getOneObjectAtOffset(55, 45, Enemy1.class);
        enemy = getOneObjectAtOffset(55, -45, Enemy1.class);
        if (enemy != null && getImage() == image1)
        {
            setImage(image2);
        }
But this wont work..
danpost danpost

2014/1/25

#
No ... that wont work. The 'enemy' field can only hold one object. Line 3 makes line 2 obsolete (or of no consequence). The value assigned by line 3 will replace any value assigned by line 2. Try this:
Actor enemy;
enemy = getOneObjectAtOffset(55, 45, Enemy1.class);
if (enemy == null) enemy = getOneObjectAtOffset(55, -45, Enemy1.class);
if (enemy != null && getImage() == image1) setImage(image2);
The above and to the right is only checked if no enemy is found below and to the right.
casper4444 casper4444

2014/1/25

#
It still isnt working, did I did it right now?:
    public void updateImage()
    {
        Actor enemy;
        enemy = getOneObjectAtOffset(55, 45, Enemy1.class);
        if (enemy == null) 
        {
            enemy = getOneObjectAtOffset(55, -45, Enemy1.class); 
        }
        if (enemy != null && getImage() == image1)
        {
            setImage(image2);
        }
    }
casper4444 casper4444

2014/1/25

#
did I do* And I also want to change the image multiple times, but if I enter
        if (enemy != null && getImage() == image1)
        {
            setImage(image2);
        }
            if (enemy != null && getImage() == image2)
        {
            setImage(image3);
        }
            if (enemy != null && getImage() == image3)
        {
            setImage(image4);
        }
            if (enemy != null && getImage() == image4)
        {
            setImage(image5);
        }
It will skip the 3 images and will go from 1 to 5
bourne bourne

2014/1/26

#
That chain of if statements is bad: Consider if the first one is executed, the Actor's image is set to image2, and continues executing... checks if image is image2... Having "else"s in there would help. Also I wouldn't check if enemy is not null that many times, just enclose it all within one "if" with condition that it isn't null.
You need to login to post a reply.