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

2017/6/10

non static method cannot be referenced from a static context java

mrlemon mrlemon

2017/6/10

#
So i'm trying to update my score when an enemy dies in a game but it keeps me giving the error message in de code above. Can someone help me out? Here's the code in my world class:
public void updateScore(int amount)
    {
        score = score + amount;
        showText("Score: " + score, 100, 50);
    }
And this is the code in my enemy class:
private void checkAlive()
    {
        
        if(isAlive=false)
        {
            MyWorld theWorld = (MyWorld) getWorld();
            MyWorld.updateScore(15);
        }
    }
Thanks!
Super_Hippo Super_Hippo

2017/6/10

#
Change 'MyWorld' in line 7 to 'theWorld'.
mrlemon mrlemon

2017/6/10

#
thanks, that helped but now the score is not counting
Super_Hippo Super_Hippo

2017/6/10

#
Do you call the 'checkAlive' method at the end of the act method in the enemy class?
mrlemon mrlemon

2017/6/10

#
yes i do. here's the code for the enemy class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Enemy2 extends Actor
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    private GreenfootImage image4;
    private GreenfootImage image5;
    private GreenfootImage image6;
    private GreenfootImage image7;
    private GreenfootImage image8;
    private GreenfootImage image9;
    private GreenfootImage image10;
    private GreenfootImage image11;
    private GreenfootImage image12;
    private boolean isAlive;
    private boolean facingRight;    
    private int frame = 1;
    private int animationCounter = 0;

    public Enemy2()
    {

        image1 = new GreenfootImage("basicenemystance1.png");
        image2 = new GreenfootImage("basicenemystance2.png");
        image3 = new GreenfootImage("basicenemypreattack1.png");
        image4 = new GreenfootImage("basicenemypreattack2.png");
        image5 = new GreenfootImage("basicenemyattack.png");
        image6 = new GreenfootImage("basicenemydies.png");
        image7 = new GreenfootImage("basicenemystance1left.png");
        image8 = new GreenfootImage("basicenemystance2left.png");
        image9 = new GreenfootImage("basicenemypreattack1left.png");
        image10 = new GreenfootImage("basicenemypreattack2left.png");
        image11 = new GreenfootImage("basicenemyattackleft.png");
        image12 = new GreenfootImage("basicenemydiesleft.png");
        isAlive = true;
    }

    /**
     * Act - do whatever the Enemy2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkDirection();
        move();
        attackLeft();
        attack();   
        die();
        animationCounter++;
        checkAlive();
        
    }    

    private void checkDirection()
    {
        MyWorld theWorld = (MyWorld) getWorld();
        Player player = (Player) theWorld.getPlayer();
        int playerX = player.getX();
        if(getX() <= playerX)
        {
            facingRight = true;
        }
        else
        {
            facingRight = false;
        }
    }

    private void move()
    { 
        MyWorld theWorld = (MyWorld) getWorld();
        Player player = (Player) theWorld.getPlayer();
        int playerX = player.getX();
        if(!isTouching(Player.class) && facingRight == true){   
            if(playerX-getX()>300){
                setLocation(getX()+3, getY());
                if(animationCounter % 8 == 0)
                {
                    animateWalk();
                }
            }
            else{
                setLocation(getX()+3, getY());
                if(animationCounter % 8 == 0)
                {
                    animatePreAttack();
                }

            }            
        }
        if(!isTouching(Player.class) && facingRight == false){   
            if(getX()-playerX>300){
                setLocation(getX()-3, getY());
                if(animationCounter % 8 == 0)
                {
                    animateWalkLeft();
                }
            }
            else{
                setLocation(getX()-3, getY());
                if(animationCounter % 8 == 0)
                {
                    animatePreAttackLeft();
                }

            }

        }
    }
    
    private void attack()
    { 
        if(isTouching(Player.class) && facingRight==true)
        {
            setImage(image5);

        }
    }
    private void attackLeft()
    { 
        if(isTouching(Player.class) && facingRight==false)
        {
            setImage(image11);

        }
    }
    private void die(){
        MyWorld theWorld = (MyWorld) getWorld();
        Player player = (Player) theWorld.getPlayer();
        if (facingRight==true && isTouching(Player.class) &&  (player.getImage().equals(player.image9) || player.getImage().equals(player.image11)
        || player.getImage().equals(player.image15)|| player.getImage().equals(player.image19)|| player.getImage().equals(player.image21))) 
        {
            Greenfoot.playSound("receivehit.wav");
            setImage(image6);
            isAlive = false;
            int x = getX();
            for(int y = getY(); y<=515; y++)
            {
                setLocation(x, y); 
            }
            
            Greenfoot.delay(10);
            getWorld().removeObject(this);             
        }
        
        else if (facingRight==false && isTouching(Player.class) && (player.getImage().equals(player.image10) || player.getImage().equals(player.image12)
            || player.getImage().equals(player.image16)|| player.getImage().equals(player.image20)|| player.getImage().equals(player.image22))) 
        {
            Greenfoot.playSound("receivehit.wav");
            setImage(image12);
            isAlive = false;
            int x = getX();
            for(int y = getY(); y<=515; y++)
            {
                setLocation(x, y); 
            
            }
            Greenfoot.delay(10);
            getWorld().removeObject(this);
        }         
    }
      
    private void animateWalk() //animation for enemy walking right
    {
        if(frame == 1)
        { 
            setImage(image2);

        }
        else if(frame == 2)
        {
            setImage(image1);
            frame = 1;
            return;
        }
        frame++;
    }
    
    private void animateWalkLeft() //animation for enemy walking left
    {
        if(frame == 1)
        { 
            setImage(image8);

        }
        else if(frame == 2)
        {
            setImage(image7);
            frame = 1;
            return;
        }
        frame++;
    }

    private void animatePreAttack() //animation for enemy walking left while near player
    {
        if(frame == 1)
        { 
            setImage(image3);

        }
        else if(frame == 2)
        {
            setImage(image4);
            frame = 1;
            return;
        }
        frame++;
    }
    
    private void animatePreAttackLeft() //animation for enemy walking right while near player
    {
        if(frame == 1)
        { 
            setImage(image9);
        }
        else if(frame == 2)
        {
            setImage(image10);
            frame = 1;
            return;
        }
        frame++;
    }
    private void checkAlive()
    {
        
        if(isAlive=false)
        {
            MyWorld theWorld = (MyWorld) getWorld();
            theWorld.updateScore(15);            
        }
    }

}
Super_Hippo Super_Hippo

2017/6/10

#
Change line 230 to this:
if (isAlive == false) // 2 = instead of 1
or just
if (!isAlive)
mrlemon mrlemon

2017/6/10

#
i tried doing that but than it gives me the next error: java.lang.NullPointerException at Enemy2.checkAlive(Enemy2.java:233) at Enemy2.act(Enemy2.java:52) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
Super_Hippo Super_Hippo

2017/6/10

#
Remove both instances of
getWorld().removeObject(this);
from the class (lines 145 and 161) and add
theWorld.removeObject(this);
after line 233.
mrlemon mrlemon

2017/6/10

#
in that case the counter quickly runs up to a 6 digit number and aftwerwards falls back to zero.
Super_Hippo Super_Hippo

2017/6/10

#
Well, nothing in the code you showed lets the counter "fall back to zero". Did you add the 'theWorld.removeObject(this);'? How many enemies are in the world?
mrlemon mrlemon

2017/6/10

#
the enemies are added randomly to the world in the world class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class MyWorld extends World
{
   public Player thePlayer;
   private int score;   
   private int amount;
   
    public MyWorld()    
    {    
        super(1200, 800, 1, true); //The extra 'true' parameter means that the world is bounded.
        addFloor();          
        score = 0;        
        thePlayer = new Player();
        addObject(thePlayer, 600, 415); //Creates the player. 
        amount = 15;
    }
    
    public void addFloor()
    {
        addObject(new Floor(), 0, 400);        
    }
    
    /**
     * Creates enemies.
     */
    public void act()
    {
        if (Greenfoot.getRandomNumber(200) < 1) //create Enemy2 object at a given x and y coordinate at a chance of 1 in 200
        {
                      
            addObject(new Enemy2(), 1,415);
            
        }
        
        if (Greenfoot.getRandomNumber(200) < 1)
        {
                        
            addObject(new Enemy2(), 1199,415);
            
        }
        
        if (Greenfoot.getRandomNumber(700) < 1 && getObjects(Enemy1.class).size() < 1) //create enemy1 at a given x and y coordinate at a chance of 1 in 700 if there is no other Enemy1 object in the world
        {
                        
            addObject(new Enemy1(), 1,415);
            
        }
        
        if (Greenfoot.getRandomNumber(700) < 1 && getObjects(Enemy1.class).size() < 1)
        {           
            
            addObject(new Enemy1(), 1199,415);
            
        }
        updateScore(score);
    }
    
    public Player getPlayer()
    {
        return thePlayer;        
    }
    
    public void updateScore(int amount)
    {
        score = score + amount;
        showText("Score: " + score, 100, 50);
    }
}
Super_Hippo Super_Hippo

2017/6/10

#
Move line 57 to the constructor line 17. Right now, you are doubling the score every act cycle.
mrlemon mrlemon

2017/6/10

#
Oh waw, I can't beleive i missed that. Guess that is what looking at the same screen for too long leads to :-). Thanks a lot Super_Hippo!!!
You need to login to post a reply.