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

2016/9/5

Beam Light Problems

1
2
3
Laurence Laurence

2016/9/5

#
I've got a problem with using the 'Beam' Class. The beam is meant to turn wherever the actor is facing but I'm not getting this result.
public class Lamp extends Actor
{
    int dir = 0;
    /**
     * Act - do whatever the Lamp wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       setRotation(90);
    }    
}
This is the Lamp Code (Simple enough).
public class Level1 extends World
{
    GifImage gif = new GifImage("Level1.gif");
    boolean colored = false;
    int size;
    GreenfootImage darkness;
    /**
     * Constructor for objects of class Level1.
     * 
     */
    public Level1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
      
 
    }
    public void act()
    {
        setBackground(gif.getCurrentImage());
        if(getObjects(Beam.class).isEmpty()) return;
        Beam beam=(Beam)getObjects(Beam.class).get(0);
   
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Wreckage wreckage = new Wreckage();
        addObject(wreckage,350,295);
        wreckage.setLocation(323,279);
        Lamp lamp = new Lamp(); 
    }
}
This is the World as a whole's code. (In short, the World Code spawns the Beam and Actor, what I presume, correctly).
danpost danpost

2016/9/5

#
It does not appear that you followed the directions in using the Beam class. Also, I do not see where you are even adding a Beam object into the world in your Level1 world class code (or anywhere else).
Laurence Laurence

2016/9/5

#
Ah, for some reason I removed that code (probably trying to find a fix), allow me to quickly post in the full version.
Laurence Laurence

2016/9/5

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

/**
 * Write a description of class Level1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Level1 extends World
{
    GifImage gif = new GifImage("Level1.gif");
    boolean colored = false;
    int size;
    GreenfootImage darkness;
    /**
     * Constructor for objects of class Level1.
     * 
     */
    public Level1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
        setPaintOrder(Lamp.class, Beam.class);
        Lamp lamp = new Lamp(); 
        addObject(lamp, 100, 200);
        addObject(new Beam(lamp, 40), 100, 200);
 
    }
    public void act()
    {
        setBackground(gif.getCurrentImage());
        if(getObjects(Beam.class).isEmpty()) return;
        Beam beam=(Beam)getObjects(Beam.class).get(0);
   
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Wreckage wreckage = new Wreckage();
        addObject(wreckage,350,295);
        wreckage.setLocation(323,279);
        Lamp lamp = new Lamp(); 
    }
}
danpost danpost

2016/9/5

#
Why are you (repeatedly) setting the rotation of the Lamp object in its act method? Also, now you have two different Lamp objects being added to the world -- one with and one without a Beam object. There are other things in your Level1 class that do not make sense as well (like lines 33 and 34 of the act method). The following are simplified versions of your two classes: The Level1 class:
import greenfoot.*; 

public class Level1 extends World
{
    GifImage gif = new GifImage("Level1.gif");
    boolean colored = false;
    int size;
    GreenfootImage darkness;

    public Level1()
    {
        super(600, 400, 1); 
        setPaintOrder(Lamp.class, Beam.class);
        prepare();
    }

    public void act()
    {
        setBackground(gif.getCurrentImage());    
    }

    private void prepare()
    {
        Wreckage wreckage = new Wreckage();
        addObject(wreckage,323,279);
        Lamp lamp = new Lamp(); 
        addObject(lamp, 100, 200);
        lamp.setRotation(90);
        addObject(new Beam(lamp, 40), 100, 200);
    }
}
Notice the setting of the rotation of the lamp in the world class code. and the Lamp class (the entire class):
public class Lamp extends greenfoot.Actor {}
danpost danpost

2016/9/5

#
I could just have well have removed lines 6 through 8 (in my revised code) as none of those fields are currently being used in the code.
danpost danpost

2016/9/5

#
You may have to expand on my line 29 to this:
Beam beam = new Beam(lamp, 40);
addObject(beam, 100, 200);
beam.act();
That should fix the rotation proper before any act cycles are executed.
Laurence Laurence

2016/9/5

#
Alrighty, thank ya'!
Laurence Laurence

2016/9/5

#
at greenfoot.core.Simulation.runContent(Simulation.java:212) at greenfoot.core.Simulation.run(Simulation.java:205) 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:695) at greenfoot.Actor.getX(Actor.java:157) at Beam.act(Beam.java:85) at Level1.prepare(Level1.java:91) at Level1.<init>(Level1.java:14) This appears on loading due to it.
danpost danpost

