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
danpost danpost

2018/3/23

#
FabioPacheco wrote...
So I need to create a new class called ActorIcon and put the code you gave me inside, right?
Yes.
And how do I call the class ActorIcon to work in myWorld?
Create them as you create the game actors that require instrument actors. Use the following example as a format:
Violin violin = new Violin(); // create game actor
ActorIcon icon = new ActorIcon(violin); // create instrument actor
addObject(icon, 20, 90); // add instrument actor to world
addObject(violin, << wherever >>); // add game actor to world
FabioPacheco FabioPacheco

2018/3/23

#
danpost wrote...
FabioPacheco wrote...
So I need to create a new class called ActorIcon and put the code you gave me inside, right?
Yes.
And how do I call the class ActorIcon to work in myWorld?
Create them as you create the game actors that require instrument actors. Use the following example as a format:
Violin violin = new Violin(); // create game actor
ActorIcon icon = new ActorIcon(violin); // create instrument actor
addObject(icon, 20, 90); // add instrument actor to world
addObject(violin, << wherever >>); // add game actor to world
When I put : ActorIcon icon= new ActorIcon(violin); it highlights the word violin and says that "cannot find symbol- variable violin"
Yehuda Yehuda

2018/3/23

#
Show the class code.
FabioPacheco FabioPacheco

2018/3/23

#
Yehuda wrote...
Show the class code.
In MyWorld I have:
Violin violin = new Violin();
addObject( violin,x ,y); // actor in game

ActorIcon icon = new ActorIcon(violin);
addObject(icon, 20,90);
and the method:
public void instrumentsTransparency(Actor Instrument,int t)
    {
       Instrument.getImage().setTransparency(t);
    }
And in the class ActorIcon:
public class ActorIcon extends Actor
{
    //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().setTransparency(50); // fade image
   }
   
    public void act()
   {
    if (linked != null && linked .getWorld() == null) // if linked actor is collected
    {
        linked = null; // reference no longer needed
        getImage().setTransparency(255); // illuminate
    }
   }
}
And on the Player 2 class:
public void lookForBonusPoints()
    {
        if(isTouching(Violin.class))
        {
            MyWorld world = getWorldOfType(MyWorld.class);
            world.instrumentsTransparency(world.violin,255);
            removeTouching(Violin.class);
        }
    }
Yehuda Yehuda

2018/3/23

#
I think you do not need lines 5 and 6 in the method lookForBonusPoints since the transparency gets taken care of in the ActorIcon class (that was the point). All Player2 needs to do is remove the Object (Violin). (See ActorIcon.act(ActorIcon.java:16))
FabioPacheco FabioPacheco

2018/3/23

#
Yehuda wrote...
I think you do not need lines 5 and 6 in the method lookForBonusPoints since the transparency gets taken care of in the ActorIcon class (that was the point). All Player2 needs to do is remove the Object (Violin). (See ActorIcon.act(ActorIcon.java:16))
But I still have the problem with:
ActorIcon icon = new ActorIcon(violin);
The "new ActorIcon" doesn't accept the variable violin It says that "cannot find symbol- variable violin"
Yehuda Yehuda

2018/3/23

#
P. S. You didn't show any more code than what has already been displayed in this discussion, the point was to copy the whole class code and paste it here.
danpost danpost

2018/3/23

#
You will need to show you MyWorld class codes.
FabioPacheco FabioPacheco

2018/3/23

