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

2018/3/20

When an object has hit an edge 8 times, have the object disappear

1
2
Miguel.Valdez Miguel.Valdez

2018/3/21

#
actually everything worked like a charm!!! my new layout
public class dolphin extends Actor
{
    private int counter = 0;
    /**
     * Act - do whatever the dolphin 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.
        move(20);
        if (isAtEdge())
        {
            turn(135+Greenfoot.getRandomNumber(90));
            counter++; //increments everytime dolphin hits edge
            disappear();
        }
    }
      
    /**
     * keep count of how many times object has hit edge.
     * if counter accumelates to == 5 remove object.
     */
    public void disappear()
    {
        if (counter == 8)
        {
            getWorld().removeObject(this); //makes object vanish
        }
    }
    
    public dolphin()
    {
     turn(Greenfoot.getRandomNumber(360));
    }
}
Miguel.Valdez Miguel.Valdez

2018/3/21

#
how does this make it turn 180 plus or minus 45 degrees?
turn(135+Greenfoot.getRandomNumber(90));
i want to be able to understand how exactly it works, rather than just inserting things to make my project work. Without no knowledge to be able to use in the future.
Vercility Vercility

2018/3/21

#
It turns for 135 +( random number between 0 inclusive and 90 exclusive) which is anything between 135 and 224
Miguel.Valdez Miguel.Valdez

2018/3/21

#
ok gotcha, just in case my professor asks how I know this already. I can explain what it is. I as well went into greenfoot class documentation and looked it up.
danpost danpost

2018/3/21

#
Miguel.Valdez wrote...
I as well went into greenfoot class documentation and looked it up.
That is an excellent habit to get into. Not only can you learn of given methods, but of what other methods are available. Any class used for the first time should have its documentation looked over.
Vercility Vercility

2018/3/21

#
"I Googled it" is always a legitimate answer, assuming you Actually understood it and didn't just copy paste :P Aside from that, that's how I do it as well, for said reason but also because you can never know if there might be a mistake if you don't understand yourself
You need to login to post a reply.
1
2