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

2020/5/21

Cannot find method

neoaspect neoaspect

2020/5/21

#
Hi, for my world currently, I am trying to add a life counter in that decreases whenever the enemy touches the player (Karmen), however there is this error that shows up as "cannot find symbol - method karmenLifeCount()" although I had put it in there ? This is my world code
public class Level_1 extends World
{
    private int score = 0;
    public static Counter karmenLifeCount = new Counter();
    /**
     * Constructor for objects of class Level_1.
     * 
     */
    public Level_1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        karmenLifeCount.setValue(3);
        addObject(karmenLifeCount(), 410, 15);
        prepare();
    }

    private void prepare()
    {
        
        Level_1_Floor level_1_floor = new Level_1_Floor();
        addObject(level_1_floor, 399, 299);
        Karmen karmen = new Karmen();
        addObject(karmen, 100, 533);
        Yoichi yoichi = new Yoichi();
        addObject(yoichi, 140, 538);
        Platform platform = new Platform(150, 80);
        addObject(platform, 353, 469);
        Platform_2 platform_2 = new Platform_2();
        addObject(platform_2, 615, 379);
        addObject(new Platform (150, 80), 453, 278);
        KarmenGem karmengem = new KarmenGem();
        addObject(karmengem, 349, 423);
        EndIsland endisland = new EndIsland(250, 80);
        addObject(endisland, 155, 116);
        EndPortal endportal = new EndPortal(150, 80);
        addObject(endportal, 151, 51);
        ThirdIsland thirdisland = new ThirdIsland();
        addObject(thirdisland, 309, 199);
        YoichiGem yoichigem = new YoichiGem(60,40);
        addObject(yoichigem, 610, 335);
        Enemy1 enemy1 = new Enemy1();
        addObject(enemy1,  289, 329);
        addObject(new Enemy1(), 578, 169);
        Pause pause = new Pause();
        addObject(pause,764, 34);
        addObject(new Level1Music(), 758, 560);
        
        
    }
}
and this is the code for the enemy
public class Enemy1 extends Enemies
{
    int rightSideOfScreen;
    int bottomOfScreen;

    public void addedToWorld(World level1)
    {
        rightSideOfScreen = level1.getWidth() - 1;
        bottomOfScreen = level1.getHeight() - 1;
    }

    /**
     * Act - do whatever the Enemy1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(5);
        moveAround();
        Karmen k =(Karmen) getOneIntersectingObject(Karmen.class);
        if(k !=null)
        {
            Level_1.karmenLifeCount.add(-1);
            if(Level_1.karmenLifeCount.getValue() == 0)
            {
                getWorld().removeObject(k);
            }
        }    
    }

    public void moveAround()
    {

        if(Greenfoot.getRandomNumber(20)==1)
        {
            setRotation(Greenfoot.getRandomNumber(360));
        }

        int x = getX();
        int y = getY();
        if(x<=0 || y<=0 || x >= rightSideOfScreen || y >= bottomOfScreen)
        {
            turn(180);
        }

    }
danpost danpost

2020/5/21

#
neoaspect wrote...
Hi, for my world currently, I am trying to add a life counter in that decreases whenever the enemy touches the player (Karmen), however there is this error that shows up as "cannot find symbol - method karmenLifeCount()" although I had put it in there ?
You did not (and cannot) name it with parentheses. Remove them.
neoaspect neoaspect

2020/5/21

#
I don't think I named it with parentheses ? I'm not quite sure what you mean
danpost danpost

2020/5/21

#
neoaspect wrote...
I don't think I named it with parentheses ? I'm not quite sure what you mean
Look at line 14 in the Level_1 class code. I see extra parentheses there.
neoaspect neoaspect

2020/5/21

#
Thank you, it works now but however when the enemy touches the player, instead of removing one life, they remove all three lives and the player. How would I fix this?
danpost danpost

2020/5/21

#
neoaspect wrote...
Thank you, it works now but however when the enemy touches the player, instead of removing one life, they remove all three lives and the player. How would I fix this?
Add a boolean to the enemy class to track touching player. That way you can detect when it first touches the player (as opposed to detecting the current state of touching player). When the boolean and the current state do not match, then the state just then changed. If at the time the new state is the touching state, you can lose a player life. As an alternative, you could use an int timer field so that you can create a delay time between dealing consecutive blows. That will force the player to get away from the enemy (instead of chumming up with it to avoid losing more lives).
neoaspect neoaspect

2020/5/21

#
I'm awfully new at this, would it be okay if you showed an example ?
danpost danpost

2020/5/21

#
neoaspect wrote...
I'm awfully new at this, would it be okay if you showed an example ?
In general: Using a boolean (does something once on a particular change in state): // field private boolean trackedState; // in act if (trackedState != << current state >>) { trackedState = ! trackedState; if (trackedState == true) { << do something >>; } } Using an int timer: // field private int actionTimer; // in act if (actionTimer == 0) { if (<< some conditions >>) { << perform action >>; actionTimer = << some positive value >>; } } else actionTimer--;
neoaspect neoaspect

2020/5/21

#
Sorry I can't seem to wrap my head around this, I understood the timer part, but not particularly what to put in the 'trackedState' parts
danpost danpost

2020/5/21

#
neoaspect wrote...
Sorry I can't seem to wrap my head around this, I understood the timer part, but not particularly what to put in the 'trackedState' parts
The << current state >> would be the isTouching condition. The << perform action >> would be the player losing a life.
You need to login to post a reply.