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

2021/6/22

STAR WARS

ronald ronald

2021/6/22

#
I just published my source code I do not understand the Java Lang Runtime JavaScript error Thank you for your help
ronald ronald

2021/6/22

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

/**
 * Write a description of class Smiley here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tie extends Actor
{
    private static final int SPEED = 2;
    private int deltaX;
    private int deltaY;
    
    private DeathStar home;
    
    public Tie(DeathStar home)
    {
        deltaX = 0;
        deltaY = 0;        
    }
    
    public void act()
    {
        hitFire();
        randomFly();
    }
    
    public void hitFire()
    {
        Actor laser = getOneIntersectingObject(Laser.class);
        if (laser != null)
        {
            getWorld().removeObject(this);
        }
        else if (isAtEdge() != false)
        {
            getWorld().removeObject(this);
        }
    }
    
    public void randomFly()
    {
        if (randomChance(50))
        {
            deltaX = adjustSpeed(deltaX);
            deltaY = adjustSpeed(deltaY);
        }
        fly();
    }
            
    public void fly()
    {
        setLocation(getY() + deltaX, getY() + deltaY);
    }
    
    private int adjustSpeed(int speed)
    {
        speed = speed + Greenfoot.getRandomNumber(2*SPEED - 1) - SPEED + 1;
        return capSpeed(speed);
    }
    
    private int capSpeed(int speed)
    {
        if (speed < -SPEED)
            return -SPEED;
        else if (speed > SPEED)
            return SPEED;
        else
            return speed;
    }
    
    private boolean randomChance(int percent)
    {
        return Greenfoot.getRandomNumber(100) < percent;
    }
    
}
I base myself on the scenario of ants I do not understand this error well thank you for correcting me java.lang.IllegalStateException: Actor has been removed from the world. at greenfoot.Actor.failIfNotInWorld(Actor.java:722) at greenfoot.Actor.getY(Actor.java:182) at Tie.fly(Tie.java:54) at Tie.randomFly(Tie.java:49) at Tie.act(Tie.java:26) 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) Caused by: greenfoot.ActorRemovedFromWorld at greenfoot.World.removeObject(World.java:466) at Tie.hitFire(Tie.java:38) at Tie.act(Tie.java:25) ... 4 more
danpost danpost

2021/6/22

#
Switch lines 25 and 26. That way, you don't try to fly after actor is removed from laser hit.
ronald ronald

2021/6/22

#
thank you
ronald ronald

2021/6/25

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

/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    private int value = 0;
    private String text;
    
    public Counter()
    {
        this("");    
    }
    
    public Counter(String prefix)
    {
        text = prefix;
        int imageWidth = (text.length() + 2) * 10;
        setImage(new GreenfootImage(imageWidth, 16));
        updateImage();       
    }
    
    public void increment()
    {
        value++;
        updateImage();
    }
    
    public void updateImage()
    {
        GreenfootImage image = getImage();
        image.clear();
        image.drawString(text + value, 1, 12);
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class DeathStar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DeathStar extends Actor
{
    private int ties = 0;
    private int maxTies = 50;
    
    private int imperials = 0;
    private int maxImperials = 10;
    
    private Counter tieCounter;
    private Counter imperialCounter;
    
    public DeathStar()
    {
        
    }
    
    public DeathStar(int numberOfTies, int numberOfImperials)
    {
        maxTies = numberOfTies;
        maxImperials = numberOfImperials;
    }
    
    public void act()
    {
        if (ties < maxTies)
        {
            if (Greenfoot.getRandomNumber(100) < 10)
            {
                getWorld().addObject(new Tie(this), getX(), getY());
                ties++;
            }
        }
        
        if (imperials < maxImperials)
        {
            if (Greenfoot.getRandomNumber(100) < 10)
            {
                getWorld().addObject(new Imperial(this), getX(), getY());
                imperials++;
            }
        }
    }
    
    public void countTie()
    {
        if (tieCounter == null)
        {
            tieCounter = new Counter("Tie : ");
            int x = getX();
            int y = getY() + getImage().getWidth()/2 + 8;
            getWorld().addObject(tieCounter, x, y);
        }
        tieCounter.increment();
    }
    
    public void countImperial()
    {
        if (imperialCounter == null)
        {
            imperialCounter = new Counter("Imperial : ");
            int x = getX();
            int y = getY() + getImage().getWidth()/2 + 16;
            getWorld().addObject(imperialCounter, x, y);
        }
        imperialCounter.increment();
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Smiley here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tie extends Actor
{
    private static final int SPEED = 2;
    private int deltaX;
    private int deltaY;
    
    private DeathStar home;
    
    public Tie(DeathStar home)
    {
        deltaX = 0;
        deltaY = 0;
    }
    
    public void act()
    {
        randomFly();
        hitFire();
    }
    
    public void hitFire()
    {
        Actor laser = getOneIntersectingObject(Laser.class);
        if (laser != null)
        {
            getWorld().removeObject(this);
        }
        else if (isAtEdge() != false)
        {
            getWorld().removeObject(this);
        }
    }
    
    public void randomFly()
    {
        if (randomChance(5))
        {
            deltaX = adjustSpeed(deltaX);
            deltaY = adjustSpeed(deltaY);
        }
        fly();
    }
            
    public void fly()
    {
        setLocation(getX() + deltaX, getY() + deltaY);
    }
    
    private int adjustSpeed(int speed)
    {
        speed = speed + Greenfoot.getRandomNumber(2*SPEED - 1) - SPEED + 1;
        return capSpeed(speed);
    }
    
    private int capSpeed(int speed)
    {
        if (speed < -SPEED)
            return -SPEED;
        else if (speed > SPEED)
            return SPEED;
        else
            return speed;
    }
    
    private boolean randomChance(int percent)
    {
        return Greenfoot.getRandomNumber(100) < percent;
    }   
}
I can not display the counters under Deathstar I know that it is missing in the tie class a method to display the number of destroyed ties I do not know how to do ? Thank you for your help
danpost danpost

2021/6/25

#
Use addedToWorld(World) method.
ronald ronald

2021/6/25

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

/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    private int value = 0;
    private String text;
    
    public Counter()
    {
        this("");    
    }
    
    public Counter(String prefix)
    {
        text = prefix;
        int imageWidth = (text.length() + 2) * 10;
        setImage(new GreenfootImage(imageWidth + 20, 16));
        updateImage();       
    }
    
    public void increment()
    {
        value++;
        updateImage();
    }
    
    public void updateImage()
    {
        GreenfootImage image = getImage();
        image.clear();
        image.setColor(Color.WHITE);
        Font font = image.getFont().deriveFont(30);
        image.setFont(font);
        image.drawString(text + value, 1, 12);
    }
}
hi I can not change the thickness of the text I add derivefont to change the size of the characters But the text is cut in two Thank you for your help
Super_Hippo Super_Hippo

2021/6/25

#
In line 23, you create an image with an height of 16. A string with a height of 30 won’t fit onto the image. In line 40, you draw the string with the bottom left corner at 12. So only the 12 bottom part of the string show up because the rest is outside the image.
ronald ronald

2021/6/26

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

/**
 * Write a description of class Missile here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class FalconLaser extends Actor
{
    /**
     * Act - do whatever the Missile wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public Counter tieCounter;
    public Counter imperialCounter;
    
    public FalconLaser()
    {
        setRotation(270);
        tieCounter = new Counter("Ties : ");
        imperialCounter = new Counter("Imperials : ");
    }
    
    protected void addedToWorld(World world)
    {
        world.addObject(tieCounter, 150, 150);
        world.addObject(imperialCounter, 150, 200);
    }
    
    public void act()
    {
        move(5);
        destroyTie();
        destroyImperial();
    }
    
    public void destroyTie()
    {
        Tie tie = (Tie) getOneIntersectingObject(Tie.class);
        if (tie != null)
        {
            getWorld().removeObject(tie);
            getWorld().removeObject(this);
            tieCounter.increment();
        }       
    }
    
    public void destroyImperial()
    {
        Imperial imperial = (Imperial) getOneIntersectingObject(Imperial.class);
        if (imperial != null)
        {
            getWorld().removeObject(imperial);
            getWorld().removeObject(this);
            imperialCounter.increment();
        }       
    }
}
java.lang.IllegalStateException: Actor has been removed from the world. at greenfoot.Actor.failIfNotInWorld(Actor.java:722) at greenfoot.Actor.getOneIntersectingObject(Actor.java:973) at FalconLaser.destroyImperial(FalconLaser.java:52) at FalconLaser.act(FalconLaser.java:36) 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) Caused by: greenfoot.ActorRemovedFromWorld at greenfoot.World.removeObject(World.java:466) at FalconLaser.destroyTie(FalconLaser.java:45) at FalconLaser.act(FalconLaser.java:35) ... 4 more thank you hippo I have both counters that block Zero or 1 and I have these error messages again Thank you for your help
danpost danpost

2021/6/26

#
Any time you make a call from act that (1) could potentially remove the actor from the world and (2) has code beyond the call that requires the actor to be in the world, you should do something like placing the following line after the call:
if (getWorld() == null) return;
You need to login to post a reply.