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

2020/11/23

Adding Object

1
2
NewbJava NewbJava

2020/11/23

#
I have been having trouble with adding more meteors for a game I am making. I want to add meteors moving from right to left. Below is my code than you for anyone who tries to help.
public class MyWorld extends World

[code]public class MyWorld extends World
{ 
    
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1, false);   
        prepare(); 
         
        
    } 
    public void act() 
    { 
       addMeteor(); 
       
    }  
    public void addMeteor() 
    { 
     addObject(new Meteors(),Greenfoot.getRandomNumber(400), 0);
    }
    private void prepare() 
    {  
        Rocket Rocket = new Rocket(); 
        addObject(new Rocket(), 100,200); 
        Rocket.setLocation(0,200);  
    }
}  
public class Obstacles extends Actor
{
    /**
     * Act - do whatever the Obstacles wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
    }    
    public void move() 
    { 
      setLocation(getX()-10,getY());    
        
    }  
    public void remove() 
    { 
        if (this.isAtEdge() == true)
        {
           World world;
           world = getWorld();
           world.removeObject(this);
           return;
    }
} 
}
public class Meteors extends Obstacles
{
  
    public void act() 
    {
        move(); 
        remove(); 
    }    
}
danpost danpost

2020/11/23

#
On line 21 in MyWorld, you are adding the actors at a variable x and at a minimum y, where you want a maximum x and a variable y.
danpost danpost

2020/11/23

#
Also, the rate of obstacle creation will need to be controlled. At one per act, you will have a virtual wall of obstacles approaching from the right.
NewbJava NewbJava

2020/11/23

#
Thank you for the help, but I am still a little confused what I should be putting in that line
danpost danpost

2020/11/23

#
NewbJava wrote...
Thank you for the help, but I am still a little confused what I should be putting in that line
The right edge is where x is 599 ( or "getWidth()-1" ) and a random y would have 400 possibilities ( "Greenfoot.getRandomNumber(400)" ).
NewbJava NewbJava

2020/11/23

#
I have changed line 21 to addObject(new Meteors(), 599, Greenfoot.getRandomNumber(400));and nothing seems to be happening. Are there any other issues?
NewbJava NewbJava

2020/11/23

#
danpost wrote...
NewbJava wrote...
Thank you for the help, but I am still a little confused what I should be putting in that line
The right edge is where x is 599 ( or "getWidth()-1" ) and a random y would have 400 possibilities ( "Greenfoot.getRandomNumber(400)" ).
danpost danpost

2020/11/23

#
NewbJava wrote...
Are there any other issues?
Not that I can see from what is provided. Post all your classes again for review of current content.
NewbJava NewbJava

2020/11/23

#
public class MyWorld extends World
{ 
    
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1, false);   
        prepare(); 
        
         
        
    } 
    public void act() 
    { 
       addMeteor(); 
       
    }  
    public void addMeteor() 
    { 
     addObject(new Meteors(),getWidth()-1,Greenfoot.getRandomNumber(getHeight()));
    }
    private void prepare() 
    {  
        Rocket Rocket = new Rocket(); 
        addObject(Rocket, 100,200); 
        Rocket.setLocation(0,200);  
       
    }
} 
public class Obstacles extends Actor
{
    /**
     * Act - do whatever the Obstacles wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
    }    
    public void move() 
    { 
      setLocation(getX()-10,getY());    
        
    }  
    public void remove() 
    { 
        if (this.isAtEdge() == true)
        {
           World world;
           world = getWorld();
           world.removeObject(this);
           return;
    }
} 
}
public class Meteors extends Obstacles
{
  
    public void act() 
    {
        move(); 
        remove(); 
    }    
}
NewbJava NewbJava

2020/11/23

#
danpost wrote...
NewbJava wrote...
Are there any other issues?
Not that I can see from what is provided. Post all your classes again for review of current content.
This is what I have now and there are no compile errors.
danpost danpost

2020/11/23

#
The codes provided have been tested and all seems good; other than the fact that there is a barrage of meteors. Maybe the problem is elsewhere (like possibly in your Rocket class?)
NewbJava NewbJava

2020/11/23

#
danpost wrote...
The codes provided have been tested and all seems good; other than the fact that there is a barrage of meteors. Maybe the problem is elsewhere (like possibly in your Rocket class?)
I can send you all my other classes. Will you be available in the next 20 min? Thank you for your help so far.
danpost danpost

2020/11/23

#
NewbJava wrote...
danpost wrote...
The codes provided have been tested and all seems good; other than the fact that there is a barrage of meteors. Maybe the problem is elsewhere (like possibly in your Rocket class?)
I can send you all my other classes. Will you be available in the next 20 min? Thank you for your help so far.
Yes -- I am here for now.
NewbJava NewbJava

2020/11/23

#
public class MyWorld extends World
{ 
    
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1, false);   
        prepare(); 
        
         
        
    } 
    public void act() 
    { 
       addMeteor(); 
       
    }  
    public void addMeteor() 
    { 
     addObject(new Meteors(),getWidth()-1,Greenfoot.getRandomNumber(getHeight()));
    }
    private void prepare() 
    {  
        Rocket Rocket = new Rocket(); 
        addObject(Rocket, 100,200); 
        Rocket.setLocation(0,200);  
       
    }
} 
public class Rocket extends Actor
{
    private int xdistance=0,ydistance=0; 
    boolean fire = true;
    
    public void addedToWorld(World MyWorld) 
    { 
       xdistance=getX(); 
       ydistance=getY(); 
    } 
    
    public void move() 
    { 
      double rx=xdistance-getX(); 
      double ry=ydistance-getY(); 
      double r=Math.sqrt(rx*rx+ry*ry); 
      int b=5; 
      int posx=0,posy=0;
      if(r>b)  
      {  
        posx=(int)(getX()+b*rx/r);  
        posy=(int)(getY()+b*ry/r);
        } else{  
            posx=xdistance; 
            posy=ydistance; 
        } 
        setLocation(posx,posy); 
    }
    public void act() 
    {
      if(Greenfoot.mouseMoved(null))  
      { 
          MouseInfo mouse=Greenfoot.getMouseInfo(); 
          xdistance=mouse.getX(); 
          ydistance=mouse.getY(); 
        } 
       move(); 
       fireLasars();
    }
    public void fireLasars()  
    {  
        if(Greenfoot.isKeyDown("a") && fire == true ) 
        { 
            getWorld().addObject(new Lasars(),getX() - 30,getY());  
            fire = false; 
        } 
        else if (!Greenfoot.isKeyDown("a")) 
        { 
            fire = true; 
        }
} 
}
public class Lasars extends Actor
{
    
    public void act() 
    { 
    lasarMove();  
    removeLasars(); 
    } 
    public void lasarMove() 
    {  
        setLocation(getX() + 5,getY()); 
        
    }
    public void removeLasars() 
    { 
        if (this.isAtEdge() == true)
        {
           World world;
           world = getWorld();
           world.removeObject(this);
           return;
    }
} 
}
public class Obstacles extends Actor
{
    /**
     * Act - do whatever the Obstacles wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
    }    
    public void move() 
    { 
      setLocation(getX()-10,getY());    
        
    }  
    public void remove() 
    { 
        if (this.isAtEdge() == true)
        {
           World world;
           world = getWorld();
           world.removeObject(this);
           return;
    }
} 
}
public class Meteors extends Obstacles
{
  
    public void act() 
    {
        move(); 
        remove(); 
    }    
}
NewbJava NewbJava

2020/11/23

#
danpost wrote...
NewbJava wrote...
danpost wrote...
The codes provided have been tested and all seems good; other than the fact that there is a barrage of meteors. Maybe the problem is elsewhere (like possibly in your Rocket class?)
I can send you all my other classes. Will you be available in the next 20 min? Thank you for your help so far.
Yes -- I am here for now.
That is what I have so far.
There are more replies on the next page.
1
2