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

2017/4/29

The ball needs to bounce in a random direction, except in 90 and this is for a ping pong game

Beatmaster42 Beatmaster42

2017/4/29

#
if (getX()<=9) { World myWorld = getWorld(); MyWorld wow = (MyWorld)myWorld; Counter1 counter1 = wow.getCounter1(); counter1.addCount(); setRotation(540-getRotation()); move(5); } if (getX()>=598) { World myWorld = getWorld(); MyWorld wow = (MyWorld)myWorld; Counter counter = wow.getCounter(); counter.addCount(); setRotation(540-getRotation()); move(-5); } if (getY() < 10 || getY() > getWorld().getHeight()-10) { setRotation(360-getRotation()); }
Beatmaster42 Beatmaster42

2017/4/29

#
After touching the edges and also when the ball touches the baby it should bounce randomly in opposite direction and this is the code i wrote Actor baby_1 = getOneIntersectingObject(baby1.class); if (baby_1!=null) { setRotation(getRotation() - 180); move(5); }
danpost danpost

2017/4/29

#
The code you wrote has no randomness in it. In fact, the ball will always reverse direction, going back along the path from whence it came. See if you can at least add some randomness to it -- even if you just divide by ninety to get an int value, then multiply by ninety (the combination reduces the current rotation to the highest multiple of 90 below the current rotation), then add a random value between 1 and 89.
Beatmaster42 Beatmaster42

2017/4/29

#
I added this "turn(Greenfoot.getRandomNumber(90));" code after removing "setRotation(getRotation() - 180);" but the ball some times keeps rotating at the edges and the baby(actor)
Beatmaster42 Beatmaster42

2017/4/29

#
danpost wrote...
The code you wrote has no randomness in it. In fact, the ball will always reverse direction, going back along the path from whence it came. See if you can at least add some randomness to it -- even if you just divide by ninety to get an int value, then multiply by ninety (the combination reduces the current rotation to the highest multiple of 90 below the current rotation), then add a random value between 1 and 89.
I added this "turn(Greenfoot.getRandomNumber(90));" code after removing "setRotation(getRotation() - 180);" but the ball some times keeps rotating at the edges and the baby(actor)
Beatmaster42 Beatmaster42

2017/4/29

#
if (getY() < 10 || getY() > getWorld().getHeight()-10) { turn(Greenfoot.getRandomNumber(45) + 30); move(7); } I did this so the balls moves randomly and moves 7 steps as soon as it touches the wall and it wont keep circling on the wall or baby but it doesn't work and it turns in a "U" pattern instead of "V "
danpost danpost

2017/4/29

#
You will probably need another condition to bounce so that bouncing is not repeated continuously while in contact with the baby or the edge. The added condition will determine in which direction the ball is currently moving in along the horizontal and vertical. Something like this:
1
if (isTouching(Baby.class) && getHorizontalDirection()*(getX()-getWorld().getWidth()/2) > 0)
where the getHorizontalDir method is:
1
2
3
4
private int getHorizontalDirection()
{
    return (int)Math.signum(Math.cos(Math.toRadians((double)getRotation())));
}
Then use something similar with 'getVerticalDirection (using 'sin'), 'getY' and the world height for the top and bottom edges.
Beatmaster42 Beatmaster42

2017/4/30

#
public int hRandom=(1 + (Greenfoot.getRandomNumber(10))), vRandom=(1 + (Greenfoot.getRandomNumber(10))); if(getY() > getWorld().getHeight()-10) { vRandom= -vRandom+1; Greenfoot.playSound("wall.wav"); } I did this so that there will be a random number at hRandom and vRandom so the ball will move randomly.
Beatmaster42 Beatmaster42

2017/4/30

#
public void topbottomwall() { if (getY() < 10 ) { vRandom= -vRandom; Greenfoot.playSound("wall.wav"); } if(getY() > getWorld().getHeight()-10) { vRandom= -vRandom; Greenfoot.playSound("wall.wav"); } } The above code are for touching the top and bottom wall but when the ball touches the wall the sound dosen't paly
danpost danpost

2017/4/30

#
Beatmaster42 wrote...
< Code Omitted > The above code are for touching the top and bottom wall but when the ball touches the wall the sound dosen't paly
If the sound is not playing, then either the method is not being called or neither of the conditions are ever true. To test, try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void topbottomwall()
{
    System.out.println("topbottomwall method is being called");
    if (getY() < 10)
    {
        System.out.println("top edge detected (first if condition is true)");
        vRanddm = -vRandom;
        Greenoot.playSound("wall.wav");
    }
    if (getY() > getWorld().getHeight()-10)
    {
        System.out.println("bottom edge detected (second if condition is true)");
        vRanddm = -vRandom;
        Greenoot.playSound("wall.wav");
    }
}
Report back any new terminal display.
Beatmaster42 Beatmaster42

2017/4/30

#
danpost wrote...
Beatmaster42 wrote...
< Code Omitted > The above code are for touching the top and bottom wall but when the ball touches the wall the sound dosen't paly
If the sound is not playing, then either the method is not being called or neither of the conditions are ever true. To test, try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void topbottomwall()
{
    System.out.println("topbottomwall method is being called");
    if (getY() < 10)
    {
        System.out.println("top edge detected (first if condition is true)");
        vRanddm = -vRandom;
        Greenoot.playSound("wall.wav");
    }
    if (getY() > getWorld().getHeight()-10)
    {
        System.out.println("bottom edge detected (second if condition is true)");
        vRanddm = -vRandom;
        Greenoot.playSound("wall.wav");
    }
}
Report back any new terminal display.
when the ball touched the bottom wall the terminal displayed these topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called topbottomwall method is being called bottom edge detected (second if condition is true) the methods are called on public void act() { moves(); touchingbaby(); topbottomwall(); rightleftwall(); getWorld().addObject(new effect(),getX(),getY()); }
Beatmaster42 Beatmaster42

2017/4/30

#
Thanks for the help and the sound started to play after I restarted my computer. thanks again
You need to login to post a reply.