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

2015/1/10

im trying to make a score counter for when the actor kills enemies but doesn't work for some reason

1
2
3
khalid11 khalid11

2015/1/10

#
how should i do it to make sure it works properly
danpost danpost

2015/1/11

#
((Level1)getWorld()).getCounter().addScore();
A problem you will run into is when you go to the next level. You will not be able to cast a Level2 world as a Level1 object. You will need to do something like this;
if (getWorld() instanceof Level1) ((Level1)getWorld()).getCounter().addScore();
else if (getWorld() instanceof Level2) ((Level2)getWorld()).getCounter().addScore();
// etc.
khalid11 khalid11

2015/1/11

#
should it look like this
 public void destroyEnemies()
    { 
        Actor enemy = getOneIntersectingObject(enemy.class);
        if(enemy != null) 
        {
            World myWorld = getWorld();
            getWorld().removeObject(enemy);  
            Level1 level1 = (Level1)myWorld;
            Counter counter = level1.getCounter();
            counter.addScore();
           ((Level1)getWorld()).getCounter().addScore();
            if (getWorld() instanceof Level1) ((Level1)getWorld()).getCounter().addScore();

           else if (getWorld() instanceof Level2) ((Level2)getWorld()).getCounter().addScore();
        }
    }
danpost danpost

2015/1/11

#
khalid11 wrote...
should it look like this
No, you are still trying to set the world to a Level1 field before you know it is one (see lines 6 and 8).
public void destroyEnemies()
{
    if (isTouching(enemy.class))
    {
        removeTouching(enemy.class))
        if (getWorld() instanceof Level1) ((Level1)getWorld()).getCounter().addScore();
    }
}
Last I saw, you did not have a counter in the Level2 world; so I removed line 14 (you can add it back in if you have one there -- or when you do add one there).
khalid11 khalid11

2015/1/11

#
actually i recreated my game using the same codes as well as adding a new working counter and health bar which doesn't work properly
khalid11 khalid11

2015/1/11

#
should i upload my new one
danpost danpost

2015/1/11

#
khalid11 wrote...
should i upload my new one
You can updae the one already up. Just re-share it without changing its name.
khalid11 khalid11

2015/1/11

#
i just uploaded it
danpost danpost

2015/1/11

#
I will get to this shortly (I have already downloaded it).
khalid11 khalid11

2015/1/11

#
thank you ,it would be nice if you can just tell me any ways to fix other errors
danpost danpost

2015/1/11

#
I had posted a fix for your 'destroyEnemies' method above. Why was it not changed??? You need to set the counter in your Level2 class like it is done in the Level1 class (with the declared field and the getter method). Then you can add an 'else if' the the inner block of the destroyEnemies method to add score in the Level2 world. Your counter class needs work. Right now it is a running counter (like its scoring for time alive -- not for kills).
khalid11 khalid11

2015/1/11

#
the running counter i changed once i uploaded it
khalid11 khalid11

2015/1/11

#
the destroyenemies method made a new error
danpost danpost

2015/1/11

#
khalid11 wrote...
the destroyenemies method made a new error
That says a lot !!!
khalid11 khalid11

2015/1/12

#
heres the error

java.lang.ClassCastException: Level2 cannot be cast to Level1
	at rocket.hitenemy(rocket.java:76)
	at rocket.act(rocket.java:22)
There are more replies on the next page.
1
2
3