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

2020/7/1

how to bounce back a ball when it hits the player

1
2
Taeyo Taeyo

2020/7/1

#
Actor player1= getOneIntersectingObject(player1.class); if (player1!=null) if (Greenfoot.getRandomNumber(100) < 5) { if(isTouching(player1.class)); turn(Greenfoot.getRandomNumber(180)-35); } Actor player2= getOneIntersectingObject(player2.class); if (player2!=null) if (Greenfoot.getRandomNumber(100) < 5) { if(isTouching(player2.class)); turn(Greenfoot.getRandomNumber(200)-35); }
danpost danpost

2020/7/1

#
In general, you would just use something like:
if (isTouching(player1.class) || isTouching(player2.class))
{
    move(-speed);
    turn(90+Greenfoot.getRandomNumber(180));
]
where any touching is removed by undoing the current move that caused the "collision" before turning. Otherwise, the ball may glitch about on the player (turn repeatedly).
Taeyo Taeyo

2020/7/2

#
when the ball hits the objects it stucks for couple of secs how to slove that
danpost danpost

2020/7/2

#
Taeyo wrote...
when the ball hits the objects it stucks for couple of secs how to slove that
Show current movement/collision code.
Taeyo Taeyo

2020/7/2

#
Actor player1 = getOneIntersectingObject(player1.class); if (player1_1!=null) if(isTouching(player2.class)) { m0ve(3) turn(90+Greenfoot.getRandomNumber(180)); } Actor player2 = getOneIntersectingObject(player2.class); if (player2_1!=null) if(isTouching(player2.class)) { m0ve(3) turn(90+Greenfoot.getRandomNumber(180)); } public String hitEdge() { int x = getX(); int y = getY(); World myWorld = getWorld(); int rightSide = myWorld.getWidth() - 1; int bottomSide = myWorld.getHeight() -1; if(y == 0){ return "top"; }else if (x == 0) { return"left"; }else if(x == rightSide){ return "right"; } else if (y == bottomSide) {return "bottom"; }else { return "null"; } } public void bounceAtEdge() { String edge = hitEdge(); if(edge == "top" || edge == "bottom") { X = X * -1; }else if (edge == "left" || edge == "right" ) { Y = Y * -1; } }
Taeyo Taeyo

2020/7/2

#
the ball should bounce back when it hits the player/wall but it gets stuck for couple of sec when it hits
danpost danpost

2020/7/2

#
Taeyo wrote...
the ball should bounce back when it hits the player/wall but it gets stuck for couple of sec when it hits
Try what I gave above. Replace "speed" with "3".
Taeyo Taeyo

2020/7/2

#
didn;t work
danpost danpost

2020/7/2

#
Taeyo wrote...
didn;t work
Show class code. Need to see where you put it / how you tried it.
Taeyo Taeyo

2020/7/2

#
Actor player1 = getOneIntersectingObject(player1.class);
        if (player_1!=null)
        if(isTouching(player2.class)) 
        {
 move(speed)
turn(90+Greenfoot.getRandomNumber(180));
       }
Actor player2 = getOneIntersectingObject(player2.class);
        if (player_2!=null)
        if(isTouching(player2.class)) 
        {
 move(speed)
turn(90+Greenfoot.getRandomNumber(180));
       }
public String hitEdge()
{
    int x = getX();
    int y = getY();
    World myWorld = getWorld();
    int rightSide = myWorld.getWidth() - 1;
    int bottomSide = myWorld.getHeight() -1;
    if(y == 0){
        return "top";
    }else if (x == 0)
    {
        return"left";
    }else if(x == rightSide){
        return "right";
          }
    else if (y == bottomSide)
    {return "bottom";
}else
        {
        return "null";
}
}
public void bounceAtEdge()
{
    String edge = hitEdge();
    if(edge == "top" || edge == "bottom")
    {
         X = X * -1;
    }else if (edge == "left" || edge == "right" )
   {
      Y = Y * -1;
}
}
danpost danpost

2020/7/2

#
Taeyo wrote...
<< Code Omitted >>
Where is the top half of the class?
Taeyo Taeyo

2020/7/2

#
public int speed = 5; public void act() { move(speed); player1(); player2(); hitTheEdge(); bounceAtEdge(); }
Taeyo Taeyo

2020/7/2

#
can you fix this with your code
danpost danpost

2020/7/2

#
Taeyo wrote...
can you fix this with your code
Please show the entire class at one time.
Taeyo Taeyo

2020/7/5

#
    public static int xSpeed, ySpeed;
    public int direction = 5;
    public void act() 
    {
        move(direction);
    }

        Actor player1 = getOneIntersectingObject(player1.class);
        if (player_1!=null)
        {
        if(isTouching(player1.class)) 
        {
            turn(90+Greenfoot.getRandomNumber(180));
            World myWorld = getWorld();
            MyWorld myworld = (MyWorld)myWorld;
            Counter1  counter1 = myworld.get Counter1();
            counter1.addScore(1);
        }
Actor player2 = getOneIntersectingObject(player2.class);
        if (player_2!=null)
        {
        if(isTouching(player2.class)) 
        {
            turn(90+Greenfoot.getRandomNumber(180));
            World myWorld = getWorld();
            MyWorld myworld = (MyWorld)myWorld;
             Counter2 counter2 = myworld.getCounter2();
             counter2addScore(1);
        
    }
        if(isAtEdge()){
            turn(90+Greenfoot.getRandomNumber(180));
        }
        xSpeed = getX();
        ySpeed = getY();
    }
}
There are more replies on the next page.
1
2