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

2021/11/18

Help im going insane about a Counter

1
2
Monk_08 Monk_08

2021/11/20

#
yes, there is the chance that there will be multiple
Monk_08 Monk_08

2021/11/20

#
I moved it outside the method and now the counter is not stacking but the value changes by 10 not 1 anymore
danpost danpost

2021/11/20

#
Monk_08 wrote...
yes, there is the chance that there will be multiple
Then the counter needs to be in a static variable and the world needs to create the Counter object.
danpost danpost

2021/11/20

#
Monk_08 wrote...
I moved it outside the method and now the counter is not stacking but the value changes by 10 not 1 anymore
In Rocket class, remove lines 32 and 35 and change line 43 to
moneyCounter.setValue(moneyCounter.getValue()+1);
Monk_08 Monk_08

2021/11/21

#
Omg thanks for the Help it works now :) but could you explain why that In Rocket class, remove lines 32 and 35 and change line 43 to
moneyCounter.setValue(moneyCounter.getValue()+1);
fixed it
danpost danpost

2021/11/21

#
Monk_08 wrote...
Omg thanks for the Help it works now :) but could you explain why that ... fixed it
Each time, you were incrementing the money field and then adding its value to the counter. As such the counter values were 0, 1, 3, 6, 10 ... Also, you had the money field as a class field (static), so it does not start at zero for each rocket -- it retained its value. However, by directly incrementing the counter,the money field was not needed at all. It is always better, when possible, to do something directly, rather than using a variable to prepare to do it later.
Monk_08 Monk_08

2021/11/21

#
I think I get it great help danpost thanks :)
Monk_08 Monk_08

2021/11/21

#
Hey still a question every thing is working fine but now I need to access the counter from another actor how would i do that
Monk_08 Monk_08

2021/11/21

#
Ok so 2 things I want the counter to lose 25% of the value when the robot gets hit and when you reset the value should stay the second thing I got a shop with buttons the button should obviously cost money to use but for that I need to set the money value from other actors Here the robot code with the button inside
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
hard reset hinzufügen 
knöpfe lernen
(World)//vielicht auto turret 
töne
animationen#
hintergrund#
tot 25% geldverlust
 * project plan: kauf system hinzufügen 
 * (ROBOTER)für wie schnell schieße ich 
 * (Schuss)für wie groß sind meine kugeln 
 * (Rakete)für geld pro rakte 
 * (ROBOTER)unsterblichkeit kaufen für 1000 + im menü unsterblich 
 * (ROBOTER)game end für 10000 kaufen 
 * 
 */
