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

2020/5/14

I need some help

1
2
Juniper Juniper

2020/5/14

#
I'm not sure if this is an API problem, but whenever I create a temporary object, and that object is removed by clicking, an issue occurs. It disappears; however, the variable I have to check the amount of times the object has been clicked increases by 4 and not 1 because the object is being clicked 4 times in the span it takes to check a click and remove the object.
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 MyWorld extends World
{
    public static block currentBlock;
    public static int Click = 0;
    public static int Miss = 0;
    static int Accuracy = 1;
    static int TotalClicked = 0;
   
    
    
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super (600, 400, 1);
        
       addObject( new block(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(400));
       addObject( new block(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(400));
       addObject( new block(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(400));
       System.out.println("This window will keep record of your clicks and your accuracy");
    }
    public static void calcTotal()
    { 
        TotalClicked = TotalClicked + 1;
        UpdateTotal();
    }
    public static void calcAcc()
    {
        if (((Click + Miss)/4) >= 1)
        {
        Accuracy = (Click / Miss) * 100;
        UpdateAcc();
        }
    }
    public static void UpdateAcc()
    {
         System.out.println(Accuracy);
         System.out.println("^Current Accuracy (%)");
    }
    public static void UpdateTotal()
    {
         System.out.println(TotalClicked);
         System.out.println("^Current Clicks ");
    }
    public void showMiss() 
    {
         System.out.println(Miss);
    }
    public void showHit() 
    {
        System.out.println(Click);
    }
    public void showTotalClicked() 
    {
         System.out.println(TotalClicked);
    }
    public void showAccuracy() 
    {
         System.out.println(Accuracy);
    }
}
MyWorld Class^^^
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class block here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class block extends Actor
{
    static int i = 0;
    static int o = 0;
public void act()
    {
     MyWorld.currentBlock = this;   
     Start();
}
public void Start()
{
    if(Greenfoot.mousePressed(this))
       {      
        if(o == 0)
     {
         getWorld().addObject( new block(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(400));
         getWorld().removeObject(this);
         Hit();
        }
    }
    if(Greenfoot.mousePressed(null))
       {
         Miss();
     }
}

public void Hit()
{
    MyWorld.Click = MyWorld.Click + 1;
    MyWorld.calcTotal();
    MyWorld.calcAcc();
}
public void Miss()
{
   MyWorld.Miss = MyWorld.Miss + 1;
   MyWorld.calcTotal(); 
   MyWorld.calcAcc();
}

/**
 * DEBUGGING TOOL
 * 
 * -A method to probe for the location of loops and multiple occurances-
 * 
 * Place "i = i + 1" to discover the amount of repetitions or areas connected
 * to the forever loop.
 */
public void showI() 
{
    System.out.println(i);
}
}
Block class^^^
Juniper Juniper

2020/5/14

#
Also the Debugging tool has since been removed, It was used to figure out what the issue was, but I have still yet to understand how to fix said issue. The calcAcc() method does not work, I was experimenting how to overcome this issue with a bandaid, but nothing ever worked. I'm new to programming and green foot, so apologies if it looks like terrible coding.
danpost danpost

2020/5/14

#
The main problem is that you are making class fields out of variables that should be instance fields by adding the static keyword to their declaration lines. From what I can tell, you should not be using the static keyword anywhere.
Juniper Juniper

2020/5/15

#
So what should I be using? How would I fix this issue?
danpost danpost

2020/5/15

#
Juniper wrote...
So what should I be using? How would I fix this issue?
Remove all "static" from your code and change all "MyWorld." in block class to "((MyWorld)getWorld())."
Juniper Juniper

2020/5/15

#
danpost wrote...
Juniper wrote...
So what should I be using? How would I fix this issue?
Remove all "static" from your code and change all "MyWorld." in block class to "((MyWorld)getWorld())."
Ok so now I have an issue where all my variables are non-static, but they are “referenced from a static context” in the MyWorld class
danpost danpost

2020/5/15

#
Juniper wrote...
Ok so now I have an issue where all my variables are non-static, but they are “referenced from a static context” in the MyWorld class
Show codes in question.
Juniper Juniper

2020/5/15

#
public static void calcTotal()
    { 
        TotalClicked = TotalClicked + 1;
        UpdateTotal();
    }
    public static void calcAcc()
    {
        if (((Click + Miss)/4) >= 1)
        {
        Accuracy = (Click / Miss) * 100;
        UpdateAcc();
        }
    }
    public static void UpdateAcc()
    {
         System.out.println(Accuracy);
         System.out.println("^Current Accuracy (%)");
    }
    public static void UpdateTotal()
    {
         System.out.println(TotalClicked);
         System.out.println("^Current Clicks ");
Afflicted Variables: TotalClicked Accuracy Click Miss
danpost danpost

2020/5/15

#
Juniper wrote...
<< Code Omitted >> Afflicted Variables: TotalClicked Accuracy Click Miss
I said to remove ALL "static" from your code.
Juniper Juniper

2020/5/15

#
oh I see, It's the methods, it appears I am blind. Sorry about this, I'm extremely new to coding in general. This is my first project. Thanks for the help!
Juniper Juniper

2020/5/15

#
It appears to be having the same issue however.
danpost danpost

2020/5/15

#
Juniper wrote...
It appears to be having the same issue however.
What? Where? Details and code please.
Juniper Juniper

2020/5/15

#
I did a little bit of tidying. Here is the new code, I will give you line numbers Block.class
public class block extends Actor
{
    int i = 0;
    int o = 0;
public void act()
    {
     ((MyWorld)getWorld()).currentBlock = this;   
   if(Greenfoot.mousePressed(this))
       {      
         getWorld().removeObject(this);
         ((MyWorld)getWorld()).newBlock();
         Hit();
        }
   if(Greenfoot.mousePressed(null))
       {
         Miss();
     }
}

public void Hit()
{
    ((MyWorld)getWorld()).Click = ((MyWorld)getWorld()).Click + 1;
    ((MyWorld)getWorld()).calcTotal();
    ((MyWorld)getWorld()).calcAcc();
}
public void Miss()
{
   ((MyWorld)getWorld()).Miss = ((MyWorld)getWorld()).Miss + 1;
   ((MyWorld)getWorld()).calcTotal(); 
   ((MyWorld)getWorld()).calcAcc();
}
MyWorld.class
public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super (600, 400, 1);
       for(int i = 0;i < 3; ++ i)
       {
       newBlock();
    }
       System.out.println("This window will keep record of your clicks and your accuracy");
    }
    public void newBlock()
    {
        addObject( new block(), Greenfoot.getRandomNumber(600), Greenfoot.getRandomNumber(400));
    }
    public void calcTotal()
    { 
        TotalClicked = TotalClicked + 1;
        UpdateTotal();
    }
    public void calcAcc()
    {
        Accuracy = (Click / Miss) * 100;
        UpdateAcc();
        }
    public void UpdateAcc()
    {
         System.out.println(Accuracy);
         System.out.println("^Current Accuracy (%)");
    }
    public void UpdateTotal()
    {
         System.out.println(TotalClicked);
         System.out.println("^Current Clicks ");
    }
    public void showMiss() 
    {
         System.out.println(Miss);
    }
    public void showHit() 
    {
        System.out.println(Click);
    }
    public void showTotalClicked() 
    {
         System.out.println(TotalClicked);
    }
    public void showAccuracy() 
    {
         System.out.println(Accuracy);
    }
}
Line 19 brings a java.lang.NullPointerException So whenever I click a block, it disappears but does not reappear, and whenever I miss once, the click variable increases by 2
danpost danpost

2020/5/15

#
In above block class code, move line 10 down two lines.
Juniper Juniper

2020/5/15

#
It has been done. NullPointerException is now on line 28 (16). I moved line 10 originally to remove the object first before counting the variable because of the issue, it seemed to fix it somewhat, but This NullPointerException seems like it should be on line 22 (12) as well.
There are more replies on the next page.
1
2