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

2015/4/16

how to make a ball bounce up when it hits the mover and bounce down when it hits the block and also bounce when it hits the edges?

vela vela

2015/4/16

#
[Disallowed URL]
vela vela

2015/4/16

#
vela vela

2015/4/16

#
 public void act() 
    {
        // Add your action code here.
       
        move();
        moveDown();
       
    }
    public void move()
    {
        Actor ball=getOneIntersectingObject(mover.class);
        if(ball!=null)
        {setLocation(getX(),getY()-1);
        }
    }
    
    public void moveDown()
    { Actor ball=getOneIntersectingObject(block.class);
        if(ball!=null)
        {setLocation(getX(),getY()+1);
        }
    }
danpost danpost

2015/4/16

#
First, you need to think about how you are going to have the ball move without any collisions taking place. The code you gave above does not have the ball move unless it is intersecting a mover or a block. The 'move' method says "move up if intersecting a mover" and the 'moveDown' method says "move down if intersecting a block". These are things that happen at one moment in time -- not continuously. Once the ball no longer intersects the object, the action stops executing (the ball will no longer move). You need to break it down to what you want the ball to do at any moment in time.
vela vela

2015/4/17

#
vela wrote...
can your please show me the code @danpost
danpost danpost

2015/4/17

#
You could check out the codes of various brick-breaker scenarios on the site. Or, for an example from one of my scenarios, check out my Pong Pinball scenario. It contains a perfect bouncing system (or real close to it) -- and contains some documentation also.
danpost danpost

2015/4/17

#
vela wrote...
can your please show me the code @danpost
You will find that the code for this is not that simple and straight-forward. You will encounter issues like (1) the ball is limited in the number of directions it travels (a smooth-moving system must by employed); and (2) how does one determine which side of the block is encountered so that the correct axis of movement, horizontal or vertical, can be reversed (truly, the only way is to move the ball back to the moment of contact and check the position of the ball at that point with the position and size of the block).
You need to login to post a reply.