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

2014/11/9

How can I call a variable int from an othe class?

largowinch123 largowinch123

2014/11/9

#
Hi, I wrote the same question yesterday, but today is different. Here is the thing: I need that my counter shows the munition of my crab. I think that the counter (it is an object) have to call to the variable int munition in the crab, for to show the crab's munition on the counter. Am I right? How can I do it?.I have tried a lot of times but anything goes well. I NEED IT FOR TOMORROW. PLEASE HELP! PD : Sorry because of my english again. I'm still learning English. I will improve it.
Super_Hippo Super_Hippo

2014/11/9

#
The counter doesn't have to call anything from the crab class. Instead, the crab calls the method to reduce the value of the variable in the counter class when it shoots. If you don't have a reference to the counter class, then it could look like this.
((Counter)getWorld().getObjects(Counter.class).get(0)).addScore(-1);
largowinch123 largowinch123

2014/11/9

#
OK. I don't know if I understand you well. This is my counter's code: public class Counter extends Contador { private static final Color transparent = new Color(0,0,0,0); public GreenfootImage image; public void act() { showMunitionInCounter(); } public Counter() { setImage(new GreenfootImage("Counter.png")); } private void showMunitionInCounter() { GreenfootImage image = new GreenfootImage("Counter.png"); Greenfoot text = new GreenfootImage("50", 22, Color.BLACK, transparent); imagen.drawImage(text, (imagen.getWidth()-text.getWidth()), (imagen.getHeight()-32)); setImage(imagen); } } I want that the counter shows the munition (that is 50) that is in the crab class, and also I want when I shoot the counter decrease. Also I want the counter appears in the begining with the munition. So, for that, I make the method showMunitionInCounter(), and I want that in "Greenfoot text...." appears something like that: Greenfoot text = new GreenfootImage("" + crab.munition, 22, Color.BLACK, transparent); Is this the best form to do that? What do you mean before? Is this right? Thank you!
Super_Hippo Super_Hippo

2014/11/9

#
I am wondering why it says 'imagen' instead of 'image' in the 'showMunitioninCounter' method. What I mean is, take the counter that you can import. Set the start value to 50. It has an act method which is something like "updateImage if target is not equal to the current value". So if you change the target through the 'add' method, it will automatically display the correct value again. You only have to call this method from the crab class (when shooting) to adjust the target variable.
danpost danpost

2014/11/9

#
This line:
Greenfoot text = new GreenfootImage("" + crab.munition, 22, Color.BLACK, transparent);
may work provided that (1) the 'munition' field is 'public'; and (2) 'crab' is defined as a Crab type variable that holds a reference to the crab object in the world. Assuming that there is one, and only one, Crab object in the world at any time, then
if ( ! getWorld().getObjects(Crab.class).isEmpty() )
{
    Crab crab = (Crab) getWorld().getObjects(Crab.class).get(0); // set reference
    GreenfootImage image = new GreenfootImage("Counter.png");
    Greenfoot text = new GreenfootImage("" + crab.munition, 22, Color.BLACK, transparent);
    // etc.
}
largowinch123 largowinch123

2014/11/9

#
Thank you Supper_Hippo. I'm Spanish,and "image" in spanish is "imagen".,so I try to translate the whole code, but I I have forgotten this part. The act method that you say it could be getAwtImage?
largowinch123 largowinch123

2014/11/9

#
Ok DanPost, it doesn't work and I don't know why (the error is incompatible types: greenfoot.Greenfoot Image cannot be coverted to greenfoot.Greenfoot). I send you the crab code too for if you can see where is the problem. Counter Class:
public class Counter extends Counter
{
    private static final Color transparent = new Color(0,0,0,0);
    public GreenfootImage image; 
    
    public void act() 
    {
        showMunitionInCounter();
    } 
    
    public Counter()
    {
        setImage(new GreenfootImage("Counter.png"));
        
    }
    
    private void showMunitionInCounter()
    {
        GreenfootImage image = new GreenfootImage("Counter.png");
        Greenfoot text = new GreenfootImage("" + crab.munition, 22, Color.BLACK, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth()), (image.getHeight()-32));
        setImage(image);
    }
}
and Crab's Code:
public class Cranc extends Actor

