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

2013/11/13

Calling method from different class

zombieslayer2977 zombieslayer2977

2013/11/13

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Font;
/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    private boolean running = false;
    private int millisElapsed = 0;
    private long lastTime=0;
    public GreenfootImage image99;
    
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Counter()
    {
        GreenfootImage image99 = getImage();
        image99.scale(image99.getWidth() + 20, image99.getHeight() + 20);
        
        updateImage();
    }
    public void start()
    {
        millisElapsed=0;
        lastTime=0;
    }
    public void act()
    {
      long time = System.currentTimeMillis();
       if(lastTime != 0)
       {
           long diff = time - lastTime;
           millisElapsed += diff;
        }
       lastTime = time;
       updateImage();
    }
    public void updateImage()
    {
        int millis= millisElapsed %1000;
        int secs = (millisElapsed / 1000) %60;
        int mins = millisElapsed / 60000;
        String millisText = String.format("%03d", millis);
        String secsText = String.format("%02d", secs);
        String minsText = "" + mins;
        String text = minsText + ":" + secsText + "." + millisText;
        
        GreenfootImage img = new GreenfootImage(text, 25, null, null);
        setImage(img);
        
}
    public void changeCounterSize()
    {    
       
       setImage(image99);
    }
    
}
I then have this
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import greenfoot.World;
import greenfoot.Actor;
    import java.util.List;  
/**
 * Write a description of class Prey here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Prey extends Actor
{

    /**
     * Act - do whatever the Prey wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
     turnDirection();
     checkCollision();
    }
    public void turnDirection()
    {
        int speed = 5;
        if(Greenfoot.isKeyDown("left"))
        {
            this.turn(-5);
        }
        if(Greenfoot.isKeyDown("right"))
        {
            this.turn(5);
        }
        if(Greenfoot.isKeyDown("up"))
        {
            move(speed);
        }
    }
    public boolean collision()
    {
        Actor predator = getOneIntersectingObject(Predator.class);
        
        return predator != null;
    }
     
    public void checkCollision()
    {
        if(collision())
        {
           Background background = (Background) getWorld();
           
           Counter.changeCounterSize();
           background.endGame();
           
            
        }
    }
}

When i try to compile, at Counter.changeCounterSize() it comes with a
zombieslayer2977 zombieslayer2977

2013/11/13

#
It comes with a non static method cannot be referenced from a static context
Game/maniac Game/maniac

2013/11/14

#
to make the method refrencable you need to make it a static method, e.g. public static void aMethod() {}
danpost danpost

2013/11/14

#
Game/maniac wrote...
to make the method refrencable you need to make it a static method, e.g. public static void aMethod() {}
This may make the method accessible by allowing you to use the class name to call the method on; however, you need an Counter object to call the 'changeCounterSize' method on, not a class. The object can be saved in an instance field in your world class and obtained by a 'get' method:
// instance field in world class
Counter scoreCounter; // its value set here or in constructor
// the 'get' method
public Counter getScoreCounter()
{
    return scoreCounter;
}
//in your actor class
((/* WorldName */)getWorld()).getScoreCounter().changeCounterSize();
Or, if you are only using one Counter object in your world, you can access it in your actor class with the following statement:
Counter counter = (Counter)getWorld().getObjects(Counter.class).get(0);
counter.changeCounterSize();
You need to login to post a reply.