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

2018/7/2

If-statement: actor has a specific picture?

elena elena

2018/7/2

#
Hi, I was wondering whether there was such a thing as making an if- method in which something is done if a specific picture is set? In my case I would want to fire to the right (make the speed positive) if picture A was set for my actor and fire to the left (negative speed) if picture B was set for my actor. I would greatly appreciate your help, Elena
danpost danpost

2018/7/2

#
elena wrote...
I was wondering whether there was such a thing as making an if- method in which something is done if a specific picture is set? In my case I would want to fire to the right (make the speed positive) if picture A was set for my actor and fire to the left (negative speed) if picture B was set for my actor.
Of course it is possible. Keep references to both images in the class and only check for the one not initially set as the default image for objects of the class (and use and else clause, without needing to check, for that one).
elena elena

2018/7/2

#
danpost wrote...
elena wrote...
I was wondering whether there was such a thing as making an if- method in which something is done if a specific picture is set? In my case I would want to fire to the right (make the speed positive) if picture A was set for my actor and fire to the left (negative speed) if picture B was set for my actor.
Of course it is possible. Keep references to both images in the class and only check for the one not initially set as the default image for objects of the class (and use and else clause, without needing to check, for that one).
How exactly do I "ask" whether the Image is the Image B?
1
2
3
4
5
6
7
if("space".equals(Greenfoot.getKey())){
           if ( this.GreenfootImage.equals("..\\cameleon_rot_neu_links_zwei.png")){
               getWorld().addObject(new NegSpit(), getX(), getY());
            }
            else{
                getWorld().addObject(new Spit(), getX(), getY());
            }
So far, I have the code above but
1
this.GreenfootImage.equals
doesn't work and I don't know what else to use in that line...
danpost danpost

2018/7/2

#
elena wrote...
How exactly do I "ask" whether the Image is the Image B? << Code Omitted >> So far, I have the code above but
1
this.GreenfootImage.equals
doesn't work and I don't know what else to use in that line...
GreenfootImage is class name -- not an image name. Objects created from the class are GreenfootImage objects that you can name yourself:
1
2
3
4
5
6
7
8
9
10
11
// add fields for the images
private GreenfootImage imageR, imageL;
 
// in constructor
imageR = new GreenfootImage("cameleon_rot_neu_rechts_zwei.png");
imageL = new GreenfootImage("cameleon_rot_neu_links_zwei.png");
setImage(imageR);
 
// in action code
Actor spit = getImage() == imageL ? new NegSpit() : new Spit();
getWorld().addObject(spit, getX(), getY());
Make sure you always use imageL and imageR when setting an image to actors of the class or the condition will not work right.
You need to login to post a reply.