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

2016/9/16

Player onstantly adding without eating and help with other ideas

1
2
Super_Hippo Super_Hippo

2016/9/23

#
You can add a method in the Enemy class:
public static void increaseSpeed(int x)
{
    speed += x;
}
And then, you can use this in line 5:
Enemy.increaseSpeed(5);
To make it increase every 100 points and not only when it is at 100:
//instead of this:
if (counter.getValue() == 100)

//use this
if (counter.getValue() % 100 == 0)
Boboy18 Boboy18

2016/9/23

#
The method that I put in the Enemy class works but the method that I changed in the player doesn't work. It says "Cannot find symbol - method increaseSpeed(int)". The every 100 points thing as well doesn't work as well. "incompatible types: int cannot be converted to boolean"
Super_Hippo Super_Hippo

2016/9/23

#
Try to compile the Enemy class and then it should be able to find the 'increaseSpeed' method which was added to the Enemy class. I am not sure how the second error happened. Please show how the current class code looks like now.
danpost danpost

2016/9/23

#
Boboy18 wrote...
The method that I put in the Enemy class works but the method that I changed in the player doesn't work. It says "Cannot find symbol - method increaseSpeed(int)". The every 100 points thing as well doesn't work as well. "incompatible types: int cannot be converted to boolean"
If you put the 'increaseSpeed' method in your Enemy class, then you cannot use just:
increaseSpeed(< some value >);
in your Player class. Without an object or class to execute the method on, the compiler will only look in the current class and its superclasses (and any implemented interfaces for those classes). You would be trying to execute the method as if it belonged to the Player object -- not the Enemy class. It is like saying:
this.increaseSpeed(<some value >);
You must say where the method is, or what object you are executing the method on. Because the method is 'static', you just need that class the method is in:
Enemy.increaseSpeed(< some value >);
(non-static method are always executed on an object of a class; static methods do not need an object of the class to execute on).
Boboy18 Boboy18

2016/9/24

#
Super_Hippo wrote...
Try to compile the Enemy class and then it should be able to find the 'increaseSpeed' method which was added to the Enemy class. I am not sure how the second error happened. Please show how the current class code looks like now.
This is the player code.
public class Player extends Characters
{
    private Counter counter;
    
    public Player(Counter pointCounter)
    {
        counter = pointCounter;
    }

    public void act() 
    {
        controls();
        eating();
    }    

    // This is where controls code
    // Wasn't needed

    /**
     * Able to collect the flower and add points to counter 
     */
    public void eating()
    {
        if (isTouching(Flower.class))
       {
            removeTouching(Flower.class);
            counter.add(5);
            createNewFlower();
            if (counter.getValue() >= 1000)
            {
                gameOver();
            }
       }
    }
    
    public void IncreaseSpeed()
    {
        if (counter.getValue() == 100)
            {
                Enemy.increaseSpeed(2);
            }
    }

    /**
     * Spawn another flower, on random location, when collected.
     */
    private void createNewFlower()
    {
        Flower newFlower;
        newFlower = new Flower();

        World world;
        world = getWorld();

        int worldWidth = world.getWidth();
        int worldHeight = world.getHeight();

        int x = Greenfoot.getRandomNumber(worldWidth);
        int y = Greenfoot.getRandomNumber(worldHeight);

        world.addObject(newFlower, x, y);
    }

    /**
     * When touching an obstacle, you'll be prevented going through it. 
     */
    public void setLocation(int x, int y)
    {
        int oldX = getX();
        int oldY = getY();
        super.setLocation(x, y);
        if(!getIntersectingObjects(Obstacle.class).isEmpty())
        {
            super.setLocation(oldX, oldY);
        }
    }

    public void gameOver()
    {
        Greenfoot.stop();
    }
}
And this is the enemy code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Enemy extends Characters
{
    private static int speed = 2;
    
    public static void setSpeed(int spd)
    {
        speed = spd;
    }
    
    
    public void act() 
    {
        move(speed);
        randomTurn();
        turnAtEdge();
        eating();
    }  
    
    public static void IncreaseSpeed(int x)
    {
        speed += x;
    }
    
    public void randomTurn()
    {
        if(Greenfoot.getRandomNumber(100)<10)
        {
            turn(Greenfoot.getRandomNumber(40)-20);
        }
    }
    
    public void turnAtEdge()
    {
        if (atWorldEdge())
        {
            turn(8);
        }
    }
    
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }
    
    public void eating()
    {
       if (isTouching(Player.class))
       {
         removeTouching(Player.class);
         Greenfoot.stop();
       }
    }
}
danpost danpost

2016/9/24

#
Boboy18 wrote...
< Codes Omitted >
I would suggest removing lines 36, 37 and 42 and moving lines 38 through 41 to before line 29 in the Player class. Line 38 should be:
if (counter.getValue()%100 == 0)
Then change line 21 of the Enemy class to this:
public static void increaseSpeed(int x)
Method names, by convention, should begin with a lowercase letter -- just like all non-final field and variable names. Class names should begin with uppercase and final field names should be all uppercase.
Boboy18 Boboy18

2016/9/24

#
Great again it all works. Thanks again. I'm trying to work on 'lives' for the user. I will ask if there is any problems. ;)
You need to login to post a reply.
1
2