{
         public int munition = 50;

        public void shot()  
    {  
        int xBarrel =    Greenfoot.getRandomNumber(600);
        int xChest= Greenfoot.getRandomNumber(600);
        if ("space".equals(Greenfoot.getKey()))  
        {  
             munition = munition - 1;
            if (munition >= 0)
            {
                disparaRoca();
                 
            }
            
            if (munition == 20)
            {
                Barrel barrel = new Barrel();
                getWorld().addObject(barrel, xBarrel, 0);
                Chest chest= new Chest();
                getWorld().addObject(chest, chest, 0);
            }
            
        }  
    }

     public void shotRock()
    {
       World wordl= this.getWorld();
       world.addObject(new Rock(), getX(), (getY() - 50));
       
    }
      
}
I hope have translated the whole code and I hope you understand me. I wrote my crab's code for, if you can tell me where is the error. Thank you very much!
danpost danpost

2014/11/9

#
I had copied your line directly into my code which now appears at line 20 of your last Counter class posting. It should be:
GreenfootImage text = ...
I had not caught that error originally.
largowinch123 largowinch123

2014/11/9

#
Ok, i copy the actual Counter code, but this is a subclass which name is bullets (Counter it is the class). It is the same that before, but I want that it is appear in the counter, the munition of the crab, and yes, there is only one crab. Please help me in that, and forgot the error. I have seen many tutorials of how make a counter but anything help me, and just case i copy the crab's code: Counter:
public class Bullets extends Counter  
{  
    private static final Color transparent = new Color(0,0,0,0);  
    public GreenfootImage image;   
      
    public void act()   
    {  
        showMunitionInCounter();  
    }   
      
    public Counter()  
    {  
        setImage(new GreenfootImage("Counter.png"));  
          
    }  
      
    private void showMunitionInCounter()  
    {  
        GreenfootImage image = new GreenfootImage("Counter.png");  
        Greenfoot text = new GreenfootImage("" + crab.munition, 22, Color.BLACK, transparent);  
        image.drawImage(text, (image.getWidth()-text.getWidth()), (image.getHeight()-32));  
        setImage(image);  
    }  
}  
Crab:
public class Crab extends Actor  
  
{  
         public int munition = 50;  
  
        public void shot()    
    {    
        int xBarrel =    Greenfoot.getRandomNumber(600);  
        int xChest= Greenfoot.getRandomNumber(600);  
        if ("space".equals(Greenfoot.getKey()))    
        {    
             munition = munition - 1;  
            if (munition >= 0)  
            {  
                shotRock();  
                   
            }  
              
            if (munition == 20)  
            {  
                Barrel barrel = new Barrel();  
                getWorld().addObject(barrel, xBarrel, 0);  
                Chest chest= new Chest();  
                getWorld().addObject(chest, chest, 0);  
            }  
              
        }    
    }  
  
     public void shotRock()  
    {  
       World wordl= this.getWorld();  
       world.addObject(new Rock(), getX(), (getY() - 50));  
         
    }  
        
}
PD: I only copy the part that I think is important of the crab (The Bullets code it is all). Again, sorry because of my english, and I try to translate all. Please, if you don't understand anything, tell me.
danpost danpost

2014/11/9

#
A 'bullet' is NOT a 'counter' and, therefore, the Bullet class must not extend the Counter class. Subclassing (or having one class extend another) should only be done to modify the behavior or state of the type of object the class being extended creates. Have the Bullet class extend Actor. NO. WAIT. You have the counter code in that class. Make it a Counter class that extends Actor.
danpost danpost

2014/11/9

#
You should have another class called Bullet that also extends Actor. Its code (act method) should move the bullet and remove itself upon reaching an edge of the world.
largowinch123 largowinch123

2014/11/9

#
Thank you. Its true, but I'm still without understand how to do the bullet counter decrease every time that I shot.
danpost danpost

2014/11/10

#
Counter counter = (Counter)getWorld().getObjects(Counter.class).get(0);
The above line can be used in your Crab class to obtain a reference to the counter. After that line you can call any public method on 'counter' ( counter.methodName(parameters); ).
You need to login to post a reply.