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

2016/12/12

Asteroids Random Direction

CakeMonster CakeMonster

2016/12/12

#
Hi, I am making an Asteroids game where a space ship is at the center of the screen and I need the asteroids to all go different directions. Like what I mean is that the asteroids are supposed to go to the edge of the screen and come out on the other side. Right now All I can get them to do is go one direction at the center of the screen but not at random angles.
CakeMonster CakeMonster

2016/12/12

#
  
public void act()
    {         
        move(5);
        aSize = size;
        World g = getWorld();
        if (isAtEdge())
        {
          int a = 20;
     int w = getWorld().getWidth();
     int h = getWorld().getHeight();
     int x = getX();
     int y = getY();
 
     if (x < a || x > w-a-1) setLocation(w-x-1, y);
     if (y < a || y > h-a-1) setLocation(x, w-y-1);
            
            
  
    }
danpost danpost

2016/12/12

#
Please show the entire class code. Also, what code are you trying to use to set a random angle?
CakeMonster CakeMonster

2016/12/13

#
public class BigAsteroid extends Actor
{
    /**
     * Act - do whatever the BigAsteroid wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    /** Size of this asteroid */
 

    /** When the stability reaches 0 the asteroid will explode */
  
   
   
    
    
    
    public void act()
    {         
       move(4);
       World g = getWorld();
       randomturn();
       teleAtEdge();
}
    public void killPlayer()
    {
        if(isTouching(Player.class))
        {
            removeTouching(Player.class);
            Greenfoot.stop();
    }
}
    
    public void randomturn()
    {
        
        getRotation(Greenfoot.getRandomNumber(90));
        

}
    public void teleAtEdge()
    {
        if (isAtEdge())
        {
            int a = 20;
            int w = getWorld().getWidth();
            int h = getWorld().getHeight();
            int x = getX();
            int y = getY();
 
            if (x < a || x > w-a-1) {
            setLocation(w-x-1, y);
        }
            if (y < a || y > h-a-1) {
            setLocation(x, w-y-1);
        }
      }
}
}
CakeMonster CakeMonster

2016/12/13

#
danpost wrote...
Please show the entire class code. Also, what code are you trying to use to set a random angle?
I am using getRotation
CakeMonster CakeMonster

2016/12/13

#
danpost wrote...
Please show the entire class code. Also, what code are you trying to use to set a random angle?
AHEM** I MEAN setRotation
CakeMonster CakeMonster

2016/12/13

#
On Line 37 that was supposed to be setRotation but i copied and pasted it as getRotation when I was posting it
danpost danpost

2016/12/13

#
From what I can tell from the code given is that the asteroid will basically remain in one general area while tumbling around non-uniformly. To have the asteroid tumble and still maintain a normal trajectory, you will need an instance field to hold the direction of movement and a local field to hold the current rotation while the movement is being done:
// with instance field
int direction = Greenfoot.getRandomNumber(360);

// sample act method might be
public void act()
{
    int rotation = getRotation(); // save tumble state
    setRotation(direction); // set rotation to direction of movement
    move(5); // move
    setRotation(rotation+tumbleRate); // return tumble state rotation plus change in tumble per act
    // wrap code window here
}
CakeMonster CakeMonster

2016/12/15

#
Thanks M8
You need to login to post a reply.