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

2018/12/14

Increase cost and add for counter.

MemThef MemThef

2018/12/14

#
Trying to make game where you can buy items with points and make it so that every time you buy an item, the cost of it goes up and the amount of points back you get go up. I have it so that the cost goes up but can't figure out the points back part.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Arrow extends Actor
{
    public void act() 
    {
        GetArrow();
    }
    public int timer2 = 100;
    public int GN = 0;
    public int add1 = 50;
    public void GetArrow()
    {
        //if(timer2 > 0) {
            timer2 --;
        //}
        if(timer2 == 0){
            BackGround theWorld = (BackGround) getWorld(); 
             Counter counter = theWorld.getCounter();  
             decideAdd1();
             GN++;
        }
    }  
      
    public void decideAdd1(){
        
        add1 = add1 + ((3^(GN+1))*GN) + (1+(3^GN)*(GN));
        
        
    }  
}
and
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class ArrowBotton extends Bottons
{
    public int cost1 = 25;
    public int GN = 0;
public  ArrowBotton()
    {
        setImage(new GreenfootImage("Buy Arrow: " + cost1, 20, Color.WHITE, Color.BLUE));
    }

    public void act() 
    {
        if(Greenfoot.mouseClicked(this)==true)
        checkBuy();
        setImage(new GreenfootImage("Buy Arrow: " + cost1, 20, Color.WHITE, Color.BLUE));
    }    
    
    public void checkBuy()
    {
        BackGround theWorld = (BackGround) getWorld();
            Counter counter = theWorld.getCounter();
        if(counter.getValue() >=cost1 ){
            counter.subtract(cost1);
            GN ++;
            decideCost();
            theWorld.addObject(new Arrow(),0,0);
            theWorld.getObjects(Arrow.class).get(0).decideAdd1();
    }
    }
    
    public void decideCost(){
        
        cost1 = cost1 + ((3^(GN+1))*GN) + (1+(3^GN)*(GN));
        
        
    }
}
I appreciate any help
danpost danpost

2018/12/14

#
Maybe you should have a purchase price field in the Arrow class and add a parameter to the Arrow constructor to pass the value to it. That way. each arrow will be able to calculate how much to add to the counter (based on its cost).
You need to login to post a reply.