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

2013/1/14

Body hits wall and bounces back?

kaciedy kaciedy

2013/1/14

#
I am not completely for sure as to how to go about this step, but I have what I have attempted. My teacher said I needed to use revertVertical and revertHorizontal. I also have code to change the color of an object when it hits an object of the same class, but it turns them all black and not a random color. game with full source
 public void colorChange()
    {
        List<Body> bodies = getIntersectingObjects(Body.class);
        
        for(Body b : bodies)
        {
            int h = b.getImage().getHeight();
            GreenfootImage x = new GreenfootImage(h,h);
            Color c = new Color(Greenfoot.getRandomNumber(256), Greenfoot.getRandomNumber(256), Greenfoot.getRandomNumber(256));
            x.fillOval(0,0,h-1,h-1);
            b.setImage(x);
           
        }
    }
        
    public void bounce()
    {
        if(getX() == 0 || getX() == 920)
        {
            if(getY() == 0 || getY() == 640)
            {
                getMovement().revertVertical();
                getMovement().revertHorizontal();
            }
        }
    }
danpost danpost

2013/1/15

#
Without opening your scenario and looking at that code, I can see from the above that your 'bounce' method needs to be adjusted. The conditions should be dealt with seperately or both reversions will happen on any edge.
davmac davmac

2013/1/15

#
Also, line 9 creates a new color, but you never set that color as the draw color. So, line 10 just draws in black.
kaciedy kaciedy

2013/1/15

#
Alright, thank you both! I have it fixed.
You need to login to post a reply.