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

2017/12/3

Problem with non-random actors

Beamer Beamer

2017/12/3

#
I am creating my first game for a uni assignment and have an issue with some of my asteroids. They start off-screen (either left or right) at a random height, have a random speed, and a random angle. But for some reason, every now and then, an occasional asteroid flies horizontally, either at 0° or 180°. They have random height and speed, but not angle. Space (World) declarations:-
    public static int asteroidHgt;     // Random Y co-ordinate for each Asteroid start position
    public static int asteroidSpd;     // Random speed of each Asteroid
    public static int asteroidDir;     // Random direction for each Asteroid
Method inside Space (World):-
    public void sendAsteroid() {
        if(Greenfoot.getRandomNumber(1000)<5) {    // 0.5% chnce for new Asteroid
        asteroidSpd = Greenfoot.getRandomNumber(2)+1;
        asteroidHgt = Greenfoot.getRandomNumber(300);
        Asteroid asteroid = new Asteroid();
            if(Greenfoot.getRandomNumber(2)==0) {  // Asteroid starts at either left of right side
               addObject(asteroid,-20,asteroidHgt);
               asteroidDir = 45-Greenfoot.getRandomNumber(30);
            }
            else {
               addObject(asteroid,820,asteroidHgt);
               asteroidDir = 135+Greenfoot.getRandomNumber(30);
            }
        }
Asteroid Actor class
public class Asteroid extends Actor
{
    public int astHgt;
    public int astSpd;
    public int astDir;
    public Asteroid()
    {
        astHgt=Space.asteroidHgt;       // Sets random Asteroid starting height
        astSpd=Space.asteroidSpd;       // Sets random Asteroid speed
        astDir=Space.asteroidDir;       // Sets random Asteroid direction (rotation)
    }
    
    /**
     * Act - do whatever the Asteroid wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(Space.gameStarted == false || Space.stage==5) return;
        setRotation(astDir);
        move(astSpd);
        if(Space.stage != 2) checkCollision();  // Checks for collisions but not during Stage 2
    }
    
    public void checkCollision()
    {
         //Everything here works fine and doesn't use or change the variables
    }
}
Thank you for your time. Any help would be much appreciated. I am learning :)
Super_Hippo Super_Hippo

2017/12/3

#
I would remove the static variables and give the asteroid their random speed and direction in their constructor. The random height when adding them to the world. Only the speed has to be saved in the asteroid then.
Beamer Beamer

2017/12/3

#
I tried that and it gave me an error saying that the object wasn't in the world. I thought that was strange. I thought it wouldn't look at that until one was created.
Beamer Beamer

2017/12/3

#
I just tried again and the problem is when I use a getX() command to work out whether the asteroid is coming from left or right.
            if(getX()==-20) { 
               astDir = 45-Greenfoot.getRandomNumber(30);
            }
            else {
               astDir = 135+Greenfoot.getRandomNumber(30);
            }
It doesn't like this in the constructor.
Beamer Beamer

2017/12/3

#
OK, so I made a static variable to pass left or right and it now chooses the direction randomly from the Asteroid constructor. Still the same problem though, definitely still creating asteroids with no direction. hmm... now that I type that... that's makes sense for the 0° ones, but how do the ones from right side get set to 180°? Edit: I just removed the Greenfoot.getRandomNumber(30) from the constructor and left the numbers at 45 and 135 and absolutely NO horizontal Asteroids. Changed the angles to 60 and 120 to make sure as well. The problem is definitely with the getRandomNumber. Any ideas? Last Edit, heading to bed. I just thought to inspect a horizontal Asteroid. It's rotation was set to 22° but it was moving left to right (0°). So the Random is working, just each now and then the asteroid doesn't want to move the direction it's facing. Oh, and it only moves that way when the random number is invoked. Probably about 1 in every 5 Asteroids do this.
Super_Hippo Super_Hippo

2017/12/3

#
An object which moves with speed 1 can only move in 8 directions = 0°, 45°, 90°, ... because it can't move a half cell or something. You could import the SmoothMover class and let the Asteroid extend it though. Other than that, without static variables:
//in world
int side = Greenfoot.getRandomNumber(2);
Asteroid asteroid= new Asteroid();
addObject(asteroid, -20+840*side, Greenfoot.getRandomNumber(300));
asteroid.setRotation(180*side-30+Greenfoot.getRandomNumber(61)); //or something like that
//in Asteroid
private int speed = Greenfoot.getRandomNumber(2)+1;

public void act()
{
    move(speed);
}
Beamer Beamer

2017/12/4

#
Thankyou @Super_Hippo, that makes perfect sense.
You need to login to post a reply.