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

2012/10/13

Variable Globalisation

roronoa roronoa

2012/10/13

#
I have an Actor subclass called AllCards, an AllCards subclass called Decks and 2 subclasses of Decks which are called SenatorDeck and ActionCardsDeck The code of class Decks:
import greenfoot.*;

import java.util.List;
import java.util.ArrayList;

public class Decks extends AllActors
{
    public int cardsNumber=5; //number of cards each player has on his hand at the beginning of the game
}
The code of class SenatorDeck
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class SenatorDeck extends Decks
{
    private Label myLabel;
    public int count=6-players;
    public SenatorDeck(Label label)
    {
        myLabel=label;
    }
    public void act()
    {
     if (Greenfoot.mouseClicked(this) && count!=0)
     {
        Senator senator = new Senator();
        getWorld().addObject(senator, 87+cardsNumber*40, 1680);
        count--;
        myLabel.setText(""+count);
        cardsNumber++; //I wanted to save the value of this variable, to make it global
     }
    }
}
The code of class ActionCardsDeck:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class ActionCardsDeck extends Decks
{   
    private Label2 myLabel;
    public int count=128;
    public ActionCardsDeck(Label2 label2)
    {
        myLabel=label2;
    }
    public void thinking()
    {
        int number = Greenfoot.getRandomNumber(4);
        if (number==0) 
        {
            getWorld().addObject(new Cyrk(), 87 + cardsNumber * 40, 1680); //so the value of cardsNumber would be the same as it's in the if of the previous class and vice versa.
        }
        if (number==1)
        {
            getWorld().addObject(new Latryna(), 87 + cardsNumber * 40, 1680);
        }
        if (number==2)
        {
            getWorld().addObject(new ŁukTryumfalny(), 87 + cardsNumber * 40, 1680);
        }
        if (number==3)
        {
            getWorld().addObject(new Ogród(), 87 + cardsNumber * 40, 1680);
        }
        count--;
        myLabel.setText(""+count);
        cardsNumber++;
        if (count==0)
        {
            Greenfoot.stop();
        }
    }
    public void act()      
    {
        if (Greenfoot.mouseClicked(this))
        {
            if (cardsNumber>=5)
            {            
                thinking();
            }
            else
            {
                for (int i=cardsNumber; i<5; i++)
                {
                    thinking();
                }
            }
        }
    } 
}   
erdelf erdelf

2012/10/13

#
do you have a question or a problem?
roronoa roronoa

2012/10/13

#
It's both. A problem cause the game isn't working properly and a question: "How to globalise a local variable?"
davmac davmac

2012/10/14

#
I think you mean "How to turn an instance variable into a class variable", in which case the answer is to use the 'static' keyword, eg: public static int count = 0; ("local" variables are variables which are declared inside methods and which are "local" to the method. The only way to make them non-local is to move their declaration outside the method.)
danpost danpost

2012/10/14

#
You said Decks was a subclass of AllCards, however it is coded as a subclass of AllActors. That would certainly throw a wrench in the works if it was not coded correctly.
roronoa roronoa

2012/10/14

#
danpost wrote...
You said Decks was a subclass of AllCards, however it is coded as a subclass of AllActors. That would certainly throw a wrench in the works if it was not coded correctly.
No, Decks is a subclass of AllActors, sorry for confusion, it was my mistake. The problem is solved, thank you davmac.
You need to login to post a reply.