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

2022/2/24

Changing photo from another class

NielsProgamer NielsProgamer

2022/2/24

#
Hello, I have a question. I want to change a picture from one class to another. I have read on this site that you can do something for this. It is as follows:
Boat boat = (Boat) getWorld().getObjects(Boat.class).get(0);
Boat.setImage("1fish.gif");
I only have a problem because in my own program I want to change the picture several times by using a variable. But that doesn't work, the picture only changes once. Is this because of the get(0) (I don't know what this does) or does it have another reason? Here is my code:
        movement1();
        if ( isTouching(Banana.class) )
        {
            i++;
            Apple a = (Apple) getWorld().getObjects(Apple.class).get(0);
            a.setImage("Apple" + i + ".png");
            
        }
        Remove();
movement1 and Remove are codes I made in another superclass.
danpost danpost

2022/2/24

#
Safer would be:
if (isTouching(Banana.class))
{
    i++;
    if (i >= 5) i = 0; // "5" should be changed, if required, to the number of apple images available
    for (Object obj : getWorld().getObjects(Apple.class))
    {
        ((Apple)obj).setImage("Apple"+i+".png");
    }
}
This way, if no apple is in the world, no error will occur by using the get method. Also, I added lines 4 and 5 so that if the number of possible apple images is exhausted, it will go back to the original and repeat the sequence. Lines 4 and 5 could be combined and replaced with the following line:
i = (i+1)%5;
NielsProgamer NielsProgamer

2022/2/25

#
Thanks for the help but I still have the same problem. The picture just changes 1 time. If I touch the banana another time it doesn't change anymore. I want to use the apple as a counter which becomes bigger. I think the i++ doesnt work but im not sure.
danpost danpost

2022/2/25

#
NielsProgamer wrote...
Thanks for the help but I still have the same problem. The picture just changes 1 time. If I touch the banana another time it doesn't change anymore. I want to use the apple as a counter which becomes bigger. I think the i++ doesnt work but im not sure.
Please supply full class codes. The superclass may be needed also.
NielsProgamer NielsProgamer

2022/2/25

#
public class Jam extends Waterspel
{
    int Speed;
    /**
     * Act - do whatever the Jam wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
    }
    public void movement1()
    {
        setLocation( getX() -Speed, getY());
    }
    public void Remove()
    {
        if( isTouching(Minion_op_Surfboard.class) || getX() == 0 )
        {
            getWorld().removeObject(this);
        }
    }
}
(Superclass)
public class Paarse_Jam extends Jam
{
    /**
     * Act - do whatever the Paarse_Jam wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public boolean ja = false;
    int i = -1;
    
    public Paarse_Jam()
    {
        Speed = Greenfoot.getRandomNumber(2) + 3;
    }
    public void act()
    {
        movement1();
//        if ( isTouching(Minion_op_Surfboard.class) )
  //      {
    //        i++;
      //      Paarse_JamCounter PJC = (Paarse_JamCounter) getWorld().getObjects(Paarse_JamCounter.class).get(0);
        //    PJC.setImage("Paarse_Jam" + i + ".png");
            
       // }
       if (isTouching(Minion_op_Surfboard.class))
        {
            i++;
            if (i >= 5) 
            {
              i = 0;    
            }
            for (Object obj : getWorld().getObjects(Paarse_JamCounter.class))
            {
                ((Paarse_JamCounter)obj).setImage("Paarse_Jam"+i+".png");
            }
        }
        Remove();
    }
}
after // is what i got first.
danpost danpost

2022/2/25

#
NielsProgamer wrote...
TThe picture just changes 1 time. If I touch the banana another time it doesn't change anymore. I want to use the apple as a counter which becomes bigger.
When you remove an actor that touches a banana, the counter, i, that belongs to that actor is removed also. To clarify, each actor of this type has its own i counter. So, none of those type actors will ever have a counter that will increase multiple times because they are different instances of the class that touch a banana. What this actually means is that the i counter (or, rather, the counter that controls the image of the apple(s)), is not in the correct class. It should be in the apple class, with a method to increment and change its image. So, instead of setting the apple image directly, you would just call the method in the apple class to have it change its own image.
NielsProgamer NielsProgamer

2022/2/25

#
Yes I understand. I also have an applecounter. If the banana touches the apple than the applecounter image changes. So I can change the image in applecounter. But I need to put a if statement in which can see if the banana touches the apple. How can I do that. Is that with getoneintersectingobject() and how can I do that.
NielsProgamer NielsProgamer

2022/2/25

#
I tried to do it with a boolean variable where in the superclass the variable is true if istouching. And then in the subclass if( variable) "the methode" but this doesnt work.
                if ( isTouching(banana.class) )
        {
            "variable" = true;
        }
and the subclass
            if( "variable" == true)
            {
                i++;
                "variable" = false;
            }
danpost danpost

2022/2/26

#
NielsProgamer wrote...
I tried to do it with a boolean variable where in the superclass the variable is true if istouching. And then in the subclass if( variable) "the methode" but this doesnt work.
Again, same issue. Once the actor is removed, its variable also is. Does not matter if it is the class of the actor or in a class it extends from. Apple image variable should be in class of apple.
Greenfoot2.0 Greenfoot2.0

2022/2/26

#
(this is my second account) Yes I understand that but then I got the problem with is touching. I got 3 classes apple, banana and applecounter. I understand that I have to put the i++ and the setImage in the applecounter. But I dont know how I can do an if statement in the class applecounter that says if apple and banana are touching. I tried many different things but I can find out.
danpost danpost

2022/2/26

#
Try this: In Apple class:
if (isTouching(Banana.class))
{
    for (Object obj : getWorld().getObjects(AppleCounter.class))
    {
        ((AppleCounter)obj).bumpImage();
    }
}
In AppleCounter class:
private int i;

public void bumpImage()
{
    i = (i+1)%5;
    setImage("Apple"+i+".png");
}
Greenfoot2.0 Greenfoot2.0

2022/2/26

#
It works. Thanks!
NielsProgamer NielsProgamer

2022/2/27

#
So if 2 class touches 3 class i want 1 class to change image. These are all different classes. In class 1 i can change it but i need a code for if 2 class touches 3 class. thanks for your help
danpost danpost

2022/2/27

#
NielsProgamer wrote...
So if 2 class touches 3 class i want 1 class to change image. These are all different classes. In class 1 i can change it but i need a code for if 2 class touches 3 class.
That is exactly what the code given above does. Apple detects collision with banana, which in turn changes counter actor image.
You need to login to post a reply.