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

2019/4/21

If else statement problem

NoobFected NoobFected

2019/4/21

#
So I have implemented a collision function where whenever a centipede hits an object, it will move down and continue to move towards the direction it was originally going. When it reaches the bottom of the world, the centipede will turn around and continue to make the zig zag movement it was originally doing upward. The problem is, the collision function stops working when it gets to the bottom of the world and turns around. Does anyone know what the problem is? Here is my code for the centipede:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * Write a description of class Ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Centipede extends Actor
{
    /**
     * Act - do whatever the Ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    boolean bounceWall;
    boolean uTurn;
    int width; 
    int height; 
    int dx;
    int dy;
    int counter;
    int bounceCounter;
    
   public Centipede(){
        GreenfootImage image = getImage();
        image.scale(25, 25);
        
        bounceWall = false;
        width = image.getWidth();
        height = image.getHeight();
        dx = width / 5;
        dy = 0;
        counter = 0;
        bounceCounter = 0;
        uTurn = false;
        setImage(image);
    }
    
    public void act() {   
        if(counter != 0){
            wallCollision();
        }
        counter++;
        mushroomCollision();
        if (getWorld() == null) return;
        setLocation(getX() + dx, getY() + dy);
        dy = 0;
        bounceWall = false;
    }    
    
    private void wallCollision(){
        if(bounceWall == false){
            if(getX() >= getWorld().getWidth() - width / 5 || getX() <= width / 5){
                if(getY() >= getWorld().getHeight() - height){
                    turn(-90);
                    dx = 0;
                    dy = -height;
                    uTurn = true;
                }else{
                    if(bounceCounter == 0){
                        dx = 0;
                        if(uTurn != true){
                            dy = height;
                        }else{
                            dy = -height;
                        }
                    }
                }
                if(bounceCounter == 1){
                    if(getX() >= getWorld().getWidth() - width / 5){
                        dx = -width / 5;
                    }else if(getX() <= width / 5){
                        dx = width / 5;
                    }
                    bounceCounter = -1;
                }
                bounceWall = true;
                bounceCounter++;
                turn(90);
            }
        }
    }
    
    private void mushroomCollision(){
        int rotation = getRotation();
        if(rotation == 0){
            Actor mushroom = getOneObjectAtOffset(width, 0, Mushroom.class);
            if(mushroom != null){
                if(uTurn != true){
                    dy = height;
                }else{
                    dy = -height;
                }
            }
        }else if(rotation == 180){
            Actor mushroom = getOneObjectAtOffset(-width, 0, Mushroom.class);
            if(mushroom != null){
               if(uTurn != true){
                    dy = height;
               }else{
                    dy = -height;
               }
            }
        }
    }
danpost danpost

2019/4/21

#
I think you are using turn in places where you should be using setRotation (line 57 and 81). You are turning -90 degrees (making a left face) when hitting a side edge along the bottom. That will make the actor face in opposite directions (one up and one down) depending on the side hit.
You need to login to post a reply.