#
Some comments and methods are in portuguese but I think that it's easy to understand
public class MyWorld extends World
{   
    //Declaração de variáveis
    private int scoreJogador1, scoreJogador2;
    private final int ZELDA = 173;
    public Violin Violin1;
    
   
    private String World =
       "WWWWWWWWWWWWWWWWWWWWWWWWWWWW"+
       "WB1-----B---T--B----B-----BW"+
       "W-WWWWW-WWWWWWWW-W-WWW-WWW-W"+
       "W-WB-----B--W--B-WB-----BW-W"+
       "WSW-W-WWWWW-W-WWWW-WWWWW-W-W"+
       "WB--W---B------B------BW-B-W"+
       "W-WCW-WWWW-WWWW-WW-WWW-W-W-W"+
       "W-W-W-B---B--B--B-V-BW---W-W"+
       "WB--W3WWWW-.....-WWW-W3WBWBW"+
       "W-WB--WB--B.....B-BW-W-W-WMW"+
       "W-W-W-W-WW-..P..-W-WB-BW---W"+
       "W-W-WB----B.....BW-W-W---W-W"+
       "WBW---WBWW-.....-WBW-W-W-WBW"+
       "W--BW-W---B--B3-B---BW-WBW-W"+
       "W-W-W-WWW-WWW-WWW-WWWW-W-B-W"+
       "WBW-W---B---WB---B-V--BW-W-W"+
       "W-W-WWW-WWW-WW-WWW-WWWWW-W-W"+
       "W-WB-C--B---B----B---M--BW-W"+
       "W-WWWWW-WWWWW-W-WWWWW-WWWW-W"+
       "WB2----B--T--BW-----B---S-BW"+
       "WWWWWWWWWWWWWWWWWWWWWWWWWWWW"; 
       
       
    /**
     * Construtor do mundo
     */
    public MyWorld()
    {   
        super(1300 ,846,1); // Tamanho do mundo
        buildWorld(); // contrução área de jogo
        buildLeftSide();
        //Para que o objeto Enemy fique por cima dos BonusPoints e Instrumentos
        setPaintOrder(Enemy.class, Instrumentos.class, BonusPoints.class);      
        
    }
    
        /**
     * Metodo para criar o World com os objetos que pretendemos
     */
    public void buildWorld()
    {
        Violin violin = new Violin();
        
        int x=24 + ZELDA, y = 23;
        for (int p = 0; p < World.length(); p++){
            //Caso o carater seja 'W' inserir um objecto Bush
            if(World.charAt(p) == 'W' ){
                addObject(new Bush(),x ,y);
            }
            //Caso o carater seja 'W' inserir um objecto Bush
            if(World.charAt(p) == 'w' ){
                addObject(new Brick(),x ,y);
            }
            //Caso o carater seja 'P' inserir um objecto Palco
            if(World.charAt(p) == 'P'){
                addObject(new Palco(),x ,y);
            }
            //Caso o carater seja 'B' inserir um objecto Beer
            if(World.charAt(p) == 'B'){
                addObject(new Beer(),x ,y);
            }
            //Caso o carater seja 'G' inserir um objecto Beer
            if(World.charAt(p) == '-'){
                addObject(new BeerBoutle(),x ,y);
            }
            //Caso o carater seja 'M' inserir um objecto Mandolin
            if(World.charAt(p) == 'M'){
                addObject(new Mandolin(),x ,y);
            }
            //Caso o carater seja 'S' inserir um objecto Saxophone
            if(World.charAt(p) == 'S'){
                addObject(new Saxophone(),x ,y);
            }
            //Caso o carater seja 'G' inserir um objecto Tambourine
            if(World.charAt(p) == 'T'){
                addObject(new Tambourine(),x ,y);
            }
            //Caso o carater seja 'V' inserir um objecto Violin
            if(World.charAt(p) == 'V'){
                addObject( violin,x ,y);
            }
            //Caso o carater seja 'C' inserir um objecto Conga
            if(World.charAt(p) == 'C'){
                addObject(new Conga(),x ,y);
            }
            //Caso o carater seja '1' inserir um objeto Jogador1
            if(World.charAt(p) == '1'){
                addObject(new Jogador1(),x ,y);
            }
            //Caso o carater seja '2' inserir um objeto Jogador2
            if(World.charAt(p) == '2'){
                addObject(new Jogador2(),x ,y);
            }
            //Caso o carater seja '3' inserir um objeto ElReitor
            if(World.charAt(p) == '3'){
                addObject(new ElReitor(),x ,y);
            }
            
            x += 40;// o avanço dado entre cada carater (x+40)
            
            //caso chegue à largura maxima para passar pra linha de baixo
            if(x>getWidth()){
                x = 24 + ZELDA; //espaçamento da parede inicial
                y += 40; // espacamento entre linhas (y+40)
            }
        }
    }
    
