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

2017/12/20

Accesing a method from another Actor

Banchion Banchion

2017/12/20

#
Hi. I need help with my Greenfoot project. I am trying to acces a method from this object called Bee. The method is in another actor's code, called Butt(short for Butterfly :))) Can you please help ? public class Bee extends Actor { int a=0; public void act() { Click(); Butt(); } public void Click() { if (Greenfoot.mouseClicked(this)) { Butt(); } } public void Butt() { Butt butt = (Butt) getOneObjectAtOffset(0, 0, Butt.class); butt.yaz(); } } public class Butt extends Actor { public int a=0; public void act() { yaz(); } public void yaz() { World world; world = getWorld(); if(a==2) world.removeObject(this); } } Please please please help me. Thanks in advance :)
danpost danpost

2017/12/20

#
Change this line:
butt.yaz();
to this:
if (butt != null) butt.yaz();
Also remove this line:
Butt();
from the act method.
Banchion Banchion

2017/12/20

#
I want to click on the bee twice in order for the butterfly to disappear. How can i do that? I think my code was poorly done from the beggining.
nolttr21 nolttr21

2017/12/20

#
right after the "int a = 0;" line add this line without the quotes: "public boolean clickedOnce = false;" then use this code in place of your Click() method
 public void Click()
    {
         if (Greenfoot.mouseClicked(this))
         {
            if (clickedOnce)
            {
                 Butt(); 
            }
            clickedOnce = true;
            
            }

    }
You need to login to post a reply.