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

2017/10/1

Problem to a game new

josem9507 josem9507

2017/10/1

#
I have the problem that after a lapse of time some of the asteroids get stuck in the vertex of the world
public class Asteroide extends Actor{
    public static final int NO_B = 0;
    public void act(){
       
       World g = getWorld();
       randomturn();
       teleAtEdge();
    
    }
    int direction = Greenfoot.getRandomNumber(360);
    int tumbleRate = 0;
    public void randomturn(){
    {  
    int rotation = getRotation();
    setRotation(direction);
    move(3);
    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);
            }
       }
       
    }
    

}
josem9507 josem9507

2017/10/1

#
code of 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(7);
    }
    
    
    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

#
Please refer to your classmates discussion thread here.
You need to login to post a reply.