public class Robot extends Animal
{
    /**
     * Act - do whatever the Robot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        shop();
        shoot();
        turn();
        System.out.println("firerate" + fireRate);
    }

    public Counter actCounter;
    int fireRate = 28;
    public int overheat;
    public int duration = fireRate; // Zeit bis zum nächsten schuss
    private int firing;

    public void shoot()
    {
        if (firing != 0)
            firing--;
        {    
            if (Greenfoot.isKeyDown("space") && firing == 0)
            {
                addBullet();
            

                firing = fireRate;
            }
        }
    }

    public void addBullet()
    {
        Bullet bullet = new Bullet(); 
        getWorld().addObject(bullet, getX() +2, getY());
        bullet.setRotation(getRotation());
    }

    public void turn()
    {
        if (Greenfoot.isKeyDown("d"))
        {
            turn​(8);
        }

        if (Greenfoot.isKeyDown("a"))
        {
            turn​(-8);
        }
    }

    Button upgradeShootingSpeed = new Button("shooting_speed_not_Pressed.png");

    public void shop()
    {
        if(Greenfoot.isKeyDown("e"))
        {   if(fireRate != 0)
            {
                upgradeShootingSpeed.setImage("shooting_speed_not_Pressed.png");
                getWorld().addObject(upgradeShootingSpeed, 450, 100);
                getWorld().showText("Level:  "+fireRate, 750, 100);
            }
            else
            {
                upgradeShootingSpeed.setImage("shooting_speed_max.png");
                getWorld().addObject(upgradeShootingSpeed, 450, 100);
            }
        }

        if(fireRate != 0)
        {
            if(Greenfoot.mouseClicked(upgradeShootingSpeed)){ 
                upgradeShootingSpeed.setImage("shooting_speed_Pressed.png");
                fireRate = fireRate-4;
                Greenfoot.delay(7);
                upgradeShootingSpeed.setImage("shooting_speed_not_Pressed.png");
                getWorld().showText("Level:  "+fireRate, 750, 100);
                if(fireRate == 0)
                {
                    upgradeShootingSpeed.setImage("shooting_speed_max.png");
                    getWorld().showText("Level: Max", 750, 100);
                }
            }
        }
        
         if(Greenfoot.isKeyDown("escape"))
        {
            getWorld().removeObject(upgradeShootingSpeed);
            getWorld().showText("", 750, 100);
        }
        
        
        
    }
    
    
}
Here The Rocket Code With the Counter in it
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Rocket here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Rocket extends Animal
{
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        move(7);
        die();
        if (getWorld() != null) 
        {
            kill(); 
        }
    }

    protected void addedToWorld(World world)
    {
        if (moneyCounter == null) 
        {moneyCounter = new Counter("Money: ");
            world.addObject(moneyCounter, 60, 40);
        }
    }

    public Counter moneyCounter;

    public void die()
    {
        if(isTouching(Bullet.class))
        {

            moneyCounter.setValue(moneyCounter.getValue()+1);

            getWorld().removeObject(this);

        }
    }

    public void kill()
    {
        if(isTouching(Robot.class))
        {
            getWorld().removeObject(this);
            Greenfoot.stop();
        }
    }

    
    }
and the code of MyWorld
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    GifImage SPBG = new GifImage("space_background.gif");

    
    public MyWorld()
    {    
        super(900, 900, 1); //welt mit 900 mal 900 pixeln 
        setBackground("space_background.gif");// damit es keinen White screen gibt beim reset 
        prepare(); //roboter hinzufügen
        if(stopSpawn == false)
        {
            rocketSpawn(1);
        }

    }

    public void act()
    {
        stop();
        regulateRockets();
        setBackground( SPBG.getCurrentImage() );
    }

    public void regulateRockets()
    {
        if (getObjects(Rocket.class).size() < 1)
        {
            if(stopSpawn == false)
            {
                rocketSpawn(1);
            }
        }
    }

    public void prepare()
    {
        Robot robot = new Robot();
        addObject(robot , 450 ,450 );
    }

    public boolean stopSpawn = false;
    
    public void stop()
    {
        if(("e").equals(Greenfoot.getKey()))
        {
            stopSpawn = true;
            
            removeObject(r);
        }

        if(Greenfoot.isKeyDown("escape")){
            
            stopSpawn = false;
            
            rocketSpawn(1);
        }
    }

    Rocket r = new Rocket();
    public void rocketSpawn (int howMany)
    {
        for(int i=0; i<howMany; i++) {
            int border = Greenfoot.getRandomNumber(4);
            int position = Greenfoot.getRandomNumber(900);
            if(border == 1)
            {
                addObject(r ,position ,0);
            }else if(border == 2)
            {
                addObject(r ,0 ,position);
            }else if(border == 3)
            {
                addObject(r ,position ,900);
            }else if(border == 4)
            {
                addObject(r ,900 ,position);
            }
            r.turnTowards(450, 450);
        }
    }
}
thanks for all the solutions that you provided me with and thanks in advance for the help
Spock47 Spock47

2021/11/28

#
I think you are now in the position for some refactoring. If I understand your scenario correctly, the robot is the "protagonist", the actor the player controls. So, it seems that the robot is the one that "owns" the money. 1. So naturally the moneyCounter should be moved to the Robot class.
public class Robot extends Animal
{
    
    private Counter money = new Counter("Money: ");
 
    protected void addedToWorld(World world)
    {
        world.addObject(money, 60, 40);
    }
...

}
As you can see, the source code to manage the money counter already got simpler. But more importantly, it is now in its "natural" place. Who would you ask how much money is there? Most likely the one that owns the money. And now the source code reflects that since the robot is now the one that has the money reference. 2. Currently, if a rocket is hid by a bullet, the money counter gets increased. For that, the money counter has to be referenced at the position where the bullet-rocket-collision is checked. Since the robot starts the bullet and the bullet is the one killing the rocket, I suggest to - give the bullet the kill-action instead of the rocket - and have the bullet know the robot that launched it:
public class Bullet extends Actor
{
    ...
    // the robot that launched the bullet.
    private final Robot owner;
    
    public Bullet( final Robot _owner ) {
        owner = _owner;
    }
    
    public void act()
    {
        ...
        killRocket();
    }
 
    private void killRocket()
    {
        for (final Rocket collidingRocket : this.getIntersectingObjects(Rocket.class)) {
            getWorld().removeObject(collidingRocket);
            owner.killedRocket();
        }
    }
    ...
}
As you can see, here a new method "killedRocket" of the robot is called. In this method, you can define whatever reward should be awarded to the robot, e.g.
    public void killedRocket() {
        money.setValue(money.getValue() + 1);
    }
And in the place, where you create the bullet (first line of Robot.addBullet()), you have to give the robot as parameter:
Bullet bullet = new Bullet(this);
3. Now, this source code is functionally the same as before, but now you have the money reference at the point where you want to have it: in the robot. And that means, that the upcoming changes are fairly easy: you just need a reference to the robot and then you can handle his money, i.e. in the shop method of the robot, you can directly change the money of the robot and also you could have the robot react to being killed by a rocket, e.g.
in class Rocket:
    private void killRobot() // renamed from "kill()"
    {
        for (final Robot collidingRobot : getIntersectingObjects(Robot.class)) {
            getWorld().removeObject(this);
            collidingRobot.killedByRocket();
        }
    }

in class Robot:
    public void killedByRocket() {
        money.setValue(3 * money.getValue() / 4);
    }
For the shop, I suggest that you create a new class Shop and give it a reference to the robot similar as it is done with the bullet above. That way, the source code stays easier to understand with all the shop operations in the Shop class. 4. Also, the refactoring makes it possible to repair the problem with multiple rockets. The previous implementation only allowed one rocket at a time, because the ONE rocket was in charge of the money. Defining "Rocket r = new Rocket();" outside of "rocketSpawn" meant that there was always only one rocket - even if you run the loop 4 times, it will just reposition the same rocket four times - not creating four rockets. So, now, you can put "Rocket r = new Rocket();" back into the method "rocketSpawn" and it will actually create multiple rockets (if the loop runs multiple times) and it works for any number of rockets. Live long and prosper, Spock47
You need to login to post a reply.
1
2