    public void buildLeftSide()
    {
        scoreJogador1();
        scoreJogador2();
    }
    
    
    /**
     * Player 1
     */
    public void scoreJogador1()
    {
        //Chama os métodos para somar pontos
        somaPontosJogador1(0); //inicialize the score at 0
        
        //Define os objetos a utilizar
        Violin Violin1  = new Violin();
        Tambourine Tambourine1 = new Tambourine();
        Mandolin Mandolin1 = new Mandolin();
        Conga Conga1 = new Conga();
        Saxophone Saxophone1 = new Saxophone();
        
        //Adiciona os objectos para apanhar
        addObject(Violin1, 20,90);
        addObject(Tambourine1, 50,90);
        addObject(Mandolin1, 85,90);
        addObject(Conga1, 120,90);
        addObject(Saxophone1, 150,90);
        
        //Transparência dos objetos 
        instrumentsTransparency(Violin1,50);
        instrumentsTransparency(Tambourine1,50);
        instrumentsTransparency(Mandolin1,50);
        instrumentsTransparency(Conga1,50);
        instrumentsTransparency(Saxophone1,50);
    }
    
    /**
     * Método que coloca os instrumentos com transparência
     */
    public void instrumentsTransparency(Actor Instrument,int t)
    {
       Instrument.getImage().setTransparency(t);
    }
    
    /**
     * Player 2
     */
    public void scoreJogador2()
    {
        //Chama os métodos para somar pontos
        somaPontosJogador2(0); //inicialize the score a 0        
       
        //Define os objetos a utilizar
        Violin Violin2= new Violin();
        Tambourine Tambourine2= new Tambourine();
        Mandolin Mandolin2= new Mandolin();
        Conga Conga2 = new Conga();
        Saxophone Saxophone2 = new Saxophone();
        
        //Adiciona os objectos para apanhar
        addObject(Violin2, 20,getHeight()-90);
        addObject(Tambourine2, 50,getHeight()-90);
        addObject(Mandolin2, 85,getHeight()-90);
        addObject(Conga2, 120,getHeight()-90);
        addObject(Saxophone2, 150,getHeight()-90);
        
        //Transparência dos objetos 
        instrumentsTransparency(Violin2,50);
        instrumentsTransparency(Tambourine2,50);
        instrumentsTransparency(Mandolin2,50);
        instrumentsTransparency(Conga2,50);
        instrumentsTransparency(Saxophone2,50);
    }
    
    /**
     * Método para somar pontos ao jogador1
     */
    public void somaPontosJogador1(int pontos)
    {
        scoreJogador1 = scoreJogador1 + pontos;
        showText("Score: " + scoreJogador1, 85,50);
    }
    
    /**
     * Método para somar pontos ao jogador2
     */
    public void somaPontosJogador2(int pontos)
    {
        scoreJogador2 = scoreJogador2 + pontos;
        showText("Score: " + scoreJogador2, 85,getHeight()-50);
    }
}
FabioPacheco FabioPacheco

2018/3/23

#
It's not modified that world code
danpost danpost

2018/3/24

#
FabioPacheco wrote...
But I still have the problem with:
ActorIcon icon = new ActorIcon(violin);
The "new ActorIcon" doesn't accept the variable violin It says that "cannot find symbol- variable violin"
Where did you try to use this line of code? You will need to remove at minimum in your MyWorld class lines 6, line 51, lines 132 through 152 and lines 170 through 190. Then line 89 will need to use my example code for adding the Viiolin object. The other objects (Tambourine, Mandolin, Congal and Saxophone) should be added (at lines 77, 81, 85 and 93) similar to that of the Violin object.
You need to login to post a reply.
1
2