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

2019/6/28

shootball

ronald ronald

2019/6/28

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class MyWorld extends World
{

    Counter counter = new Counter();
    
    public MyWorld()
    {    

        super(900, 600, 1); 
        prepare();
    }
    public Counter getCounter()
    {
        return counter;
    }
    
    private void prepare()
    {
        Rocket rocket = new Rocket();
        addObject(rocket,91,299);

        Ball [] ball = new Ball[7];
        for(int i=0; i<ball.length; i++)
        {
            ball[i] = new Ball();
            int ballX = Greenfoot.getRandomNumber(getWidth());
            int ballY = Greenfoot.getRandomNumber(getHeight());
            addObject(ball[i], ballX, ballY);
        }
        addObject(counter, 38, 49);
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Ball extends Actor
{
   
    public void act() 
    {
        move(5);
        
    }
    public void move()
    {
       int x = getX();
       int y = getY();
        
        
       if(Greenfoot.isKeyDown("right"))x++;
       if(Greenfoot.isKeyDown("left"))x--;
       if(Greenfoot.isKeyDown("up"))y--;
       if(Greenfoot.isKeyDown("down"))y++;
       setLocation(x,y);
    
       
            
       
       ((MyWorld)getWorld()).getCounter().addScore();
          
       
    }
    
    }

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Counter extends Actor
{
   int score = 0; 
    
   public Counter()
    {
        updateImage();
    }
    
    public void updateImage()
    {
        setImage(new GreenfootImage("Score :" + score, 20, Color.WHITE, Color.BLACK));
    }
    public void addScore()
    {
        score++;
        updateImage();
    } 
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Fire extends Actor
{
    
    public void act() 
    {
     move(10); 
     Actor ball = getOneIntersectingObject(Ball.class);
     if (ball!=null)
     {
         World myWorld = getWorld();
         myWorld.removeObject(ball);
         myWorld.removeObject(this);
     } 
     else if (isAtEdge()!=false)
     {
         World myWorld = getWorld();
         myWorld.removeObject(this);
     }
    }    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Rocket extends Actor
{
   Fire fire = new Fire();
    
    public void act() 
    {
      
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-5, getY());
        }
        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+5, getY());
        }
        if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-5);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+5);
        }
        fire();
        fireOnCommand();
    }
        
    private void fire()
    {
        Actor ball = getOneIntersectingObject(Ball.class);
        if(ball != null)
        {
            World myWorld = getWorld();
            myWorld.removeObject(this);
        }
    }
    
    public void fireOnCommand()
    {
        if(Greenfoot.isKeyDown("a"))
        {
            World myWorld = getWorld();
            myWorld.addObject(fire, 0, 0);
            fire.setLocation(getX(), getY());
            
        }
    } 
     
}
I would like balloons to go from right to left kind of move getrandomnumber and the score is displayed I did some code, I do not know if it's good I do not manage to connect the score with the balloons that are constantly coming can you help me correct
danpost danpost

2019/6/28

#
No balloon codes given. However, connection to score would be similar to line 27 in the Ball class.
ronald ronald

2019/6/28

#
OK no worries will find or not find
ronald ronald

2019/7/3

#
Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. what should I do ? Thank you for your help
danpost danpost

2019/7/3

#
ronald wrote...
Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. what should I do ? Thank you for your help
Need complete error message (along with code trace) and relevant codes.
ronald ronald

2019/7/3

#
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:713) at greenfoot.Actor.getX(Actor.java:165) at Fire.act(Fire.java:40) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
public class Fire extends Actor
{
    /**
     * Act - do whatever the Fire wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private boolean toRemove=false;
    private int vx=3;
    
    
    public void addedToWorld(World MyWorld)
    {
        
    }
    
    
    
    public void act() 
    {
       if(!toRemove)
       {
           setLocation(getX()+vx,getY());
           Actor balloon = getOneIntersectingObject(Balloon.class);
           if(balloon!=null)
           {
            World myWorld = getWorld();
            myWorld.removeObject(balloon);
            myWorld.removeObject(this);
           }
           
           if(getX()>getWorld().getWidth()+200)toRemove=true;
       } 
           else
       {
           getWorld().removeObject(this);
       }
    }
}
I hope it will serve you
ronald ronald

2019/7/3

#
line 40 is line 32, sorry
danpost danpost

2019/7/3

#
If line 29, which removes the actor from the world, is executed, then line 32, which requires the actor be in the world, will fail. Move line 32 to the beginning of the act method.
ronald ronald

2019/7/3

#
thank you
You need to login to post a reply.