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

2018/6/6

#
i am doing a school project where i make a working copy of space invaders and at the moment i am ahead of most people but this one piece of code is still bugging me. What i did was change the print order so that when the lifeboard detected it was at 0 it will not has a bunch of other things on the gameover screen the problem is now i have implimented code so that when a invader touches the player it show a gameover so my lifeboard shows over it because it is at the front of the print order. i am very new to this but i think i need to either change the print order mid game or remove the lifeboard at the gameover screen and the latter is what i have being attempting. please help :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Actor invader;
invader = getOneIntersectingObject(Invader.class);
if(invader != null)
{
    Space space = (Space) getWorld();
    this.setImage(explode);
    Greenfoot.delay(100);
    this.setImage(player);
    setLocation(400,300);
    getWorld().removeObject(Lifeboard.class); //this is the problematic line specifically the '.class' part
    setImage("gameover.jpg");         
    Greenfoot.stop();
    return;
}
ogy2014 ogy2014

2018/6/6

#
i know the delay thingy isn't optimal but i don't really care.
danpost danpost

2018/6/6

#
ogy2014 wrote...
What i did was change the print order so that when the lifeboard detected it was at 0 it will not has a bunch of other things on the gameover screen the problem is now i have implimented code so that when a invader touches the player it show a gameover so my lifeboard shows over it because it is at the front of the print order. i am very new to this but i think i need to either change the print order mid game or remove the lifeboard at the gameover screen and the latter is what i have being attempting. << Code Omitted >>
Line 10 tries to remove a Class object (which is not a list of Actor objects) from the world. To get a list of Lifeboard object in the world, use the expression (not a statement in itself):
1
getWorld().getObjects(Lifeboard.class)
You can replace the Class object with this expression.
ogy2014 ogy2014

2018/6/6

#
i'm not sure what that means
ogy2014 ogy2014

2018/6/6

#
is this what you mean?
1
getWorld().removeObject(getWorld().getObjects(Lifeboard.class));
danpost danpost

2018/6/6

#
ogy2014 wrote...
is this what you mean?
1
getWorld().removeObject(getWorld().getObjects(Lifeboard.class));
Actually, I meant this:
1
getWorld().removeObjects(getWorld().getObjects(Lifeboard.class));
But, it won't work if you have multiple Lifeboard object in the world and you only want to remove one of them. In that case use this:
1
getWorld().removeObject(getWorld().getObjects(Lifeboard.class).get(0));
You must have at least one in the world for this line to work, however.
ogy2014 ogy2014

2018/6/6

#
oh i figured it out my self i just had to include these lines of code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Actor invader;
invader = getOneIntersectingObject(Invader.class);
if(invader != null)
{
    Space space = (Space) getWorld();
    Lifeboard lBoard = space.lifeboard;
    this.setImage(explode);
    Greenfoot.delay(100);
    this.setImage(player);
    setLocation(400,300);
    getWorld().removeObject(lBoard);
    setImage("gameover.jpg");         
    Greenfoot.stop();
    return;
}
ogy2014 ogy2014

2018/6/6

#
thanks for the help anyway
ogy2014 ogy2014

2018/6/6

#
i have 1 more question what do i need to change to have a UFO class go across the top of the screen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void act()
{
    if(Greenfoot.getRandomNumber(5) < 0)
    {
        getWorld().addObject(new UFO(), 1, 70);
    }
     
    if(getX() < 799)
    {
        move(5);
    }
    else
    {
        getWorld().removeObject(this);
    }
}
i'm probably gonna speed it up
danpost danpost

2018/6/6

#
ogy2014 wrote...
i have 1 more question what do i need to change to have a UFO class go across the top of the screen << Code Omitted >> i'm probably gonna speed it up
What class do you propose this code to be in?
ogy2014 ogy2014

2018/6/6

#
a different class called UFO or am i just getting confused over classes and actor
ogy2014 ogy2014

2018/6/6

#
i am just trying to get the movement sorted for now
ogy2014 ogy2014

2018/6/6

#
and then i will add intersecting object code with the player missile
danpost danpost

2018/6/6

#
ogy2014 wrote...
a different class called UFO or am i just getting confused over classes and actor
Well, its not that. I saw code that adds a UFO object into the world (from an Actor subclass) with code that moves the object that added the UFO object into the world. The movement code certainly cannot be for the UFO that was added into the world unless one UFO object adds another one into the world. When coding and implementing a new type object, do the following steps: * create a class for the object; * add any action code to the class (things that a UFO object does -- move missile-detection and at-edge removal) * then, determine what object should be adding that type object into the world (in this case, your World object would be the logical choice; also, it would have to be at some regular interval of time or at some random time with a minimum delay) * add to that determined object's actions to create and add a new type object into the world; You would choose the world because it is a basic game play behavior for a UFO to appear at some interval of time. It would not be for the player's object or an alien (or another UFO object) to do so.
ogy2014 ogy2014

2018/6/6

#
so what type of code should i use
There are more replies on the next page.
1
2
3
4