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

2015/1/12

#
khalid11 wrote...
at rocket.hitenemy(rocket.java:76)
And what is line 76 of your rocket class (in the 'hitenemy' method where you try to cast 'getWorld' to Level1). Just post the 'hitenemy' method.
khalid11 khalid11

2015/1/12

#
    public void hitenemy()
    {
        Actor enemy = getOneIntersectingObject(enemy.class);
        if (enemy!=null)
        {
            World myWorld= getWorld();
            Level1 level1 = (Level1)myWorld;
            HealthBar healthbar = level1.getHealthBar();
            if(touchingenemy == false)
            {
                healthbar.loseHealth();
                touchingenemy = true;

                if(healthbar.health<=0)
                {
                    getWorld().removeObject(this);
                    GameOver gameover = new GameOver();
                    Restart restart = new Restart();
                    myWorld.addObject(gameover, myWorld.getWidth()/2,myWorld.getHeight()/2);
                    myWorld.addObject(restart, myWorld.getWidth()/2,myWorld.getHeight()/2);

                }
            }

        }else {
            touchingenemy = false;
        }          
    }
danpost danpost

2015/1/13

#
It is important to realize that the actor could be in a Level1 world or a Level2 world. Line 7 will fail in the actor is in a Level2 world because you did not ensure that the actor was indeed in a Level1 world before trying to save the world in the 'level1' field. Now, the only reason for needing the specific world in the entire method is to call the 'getHealthBar' method in the correct world (or to retrieve the current HealthBar object). All other method calls on the world object can be done directly on the World object 'getWorld' returns. Therefore, set up the variable to hold a HealthBar object, determine the subclass of World the world is, and assign the 'healthbar' variable the appropriate object:
HealthBar healthbar = null;
if (myWorld instanceof Level1) healthbar = ((Level1) myWorld).getHealthBar();
else if (myWorld instanceof Level2) healthbar = ((Level2) myWorld).getHealthBar();
These lines should replace lines 7 and 8 above.
khalid11 khalid11

2015/1/13

#
new error cannot find symbol method getHealthBar()
danpost danpost

2015/1/13

#
khalid11 wrote...
new error cannot find symbol method getHealthBar()
Copy the method from your Level1 class into your Level2 class. Also, if you have not already declared the 'healthbar' field in your Level2 world, you need to do that and ensure it is assigned properly within the constructor (or prepare method).
khalid11 khalid11

2015/1/13

#
the getHealthbar method was sorted but i still need to sort out my bullet error from the other discussion
khalid11 khalid11

2015/1/13

#
if it helps ive noticed in the other discussion their error only appears once i hit an enemy in level2
You need to login to post a reply.
1
2
3