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

2017/10/1

Problem to a game

diablo95 diablo95

2017/10/1

#
I have the problem that sometimes when an asteroid touches the right edge, it stays stuck on that edge and does not pass to the other edge of the world like the others and it gets stuck on that edge. help please
public class Asteroide extends Actor{
    
    public void act(){
       move(5);
       World g = getWorld();
       randomturn();
       teleAtEdge();
    
    }
    int direction = Greenfoot.getRandomNumber(360);
    int tumbleRate = 0;
    public void randomturn(){
    {  
    int rotation = getRotation();
    setRotation(direction);
    move(5);
    setRotation(rotation+tumbleRate);
    }
    } 
   
    public void teleAtEdge(){
       if (isAtEdge()){
            
            int w = getWorld().getWidth();
            int h = getWorld().getHeight();
            int x = getX();
            int y = getY();
            if (x >= w - 1) {
                setLocation(0, y);
            }else if ( x <= 1 ) {
                setLocation(w,y);
            } 
            if( y <= 1 ) {
                setLocation(x,h);
            }else if ( y >= h - 1 ) {
                setLocation(x,0);
            }
       }
       
    }
    

}
diablo95 diablo95

2017/10/1

#
this the world
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.ArrayList;

public class Mundo extends World
{
    public static final int NO_BORDE = 0;
    public static final int BORDE_IZQUIERDO = 1;
    public static final int BORDE_DERECHO = 2;
    public static final int BORDE_ARRIBA = 3;
    public static final int BORDE_ABAJO = 4;
    
    private Corazon corazon;
    private NaveEspacial nave;
    private ArrayList<Asteroide> asteroides;
    private ArrayList<Bala> balas;
    
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public Mundo()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        nave = new NaveEspacial();
        addObject(nave, 300, 200);
        //nuevo
        crearAsteroides(2);
    }
    
    
    public int identificarBordeEnElQueEstaLaNave() {
        if( nave.getX() >= getWidth() - 1 ) {
            return BORDE_DERECHO;
        } else if ( nave.getX() <= 1 ) {
            return BORDE_IZQUIERDO;
        } 
        
        if( nave.getY() <= 1 ) {
            return BORDE_ARRIBA;
        } else if ( nave.getY() >= getHeight() - 1 ) {
            return BORDE_ABAJO;
        }
        
        return NO_BORDE;
    }
    //nuevo
    public void crearAsteroides(int numeroAsteroides){
        for(int i=0;i<numeroAsteroides;i++){
            Asteroide r=new Asteroide();            
            int x=Greenfoot.getRandomNumber(getWidth());
            int y=Greenfoot.getRandomNumber(getHeight());
            addObject(r,x,y);
            
        }    
    }
    
    
}
danpost danpost

2017/10/1

#
Your move command in the act method is interfering with the move in the teleAtEdge method. Remove the move from within the act method (line 4 of the Asteroide class code above).
diablo95 diablo95

2017/10/1

#
you have the reason many thanks, but now after a good time to run the game, some of the asteroids are stuck in the diagonals.
danpost danpost

2017/10/1

#
That is probably because when they move onto an edge, they are restricted in one direction and therefore eventually (after enough times of this) find a corner where they are finally restricted by two adjacent edges. This can be avoided by unbounding the world and "mod"ing the coordinates with the dimensions of the world.
You need to login to post a reply.