2016/9/5

#
Laurence wrote...
< Error Trace Omitted > This appears on loading due to it.
Please show your modified world class code (I should not have to ask every time).
Laurence Laurence

2016/9/5

#
Apologies, just thought you'd know what it would be considering you gave it to me.
import greenfoot.*; 
 
public class Level1 extends World
{
    GifImage gif = new GifImage("Level1.gif");
    boolean colored = false;
    int size;
    GreenfootImage darkness;
 
    public Level1()
    {
        super(600, 400, 1); 
        setPaintOrder(Lamp.class, Beam.class);
        prepare();
    }
 
    public void act()
    {
        setBackground(gif.getCurrentImage());    
    }
    private void prepare()
    {
        Wreckage wreckage = new Wreckage();
        addObject(wreckage,350,295);
        wreckage.setLocation(323,279);
        Lamp lamp = new Lamp(); 
        CrashedShip crashedship = new CrashedShip();
        addObject(crashedship,142,246);
        crashedship.setLocation(34,377);
        crashedship.setLocation(23,379);
        Wau2 wau2 = new Wau2();
        addObject(wau2,181,263);
        wau2.setLocation(29,382);
        Wau2 wau22 = new Wau2();
        addObject(wau22,145,285);
        wau22.setLocation(14,115);
        wau22.setLocation(18,116);
        Wau2 wau23 = new Wau2();
        addObject(wau23,279,192);
        wau23.setLocation(29,230);
        wau23.setLocation(29,230);
        wau23.setLocation(29,230);
        wau23.setLocation(29,230);
        wau23.setLocation(29,230);
        wau23.setLocation(29,230);
        wau23.setLocation(29,230);
        wau23.setLocation(29,230);
        wau23.setLocation(29,230);
        wau23.setLocation(29,230);
        wau23.setLocation(29,230);
        wau23.setLocation(29,230);
        wau23.setLocation(29,230);
        wau23.setLocation(29,230);
        wau23.setLocation(29,230);
        wau23.setLocation(29,230);
        wau23.setLocation(3,317);
        Wau2 wau24 = new Wau2();
        addObject(wau24,259,265);
        wau24.setLocation(46,236);
        Wau2 wau25 = new Wau2();
        addObject(wau25,170,274);
        wau25.setLocation(31,274);
        Wau2 wau26 = new Wau2();
        addObject(wau26,201,296);
        wau26.setLocation(89,384);
        wau26.setLocation(5,180);
        wau26.setLocation(5,180);
        wau26.setLocation(5,180);
        wau26.setLocation(5,180);
        wau26.setLocation(5,180);
        wau26.setLocation(5,180);
        wau26.setLocation(5,180);
        wau26.setLocation(5,180);
        wau26.setLocation(5,180);
        wau26.setLocation(5,180);
        wau26.setLocation(5,180);
        wau26.setLocation(5,180);
        wau26.setLocation(5,180);
        wau26.setLocation(5,180);
        wau26.setLocation(5,180);
        wau26.setLocation(5,180);
        wau26.setLocation(5,180);
        wau26.setLocation(5,180);
        wau26.setLocation(5,180);
        wau26.setLocation(5,180);
        wau26.setLocation(5,180);
        wau26.setLocation(6,308);
        wau26.setLocation(10,178);
        lamp.setRotation(90);
        addObject(new Beam(lamp, 40), 100, 200);
        Beam beam = new Beam(lamp, 40);
        addObject(beam, 100, 200);
        beam.act();
    }
}
danpost danpost

2016/9/5

#
What happened to the line that adds the Lamp object into the world?
Laurence Laurence

2016/9/5

#
Ah, never mind, I found the source of the issue - simply forgot to input 'lamp.setRotation(90);
Laurence Laurence

2016/9/5

#
  addObject(new Beam(lamp, 40), 100, 200);
This is it, isn't it? If I'm correct, I thought it spawns both of them like a sandwich.
danpost danpost

2016/9/5

#
Laurence wrote...
  addObject(new Beam(lamp, 40), 100, 200);
This is it, isn't it? If I'm correct, I thought it spawns both of them like a sandwich.
You are not correct. That line adds the Beam object into the world -- not the Lamp object.
There are more replies on the next page.
1
2
3