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

2018/3/22

Calling World public method with actor as a parameter

1
2
FabioPacheco FabioPacheco

2018/3/22

#
I have this method in World:
Violin Violin1= new Violin();
addObject(Violin1, 20,90);
instrumentsTrasnparency(Violin1,50);
 public void instrumentsTrasnparency(Actor Instrument,int t)
    {
       Instrument.getImage().setTransparency(t);
    }
and I want to call it in Player 2 to change the transparency of Violin1:
if(isTouching(Violin.class))
        {
         getWorldOfType(MyWorld.class).instrumentsTrasnparency(Violin1 ,255); 
         removeTouching(Violin.class);
        }
Can someone help me?
Game/maniac Game/maniac

2018/3/22

#
If you set the Violin1 as a public field in MyWorld you can then access it by doing world.Violin1.
public class MyWorld extends World {

    public Violin Violin1;

    ...

    public MyWorld() {

        ...

        Violin1 = new Violin();
        addObject(Violin1, 20, 90);
        instrumentsTransparency(Violin1, 50);

        ...

    }

    ...

}
public Player2 extends Actor {

    ...

    public void act() {

        ...

        if(isTouching(Violin.class))
        {
            MyWorld world = getWorldOfType(MyWorld.class);
            world.instrumentsTransparency(world.Violin1, 255);
            removeTouching(Violin.class);
        }

        ...

    }

    ...

}
danpost danpost

2018/3/22

#
Why would you care about the transparency of the image of an actor that is being removed from the world? Btw, I do not see where Violin1 is defined in the Player2 class.
FabioPacheco FabioPacheco

2018/3/22

#
danpost wrote...
Why would you care about the transparency of the image of an actor that is being removed from the world? Btw, I do not see where Violin1 is defined in the Player2 class.
On the top part of the world I have a list of objects that a player needs to collet in a lower transparency. So my goal is to do a method that changes te transparency of the Violin1 when the Player2 class catches a Violin class object
danpost danpost

2018/3/22

#
FabioPacheco wrote...
my goal is to do a method that changes te transparency of the Violin1 when the Player2 class catches a Violin class object
I see it being removed from the world by way of removeTouching(Violin.class). What is the purpose in changing its image transparency at that time -- if it is not to be viewed in the world?
FabioPacheco FabioPacheco

2018/3/22

#
Game/maniac wrote...
If you set the Violin1 as a public field in MyWorld you can then access it by doing world.Violin1.
public class MyWorld extends World {

    public Violin Violin1;

    ...

    public MyWorld() {

        ...

        Violin1 = new Violin();
        addObject(Violin1, 20, 90);
        instrumentsTransparency(Violin1, 50);

        ...

    }

    ...

}
public Player2 extends Actor {

    ...

    public void act() {

        ...

        if(isTouching(Violin.class))
        {
            MyWorld world = getWorldOfType(MyWorld.class);
            world.instrumentsTransparency(world.Violin1, 255);
            removeTouching(Violin.class);
        }

        ...

    }

    ...

}
Still doesn't work. The game stops and says "java.lang.NullPointerException"
FabioPacheco FabioPacheco

2018/3/22

#
danpost wrote...
FabioPacheco wrote...
my goal is to do a method that changes te transparency of the Violin1 when the Player2 class catches a Violin class object
I see it being removed from the world by way of removeTouching(Violin.class). What is the purpose in changing its image transparency at that time -- if it is not to be viewed in the world?
The object that is being removed is of the same class but different object. So when the object in the game is removed by the player should change the transparency of the object in the list
danpost danpost

2018/3/22

#
Okay, I understand now. It might be easier to use a separate class for the list actors. Then you can link a game actor to a list actor and the list actor can illuminate itself when the game actor is collected.
Yehuda Yehuda

2018/3/23

#
From the shown code it's possible for Violin1 to be the Violin that's being removed. If Player2 touches Violin1, then all three lines in the if- statement will execute, and on the same Object.
danpost danpost

2018/3/23

