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

2014/7/17

How do I delete one of many actors?

1
2
3
danpost danpost

2014/7/19

#
Evil_Lan wrote...
< Quote Omitted > I have a Score.class which adds 1 point every time I remove a dot. And I will probably add more actors into my game will this be an issue? < Code Omitted > I replaced the 'null' by Score.class but it just stops the game when I press Right/Left < Code Omitted >
Like I mentioned in that same post you quoted:
If you have other objects in the world, then if you have an intermediate class between Actor and both your RedDot and BlueDot classes (maybe called 'Dot'), you can replace 'null' with 'Dot.class' in the 'getObjects' calls and have it work.
NikZ NikZ

2014/7/19

#
danpost wrote...
NikZ wrote...
You can also use removeTouching()
There is no 'touching' going on here. 'removeTouching' has no place within the required code.
Evil_Lan wrote...
< Quote Omitted > Okay, this is weird... I did what you said: < Code Omitted > Ok so it works great and all, it removes only one of the falling dots and if the player presses the wrong key it displays 'game over' and stops the game. But the thing is in the Greenfoot Terminal Window, "GAME OVER! Wrong KEY PRESSED!" appears even though the correct arrow key is pressed and does not stop the game. It only prints "GAME OVER! Wrong KEY PRESSED!". Why?
Just to clear things up -- 'if', 'while', 'do', 'for' or 'else' statements are expected to have something executable after them. It can be a single statement (not enclosed in curly brackets) or a block (enclosed in curly brackets). Whichever it is, it is the first thing after the 'if', 'while', 'do' 'for' or 'else' statement. Line 10 is the second thing after the 'else' statement and therefore is not considered within the 'if-else' part of the code and, hence, executed only on the condition of line 2 (line 5 has no bearing on its execution). As far as a single statement is concerned, a simple semi-colon, ';', concludes an empty statement. That is, with this
if (getX() == 0);
any following code is executed without regard to the condition of the statement.
I know, it is for the other methods.
Evil_Lan Evil_Lan

2014/7/19

#
danpost wrote...
Evil_Lan wrote...
< Quote Omitted > I have a Score.class which adds 1 point every time I remove a dot. And I will probably add more actors into my game will this be an issue? < Code Omitted > I replaced the 'null' by Score.class but it just stops the game when I press Right/Left < Code Omitted >
Like I mentioned in that same post you quoted:
If you have other objects in the world, then if you have an intermediate class between Actor and both your RedDot and BlueDot classes (maybe called 'Dot'), you can replace 'null' with 'Dot.class' in the 'getObjects' calls and have it work.
I don't quite understand what do you mean by 'an intermediate class between Actor and both your RedDot and BlueDot classes'. Do you mean replace the 'null' in 'Object obj = getObjects(null).get(0); ' by the actor's name? Because i've tried that and it didn't work. Isnt there a way to make the BlueDot/RedDot and my other actor (for the score) work independently?
danpost danpost

2014/7/19

#
Create an Actor subclass called 'Dot' and change what 'RedDot' and 'BlueDot' extend to 'Dot' instead of 'Actor'. Then you will be able to 'getObjects(Dot.class)' and get all Dot objects ("RedDot' and 'BlueDot' objects) without getting Score objects.
Evil_Lan Evil_Lan

2014/7/19

#
danpost wrote...
Create an Actor subclass called 'Dot' and change what 'RedDot' and 'BlueDot' extend to 'Dot' instead of 'Actor'. Then you will be able to 'getObjects(Dot.class)' and get all Dot objects ("RedDot' and 'BlueDot' objects) without getting Score objects.
Ohh great thanks! works perfectly! But I have i minor problem, if i press an arrow when there is no dots on the screen the game crashes java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:604) at java.util.ArrayList.get(ArrayList.java:382) at Background.act(Background.java:40) at greenfoot.core.Simulation.actWorld(Simulation.java:574) at greenfoot.core.Simulation.runOneLoop(Simulation.java:509) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) the error is on this line 'Object obj = getObjects(Dot.class).get(0); '
NikZ NikZ

2014/7/19

#
That means there is nothing stored in get(0). You can prevent that by:
if (!getObejcts(Dot.class).isEmpty())
    Object obj = getObjects(Dot.class).get(0); 
This might work--you might need to save the code in the if into a List.
Evil_Lan Evil_Lan

2014/7/19

#
NikZ wrote...
That means there is nothing stored in get(0). You can prevent that by:
if (!getObejcts(Dot.class).isEmpty())
    Object obj = getObjects(Dot.class).get(0); 
This might work--you might need to save the code in the if into a List.
Great thanks it works
You need to login to post a reply.
1
2
3