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

2017/3/31

Need Help with annoying error

thecooldude0517 thecooldude0517

2017/3/31

#
Im just starting to learn java and greenfoot. I made 1 game with alot of help and now Im making pong. I keep getting "non static variable cannot be referenced from a static context greenfoot"(Replace variable with whatever the varible name is. ) Here is my Background code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Background extends World
{
    StartBtn btn;
    
    Player_1_Score score1Obj = null; //unnecessary initialization to avoid errors
    Player_2_Score score2Obj = null; //unnecessary initialization to avoid errors
    public Background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        
        btn = new StartBtn();
        addObject(btn,getWidth()/2,getHeight()/2);        
    }
    public void act() {
        if (Greenfoot.mousePressed(btn)){
            removeObject(btn);
            
            Paddle_1 Player1 = new Paddle_1();
            addObject(Player1,getWidth()/9,getHeight()/2);
            
            Paddle_2 Player2 = new Paddle_2();
            addObject(Player2,getWidth()-(getWidth()/9),getHeight()/2);
            
            Ball Ball = new Ball();
            addObject(Ball, getWidth()/2, getHeight()/2);
            
            score1Obj = new Player_1_Score();
            addObject(score1Obj, getWidth()/4, getHeight()/4);
            
            score2Obj = new Player_2_Score();
            addObject(score2Obj, getWidth()-(getWidth()/15), getHeight()/4);
        }
        score1Obj.set1Score(Ball.score1);
        score2Obj.set2Score(Ball.score2);
    }
}
Here is my ball code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball extends Actor
{
    int speed = 5;
    
    int yChange = 0;
    
    int maxY = 385;
    int minY = 15;
    
    int score1 = 0;
    int score2 = 0;
    
    int maxX = 585;
    int minX = 15;
    public void act() 
    {
        setLocation(getX() + speed, getY() + yChange);
        checkCollision();
        topBounce();
    }
    public void checkCollision()
    {
        Actor Paddle_1 = getOneIntersectingObject(Paddle_1.class);
        Actor Paddle_2 = getOneIntersectingObject(Paddle_2.class);
        if(Paddle_1 != null || Paddle_2 != null)
        {
            speed = speed * -1;
            yChange = getRandomNumber(-2, 2);
            yChange = yChange * -1;
        }
    }
    public int getRandomNumber(int start,int end)
    {
           int normal = Greenfoot.getRandomNumber(end-start+1);
           return normal+start;
    }
    public void topBounce() {
        if (getY()>maxY) {
            yChange = yChange * -1;
        }
        if (getY()<minY) {
            yChange = yChange * -1;
        }
    }
    public void sideEnd() {
        if (getX()<maxX) {
            score2++;
        }
        if (getX()>minX) {
            score1++;
        }
    }
}
And her is my Player_1_Score code and Player_2_Score code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player_1_Score here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player_1_Score extends Actor
{
   public Player_1_Score(){
       Font f = new Font("Raleway", true, false, 50);
       GreenfootImage newImage = new GreenfootImage(250,100);
       newImage.setColor(Color.WHITE);
       newImage.setFont(f);
       setImage(newImage);
   }
   //this method is used by the background class to refresh   //the score every time the timer is a multiple of some value
   public void set1Score(int score1){
       GreenfootImage newImage = getImage();
       newImage.clear();
       newImage.drawString("" + score1, 50,50);
       setImage(newImage);
    }   
}

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player_2_Score here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player_2_Score extends Actor
{
   public Player_2_Score(){
       Font f = new Font("Raleway", true, false, 50);
       GreenfootImage newImage = new GreenfootImage(250,100);
       newImage.setColor(Color.WHITE);
       newImage.setFont(f);
       setImage(newImage);
   }
   //this method is used by the background class to refresh   //the score every time the timer is a multiple of some value
   public void set2Score(int score2){
       GreenfootImage newImage = getImage();
       newImage.clear();
       newImage.drawString("" + score2, 50,50);
       setImage(newImage);
    }   
}
Nosson1459 Nosson1459

2017/3/31

#
What you basically have to do is have all your variables start with a lowercase letter, then when you do ball.score1 it's talking about a specific instance of ball. Doing a capital letter is talking about that class which gives you that error since the coding in a class doesn't change it's static. It would help if you said where you got the error instead of giving so much code to look through (which might be needed but I don't know that yet).
thecooldude0517 thecooldude0517

2017/3/31

#
I dont really understand what you mean but the error is in background and its these lines
score1Obj.set1Score(Ball.score1);
score2Obj.set2Score(Ball.score2);
The is error is this btw "non static variable score1 cannot be referenced from a static context"
Nosson1459 Nosson1459

2017/3/31

#
thecooldude0517 wrote...
I dont really understand what you mean but the error is in background and its these lines <Code Omitted> The is error is this btw "non static variable score1 cannot be referenced from a static context"
I understood the error it's just that I had to find it by myself, but thanks for mentioning it anyway. What you have to do is:
// instance field
Ball ball = new Ball();

//  lines 27 - 34
Paddle_1 player1 = new Paddle_1();
addObject(player1,getWidth()/9,getHeight()/2);
             
Paddle_2 player2 = new Paddle_2();
addObject(player2,getWidth()-(getWidth()/9),getHeight()/2);
             
addObject(ball, getWidth()/2, getHeight()/2);

// lines 42 - 43
score1Obj.set1Score(ball.score1);
score2Obj.set2Score(ball.score2);
thecooldude0517 thecooldude0517

2017/3/31

#
OMG Thank you! Ive been trying to solve this error for like a week now!
You need to login to post a reply.