#
FabioPacheco wrote...
Still doesn't work. The game stops and says "java.lang.NullPointerException"
Please give the entire error output and any related codes.
FabioPacheco FabioPacheco

2018/3/23

#
danpost wrote...
FabioPacheco wrote...
Still doesn't work. The game stops and says "java.lang.NullPointerException"
Please give the entire error output and any related codes.
There is the error output java.lang.NullPointerException at MyWorld.instrumentsTransparency(MyWorld.java:163) at Jogador2.lookForBonusPoints(Jogador2.java:91) at Jogador2.act(Jogador2.java:22) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) Code in World ( at MyWorld.instrumentsTransparency(MyWorld.java:163) ):
public void instrumentsTransparency(Actor Instrument,int t)
    {
       Instrument.getImage().setTransparency(t);
    }
Code in player 2 ( at Jogador2.act(Jogador2.java:22) ):
public void act() 
    {
        lookForBonusPoints();
    }
Code in player 2 ( at Jogador2.lookForBonusPoints(Jogador2.java:91) ):
public void lookForBonusPoints()
    {
        
        if(isTouching(Violin.class))
        {
         MyWorld world = getWorldOfType(MyWorld.class);
         world.instrumentsTransparency(world.Violin1, 255);
         removeTouching(Violin.class);
        }
    }
FabioPacheco FabioPacheco

2018/3/23

#
danpost wrote...
Okay, I understand now. It might be easier to use a separate class for the list actors. Then you can link a game actor to a list actor and the list actor can illuminate itself when the game actor is collected.
I was trying to not have a separate class with the same image. But if there isn't other way I will do that
FabioPacheco FabioPacheco

2018/3/23

#
Yehuda wrote...
From the shown code it's possible for Violin1 to be the Violin that's being removed. If Player2 touches Violin1, then all three lines in the if- statement will execute, and on the same Object.
Probably that's the problem
danpost danpost

2018/3/23

#
FabioPacheco wrote...
I was trying to not have a separate class with the same image. But if there isn't other way I will do that
I am not saying to have multiple classes for each instrument actor. One class is sufficient. Each instance of the class can assume a likeness of the image of the actor it is linked to using a field and constructor like the following:
// field for linked actor
private Actor linked;

// constructor
public ActorIcon(Actor link)
{
    linked = link; // retain linked actor
    setImage(new GreenfootImage(linked.getImage())); // assume a likeness of image of actor
    getImage().scale(30, 30); // scale image for listing
    getImage().setTransparency(50); // fade image
}
Since it retains the linked actor, its act method can be like this:
public void act()
{
    if (linked != null && linked .getWorld() == null) // if linked actor is collected
    {
        linked = null; // reference no longer needed
        getImage().setTransparency(255); // illuminate
    }
}
Nullifying the reference avoids any code in the block from being executed over and over again.
FabioPacheco FabioPacheco

2018/3/23

#
danpost wrote...
FabioPacheco wrote...
I was trying to not have a separate class with the same image. But if there isn't other way I will do that
I am not saying to have multiple classes for each instrument actor. One class is sufficient. Each instance of the class can assume a likeness of the image of the actor it is linked to using a field and constructor like the following:
// field for linked actor
private Actor linked;

// constructor
public ActorIcon(Actor link)
{
    linked = link; // retain linked actor
    setImage(new GreenfootImage(linked.getImage())); // assume a likeness of image of actor
    getImage().scale(30, 30); // scale image for listing
    getImage().setTransparency(50); // fade image
}
Since it retains the linked actor, its act method can be like this:
public void act()
{
    if (linked != null && linked .getWorld() == null) // if linked actor is collected
    {
        linked = null; // reference no longer needed
        getImage().setTransparency(255); // illuminate
    }
}
Nullifying the reference avoids any code in the block from being executed over and over again.
So I need to create a new class called ActorIcon and put the code you gave me inside, right? And how do I call the class ActorIcon to work in myWorld?
There are more replies on the next page.
1
2