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

2013/12/11

Cannot find symbol - method getWidth()

shern91 shern91

2013/12/11

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Timer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Timer extends Actor
{
    /**
     * Act - do whatever the Timer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public int time = 20;
    public int count = 65;
    public void act()
    {
        if(time==0)
        {
            addText("Time Up");
            Greenfoot.stop();
            return;
        }
        if(counter())
        {
            time--;
            count =65;
        }
        display();
    }
    
    public boolean counter()
    {
        if(count>0)
        {
            count--;
        }
        return count==0;
    }
    public void display()
    {
        setImage(new GreenfootImage("Time left: "+time,30,Color.RED,Color.BLACK));
    }
    
    public void setTime()
    {
       time = 20;
    }
    
    public boolean isTimeUp()
    {
        return time ==0;
    }
    
     public void addText(String message) {
        addObject(new Message(message), getWidth()/2, getHeight()/2);
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Text here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Message extends Actor
{

    public Message(String message) {
        setImage(new GreenfootImage(message,50, Color.RED, Color.WHITE));
    }
    /**
     * Act - do whatever the Text wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void act() 
    {
        // Add your action code here.
    }
}
i get an error when i compile, do i have to call method getWidth()? how can i call method getWidth
danpost danpost

2013/12/11

#
The Actor class does not have a 'getWidth' or 'getHeight' method. Those methods are found in both the GreenfootImage class to get the dimensions of an object of that class and in the World class to get the dimensions of the viewport of the world. Therefore, those methods must be called on one of those objects (a World object or a GreenfootImage object).
shern91 shern91

2013/12/11

#
danpost wrote...
The Actor class does not have a 'getWidth' or 'getHeight' method. Those methods are found in both the GreenfootImage class to get the dimensions of an object of that class and in the World class to get the dimensions of the viewport of the world. Therefore, those methods must be called on one of those objects (a World object or a GreenfootImage object).
can give me an example?than you very much
danpost danpost

2013/12/11

#
You are coding in a subclass of Actor. There are methods in the Actor class that you can use to get either the world it is in or the image that belongs to it.
shern91 shern91

2013/12/11

#
so i have to create the timer is myworld?
danpost danpost

2013/12/11

#
shern91 wrote...
so i have to create the timer is myworld?
This 'issue' has nothing to do with your error that 'getWidth' method cannot be found. You need to prefix (with dot notation) your calls to 'getWidth' (in your 'isTimeUp' method of your Timer class) with an object of a class that has a 'getWidth' method. There are two Actor class methods that will return objects that will return objects that the method can be used on (one returns a GreenfootImage object and the other a World object). Ask yourself, what object are you trying to get the width of here (in the 'isTimeUp' method). Then ask yourself, what method (or methods) can I use (starting from the Timer class and then its superclasses) to get a reference to that object.
shern91 shern91

2013/12/11

#
danpost wrote...
shern91 wrote...
so i have to create the timer is myworld?
This 'issue' has nothing to do with your error that 'getWidth' method cannot be found. You need to prefix (with dot notation) your calls to 'getWidth' (in your 'isTimeUp' method of your Timer class) with an object of a class that has a 'getWidth' method. There are two Actor class methods that will return objects that will return objects that the method can be used on (one returns a GreenfootImage object and the other a World object). Ask yourself, what object are you trying to get the width of here (in the 'isTimeUp' method). Then ask yourself, what method (or methods) can I use (starting from the Timer class and then its superclasses) to get a reference to that object.
by the way...thank you for reply and teaching...so i wan to call the addText() method when time up
MyWorld my = new MyWorld();            
my.addText("Time Up");
but i get many error
danpost danpost

2013/12/11

#
shern91 wrote...
so i wan to call the addText() method when time up
MyWorld my = new MyWorld();            
my.addText("Time Up");
but i get many error
Again, this is not where your problem currently is located (change that back to what you had above). The problem resides in the 'isTimeUp' method where you are using 'getWidth'.
shern91 shern91

2013/12/11

#
danpost wrote...
shern91 wrote...
so i wan to call the addText() method when time up
MyWorld my = new MyWorld();            
my.addText("Time Up");
but i get many error
Again, this is not where your problem currently is located (change that back to what you had above). The problem resides in the 'isTimeUp' method where you are using 'getWidth'.
then i suppose to in MyWorld class call "isTimeUp" method?
danpost danpost

2013/12/11

#
First, fix your errors (until you are able to compile the project). Then, fix the behavior (so that your project runs the way you want, as far as it can, at that point). Finally, build upon the project (until it is complete). Repeat this process each step of the way. This is just a basic guideline.
danpost danpost

2013/12/11

#
It is not where you are calling 'isTimeUp' from. It is the code within the method that is troublesome. 'getWidth' needs to be applied to an object that you can use the method on. As written, it is equivalent to 'this.getWidth()', where 'this' is a Timer object (an Actor). You cannot use 'getWidth' on a Timer object (or an Actor object). You need to adjust the code within the 'isTimeUp' method so that 'getWidth' has an appropriate object to be executed on (same with the 'addObject' method). 'this', in 'this.getWidth()' needs to be replaced with an appropriate object.
shern91 shern91

2013/12/11

#
danpost wrote...
It is not where you are calling 'isTimeUp' from. It is the code within the method that is troublesome. 'getWidth' needs to be applied to an object that you can use the method on. As written, it is equivalent to 'this.getWidth()', where 'this' is a Timer object (an Actor). You cannot use 'getWidth' on a Timer object (or an Actor object).
Oh,ok..i understand a bit after u explain to me...thank you very much..i try to do first..
shern91 shern91

2013/12/11

#
i would like ask you some question, i got two subclass in actor, one is Message another one is Timer
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Text here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Message extends Actor
{

    public Message(String text) 
    {
       setImage(new GreenfootImage(text,50, Color.RED, Color.WHITE));
    }
    /**
     * Act - do whatever the Text wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void act() 
    {
        // Add your action code here.
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Timer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Timer extends Actor
{
    /**
     * Act - do whatever the Timer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    //Message msg = new Message(msg);
    public int time = 2;
    public int count = 65;
    Message msg;   
    public void act()
    {
        if(time==0)
        {
            gameOver();
            return;
        }
        if(counter())
        {
            time--;
            count =65;
        }
        display();
    }
    
    public boolean counter()
    {
        if(count>0)
        {
            count--;
        }
        return count==0;
    }
    public void display()
    {
        setImage(new GreenfootImage("Time left: "+time,30,Color.RED,Color.BLACK));
    }
    
    public void setTime()
    {
       time = 20;
    }
    
    public boolean isTimeUp()
    {
        return time == 0;
    }
    
    public void gameOver()
    {
       
        if(time==0)
        {
            msg = new Message(text);
            Greenfoot.playSound("whistle.wav");
            Greenfoot.stop();
            msg.addObject(new Message("Time Up") , 300, 300);
        }
    }
}
when i compile always get an error is "Cannot find symbol - variable text" ,i already check in my Message class my the text already exits...what is the problem??
danpost danpost

2013/12/11

#
The Message class is fine. It will receive a String and name it 'text' within the constructor and use that String value in the creation of its image. In the 'gameOver' method of the Timer class, however, the field 'text' is never declared. It is like you are grabbing a rabbit out of a hat (it cannot be done, unless it was there to begin with). In line 65, you are trying to execute 'addObject' into a Message object. Again, the Message class, nor the Actor class (its superclass), has an 'addObject' method and therefore the method cannot be executed on an object of that class. Find the class that does have an 'addObject' method. Hint: what is the object being added to? Run the method on that object.
danpost danpost

2013/12/11

#
You should probably review the Actor class documentation (double click on the 'Actor' icon) or use this link. See what methods are available to use in your Actor subclasses (besides those you may write yourself). Make use of them to get other objects you may want to run methods on.
You need to login to post a reply.