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/20

#
so currently this is what im trying to resemble to happen. "Put in some code to allow the object to rotate when it gets to the edge of the world so that it doesn't get stopped when it reaches the edge. When an object has hit an edge 8 times, have the object disappear."
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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(-90);
            counter = counter + 1; //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);
        }
    }
}
but my counter does not seem to be working, so when object hits the edge 8 times, it disappears. Any help is appreciated, THANK YOU.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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(-90);
            move(5);
            counter = counter + 1; //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);
        }
    }
}
danpost danpost

2018/3/20

#
How are the initial rotations of the dolphins set?
Miguel.Valdez Miguel.Valdez

2018/3/20

#
initial rotations? that might come off somewhat like a dumb question. But I don't really know what you mean by that. I sent it to my professor and all he had to say is: "Your problem is that once you hit the edge, you are not moving far enough avoid being at the edge on the next call. Also, you should turn before moving to further ensure that you are away from the edge." he never really went over in class how to make this possible, so I've just been winging it, with as well trying to find any help I can in the greenfoot book.
Vercility Vercility

2018/3/20

#
well their rotations :o Angle in degree, what u get from getRotation You always move by 20 units so moving 5 after turning might not get you away from the edge completely (assuming the "edge" is an area wider than 5 Pixel, don't know how greenfoot implemented it) As a side note, if your dolphin is moving right turning it - 90 degrees will make it face upwards and you'll still be at the edge, so you will always count twice
Miguel.Valdez Miguel.Valdez

2018/3/20

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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())
        {
            move(5);
            setRotation(getRotation()-40);
            counter = counter + 1; //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);
        }
    }
}
 
so getRotation is -40
Miguel.Valdez Miguel.Valdez

2018/3/20

#
this seemed to do the trick just bouncing back and forth with getRotation()-180); its kinds of weird it just going back forth though. It works though.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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())
        {
            move(5);
            setRotation(getRotation()-180);
            counter = counter + 1; //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);
        }
    }
}
Miguel.Valdez Miguel.Valdez

2018/3/21

#
turn(180); seems to do the exact same thing as well though.
Vercility Vercility

2018/3/21

#
Get/set - 180 and turn - 180 are the same thing Is it bouncing back/forth right at the edge? It yes, change move(5) to move(20)
Miguel.Valdez Miguel.Valdez

2018/3/21

#
Thank you Vercility and Danpost. @vercility I will keep that in mind. Once again thank you. in my programming class CS -134 we just started with greenfoot, so getting used to actually structuring the bones for everything to function is all new to me. I'm glad I signed up for this discussion board.
Miguel.Valdez Miguel.Valdez

2018/3/21

#
and yet is its vercility, it goes from one side to the other (right to left and so on). Then on the 8th time, it vanishes.
Miguel.Valdez Miguel.Valdez

2018/3/21

#
i just removed the move pertaining to isAtEdge and it all still functions. Making me think was it even necessary?
Miguel.Valdez Miguel.Valdez

2018/3/21

#
new layout
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
new layout
    public void act()
    {
        // Add your action code here.
        move(20);
        if (isAtEdge())
        {
            turn(180);
            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);
        }
    }
}
danpost danpost

2018/3/21

#
Unless you change the rotation of the dolphins when they are created in the world class, their initial rotations will be zero. The initial rotations can be randomized in the world class as just stated or in a dolphin class constructor block:
1
2
3
4
public dolphin()
{
    turn(Greenfoot.getRandomNumber(360));
}
You could also vary the turn amount when hitting an edge with something like this:
1
turn(135+Greenfoot.getRandomNumber(90));
which will turn 180 plus or minus 45 degrees.
Miguel.Valdez Miguel.Valdez

2018/3/21

#
danpost i tried adding
1
2
3
4
public dolphin()
{
    turn(Greenfoot.getRandomNumber(360));
}
but it wont let me, saying that it is missing a returntype.
danpost danpost

2018/3/21

#
Miguel.Valdez wrote...
danpost i tried adding << Code Omitted >> but it wont let me, saying that it is missing a return type.
You did try to add it into your dolphin class, correct? If yes, show what you tried -- post the class code.
There are more replies on the next page.
1
2