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

2018/12/18

Updating Money score.

1
2
DragonL68 DragonL68

2018/12/18

#
I want to make A shop in my game but when you buy somthing the money score should change but it doesen't. Here is the code. you schuld now that "Geld" means money.
public class Shop extends Objects
{
    
    int Geld = 0;
    /**
     * Act - do whatever the Shop wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
        if(Greenfoot.mouseClicked(this)){
            getWorld().addObject(new He162price(),500 ,500);
            getWorld().addObject(new Ki67price(),500 ,565);
            getWorld().addObject(new Meteorprice(),500 ,637);
            getWorld().addObject(new Mitchellprice(),500 ,704);
            getWorld().addObject(new Spitfireprice(),500 ,760);
            getWorld().addObject(new Yakprice(),500 ,800);
            getWorld().addObject(new bf109price(),500 ,850);
        }
        if(Greenfoot.mouseClicked(getWorld())){
           getWorld().removeObjects(getWorld().getObjects(He162price.class));
           getWorld().removeObjects(getWorld().getObjects(Ki67price.class));
           getWorld().removeObjects(getWorld().getObjects(Meteorprice.class));
           getWorld().removeObjects(getWorld().getObjects(Mitchellprice.class));
           getWorld().removeObjects(getWorld().getObjects(Spitfireprice.class));
           getWorld().removeObjects(getWorld().getObjects(Yakprice.class));
           getWorld().removeObjects(getWorld().getObjects(bf109price.class));
        }
        if (Greenfoot.mouseClicked(He162price.class)){
            Geld = Geld - 800;
            getWorld().addObject(new He162(),250 ,485);
        }
        if (Greenfoot.mouseClicked(Ki67price.class)){
            getWorld().addObject(new KI67(),250 ,485);
            Geld = Geld - 700;
        }
        if (Greenfoot.mouseClicked(Meteorprice.class)){
            Geld = Geld - 1000;
            getWorld().addObject(new Meteor(),250 ,485);
        }
        if (Greenfoot.mouseClicked(Mitchellprice.class)){
            Geld = Geld - 800;
            getWorld().addObject(new Mitchell(),250 ,485);
        }
        if (Greenfoot.mouseClicked(Spitfireprice.class)){
            Geld = Geld - 400;
            getWorld().addObject(new Spitfire(),250 ,485);
        }
        if (Greenfoot.mouseClicked(Yakprice.class)){
            Geld = Geld - 400;
            getWorld().addObject(new Yak9(),250 ,485);
        }
        if (Greenfoot.mouseClicked(bf109price.class)){
            Geld = Geld - 500;
            getWorld().addObject(new BF109(),250 ,485);
        }
        
        getWorld().showText("Geld: " + Geld , 280, 400);
        getWorld().showText("$" , 335, 400);
    }    
}
danpost danpost

2018/12/18

#
You cannot click on an object of type Class -- only on a World or Actor type objects. The mouseClicked method is expecting either 'null' or a specific object created by way of the World or Actor class. Lines 12 and 21 are fine as they point to specific objects (the shop and the specific world that the shop has been added into). All others, lines 30, 34, 38, 42, 46, 50 and 54 will never return as a true condition for their respective if statements. Have the shop keep a reference to the price objects. Then, only one set will need be created for each Shop object created and you can for each one ask if a click was performed on them.
DragonL68 DragonL68

2018/12/18

#
but when i click bf109price it spawns a bf109 as writen in the code
DragonL68 DragonL68

2018/12/18

#
How could i fix it?
danpost danpost

2018/12/18

#
DragonL68 wrote...
but when i click bf109price it spawns a bf109 as writen in the code
What code do you have in your bf109price class?
danpost danpost

2018/12/18

#
DragonL68 wrote...
How could i fix it?
danpost wrote...
Have the shop keep a reference to the price objects. Then, only one set will need be created for each Shop object created and you can for each one ask if a click was performed on them.
DragonL68 DragonL68

2018/12/19

#
This is the code in the bf109price
public void act() 
    {
        if(Greenfoot.mouseClicked(this)){
            getWorld().addObject(new BF109(),250 ,485);
        }
    }    
DragonL68 DragonL68

2018/12/19

#
And what do you mean by reference? i'm new to this and don't now how to make a reference.
danpost danpost

2018/12/19

#
DragonL68 wrote...
This is the code in the bf109price << Code Omitted >>
This code is why you have a BF109 object spawning. Line 56 in the Shop class never executes due to a fixed false condition.
DragonL68 DragonL68

2018/12/19

#
and how can i fix it?
danpost danpost

2018/12/19

#
DragonL68 wrote...
And what do you mean by reference? i'm new to this and don't now how to make a reference.
A reference is a variable that points to the object ("contains" the object). You first declare the variable:
Object objectName;
which states you will be using the name 'objectName' to refer to an object of type Object. Once declared, you can assign an object of said type to the variable:
objectName = new bf109price();
The assignment can be done while declaring the variable:
Actor btnBF109price = new bf109price();
To retain the reference, the variable will need to be declared outside any methods, making the variable what is called a field. With a field for each object that can be clicked on, you can then check for clicks on those specific actors:
if (Greenfoot.mouseClicked(btnBF109price))
danpost danpost

2018/12/19

#
DragonL68 wrote...
and how can i fix it?
Remove the act method from all your "price" classes; add reference fields for all "price" objects in the Shop class and have your click detection check for clicks on those objects.
DragonL68 DragonL68

2018/12/20

#
danpost wrote...
DragonL68 wrote...
And what do you mean by reference? i'm new to this and don't now how to make a reference.
A reference is a variable that points to the object ("contains" the object). You first declare the variable:
Object objectName;
which states you will be using the name 'objectName' to refer to an object of type Object. Once declared, you can assign an object of said type to the variable:
objectName = new bf109price();
The assignment can be done while declaring the variable:
Actor btnBF109price = new bf109price();
To retain the reference, the variable will need to be declared outside any methods, making the variable what is called a field. With a field for each object that can be clicked on, you can then check for clicks on those specific actors:
if (Greenfoot.mouseClicked(btnBF109price))
I tried it but it won't work. here's my code.
public class Shop extends Objects
{
    
    int Geld = 20000;
    /**
     * Act - do whatever the Shop wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Object objectName;
        objectName = new bf109price();
        Actor btnBF109price = new bf109price();
        if(Greenfoot.mouseClicked(this)){
            getWorld().addObject(new He162price(),500 ,500);
            getWorld().addObject(new Ki67price(),500 ,565);
            getWorld().addObject(new Meteorprice(),500 ,637);
            getWorld().addObject(new Mitchellprice(),500 ,704);
            getWorld().addObject(new Spitfireprice(),500 ,760);
            getWorld().addObject(new Yakprice(),500 ,800);
            getWorld().addObject(new bf109price(),500 ,850);
        }
        if(Greenfoot.mouseClicked(getWorld())){
           getWorld().removeObjects(getWorld().getObjects(He162price.class));
           getWorld().removeObjects(getWorld().getObjects(Ki67price.class));
           getWorld().removeObjects(getWorld().getObjects(Meteorprice.class));
           getWorld().removeObjects(getWorld().getObjects(Mitchellprice.class));
           getWorld().removeObjects(getWorld().getObjects(Spitfireprice.class));
           getWorld().removeObjects(getWorld().getObjects(Yakprice.class));
           getWorld().removeObjects(getWorld().getObjects(bf109price.class));
        }
        if (Greenfoot.mouseClicked(btnBF109price)){
            Geld = Geld - 500;
            getWorld().addObject(new BF109(),250 ,485);
        }
        getWorld().showText("Geld: " + Geld , 280, 400);
        getWorld().showText("$" , 335, 400);
    }    
Dose it have to be outside the act method?
danpost danpost

2018/12/20

#
DragonL68 wrote...
Dose it have to be outside the act method?
Only the declaration lines need to be outside any method. Here is an example of one clickable object:
private Actor btnBF109 = new bf109price();
int geld;

protected void addedToWold(World world)
{
    adjustGeld(20000);
}

public void act()
{
    if (Greenfoot.mouseClicked(this))
    {
        getWorld().addObject(btnBF109, 500, 850);
    }
    if (Greenfoot.mouseClicked(getWorld())
    {
        getWorld().removeObject(btnBF109);
    }
    if (Greenfoot.mouseClicked(btnBF109))
    {
        adjustGeld(-500);
        getWorld().addObject(new BF109(), 250, 485);
    }
}

public void adjustGeld(int adjustment)
{
    geld += adjustment;
    getWorld().showText("Geld: "+geld, 280, 400);
    getWorld().showText("$", 335, 400);
}
DragonL68 DragonL68

2018/12/20

#
so i tried it it doesn't show any erors until i start it. These are marked as error:
public class bf109price extends Shop
private Actor btnBF109 = new bf109price();
There are more replies on the next page.
1
2