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

2018/6/6

incompatible types: java.lang.Class<lifeboard> cannot be converted into greenfoot.Actor

1
2
3
4
5
danpost danpost

2018/6/6

#
ogy2014 wrote...
so what type of code should i use
What do you have so far for a UFO class?
ogy2014 ogy2014

2018/6/6

#
i have a pic and this is all the code i have
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class UFO here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class UFO extends Actor
{
    /**
     * Act - do whatever the UFO wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(Greenfoot.getRandomNumber(5) == 0)
        {
            getWorld().addObject(new UFO(), 1, 70);
        }
        
        if(getX() < 799)
        {
            move(5);
        }
        else
        {
            getWorld().removeObject(this);
        }
    
        Actor playermissile;
        
        Space space = (Space) getWorld();
        Scoreboard sBoard = space.scoreboard;
        
        playermissile = getOneIntersectingObject(PlayerMissile.class);
        if(playermissile != null)
        {
            space.scoreboard.add(50);
            getWorld().removeObject(playermissile);
            getWorld().removeObject(this);
        }
    }    
}
danpost danpost

2018/6/6

#
Remove line 17 through 2;0 -- a UFO object is not supposed to add another UFO object into your world. You can simplify lines 31 through 42 by replacing it with the following:
Actor playermissile = getOneIntersectingObject(PlayerMissile.class);
if (playermissile != null)
{ 
    Space space = (Space) getWorld();
    space.scoreboard.add(50);
    space.removeObject(playermissile);
    space.removeObject(this);
}
ogy2014 ogy2014

2018/6/6

#
but how will the ufo randomly appear or on a timer
danpost danpost

2018/6/6

#
ogy2014 wrote...
but how will the ufo randomly appear or on a timer
Use your Space class act method with a timer field.
ogy2014 ogy2014

2018/6/6

#
how do i do that?
danpost danpost

2018/6/6

#
ogy2014 wrote...
how do i do that?
Sort of like lines 15 through 20 above; but without getWorld().
ogy2014 ogy2014

2018/6/6

#
ok will do
ogy2014 ogy2014

2018/6/6

#
whenever the ufo goes to the edge of the screen the game freezes
ogy2014 ogy2014

2018/6/6

#
its saying the problem is with the first line
        Actor playermissile = getOneIntersectingObject(PlayerMissile.class);
        if (playermissile != null)
        { 
            Space space = (Space) getWorld();
            space.scoreboard.add(50);
            space.removeObject(playermissile);
            space.removeObject(this);
        }
ogy2014 ogy2014

2018/6/6

#
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.
ogy2014 ogy2014

2018/6/6

#
r u there
ogy2014 ogy2014

2018/6/6

#
if the ufo doesnt appear at the start of the game it doesnt appear again at all
Super_Hippo Super_Hippo

2018/6/6

#
Move that code to after line 24 into that if-block. Show what you have in your Space class to add UFOs.
ogy2014 ogy2014

2018/6/6

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

/**
 * Write a description of class Space here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Space extends World
{
    /**
     * Constructor for objects of class Space.
     * 
     */
    public Space()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        addObject (lifeboard, 500, 20);
        addObject (new Player(), 400, 550);
        addObject (new Invaders(), 0, 0);
        addObject (scoreboard, 100, 20);
        setPaintOrder(Lifeboard.class,Player.class,Scoreboard.class);
        if(Greenfoot.getRandomNumber(5) == 1)
        {
            addObject(new UFO(), 1, 70);
        }
    }
    public Scoreboard scoreboard = new Scoreboard();
    public Lifeboard lifeboard = new Lifeboard();
}
There are more replies on the next page.
1
2
3
4
5