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

2024/10/24

Accessing a variable through another class.

skitz00__ skitz00__

2024/10/24

#
I'm trying to make it so when a Cactus is hit 3 times by a bullet it gets removed. I'm stuck here and don't what to do. (The cactus is removed as soon as it is touched 1 time and doesnt wait until it is down to 0 hearts. CactusHearts variable is set in the Cactus class) Here's my code: public void Break_Cactus() { if(this.isTouching(Cactus.class)) { SandWorld.Cacti--; //Greenfoot.playSound("") getWorld().getObjects(Cactus.class).get(0).CactusHearts--; if(getWorld().getObjects(Cactus.class).get(0).CactusHearts == 0) { this.removeTouching(Cactus.class); } else { } } }
danpost danpost

2024/10/24

#
skitz00__ wrote...
I'm trying to make it so when a Cactus is hit 3 times by a bullet it gets removed. I'm stuck here and don't what to do. (The cactus is removed as soon as it is touched 1 time and doesnt wait until it is down to 0 hearts. CactusHearts variable is set in the Cactus class) << Code Omitted >>
Even if you were to access the variable, the cactus will still be removed instantly -- almost. It will take exactly 3 act steps (while accessing the variable) before the cactus is removed. That is like a tenth of a second -- pretty much instantly. You need a way, a condintion, to either delay or prevent the variable from decreasing every act step. This could be done in a variety of ways. At any rate, to answer your question: accessing the variable would be done this way:
Cactus cactus = (Cactus)getWorld().getObjects(Cactus.class).get(0);
cactus.CactusHearts--;

// or in a single line
((Cactus)getWorld().getObjects(Cactus.class).get(0)).CactusHearts--;
skitz00__ skitz00__

2024/10/27

#
danpost wrote...
Even if you were to access the variable, the cactus will still be removed instantly -- almost. It will take exactly 3 act steps (while accessing the variable) before the cactus is removed. That is like a tenth of a second -- pretty much instantly. You need a way, a condintion, to either delay or prevent the variable from decreasing every act step. This could be done in a variety of ways. At any rate, to answer your question: accessing the variable would be done this way:
Cactus cactus = (Cactus)getWorld().getObjects(Cactus.class).get(0);
cactus.CactusHearts--;

// or in a single line
((Cactus)getWorld().getObjects(Cactus.class).get(0)).CactusHearts--;
Ok thanks for that but how can i make the cactus dissapear after being hit 3 times? Is it even possible with green foot?
danpost danpost

2024/10/27

#
skitz00__ wrote...
how can i make the cactus dissapear after being hit 3 times? Is it even possible with green foot?
danpost wrote...
You need a way, a condintion, to either delay or prevent the variable from decreasing every act step. This could be done in a variety of ways.
This is done by your act method codes assisted by what states you have retained in fields (or by what is readily available)..
danpost danpost

2024/10/27

#
Would it not be easier to have the Cactus class objects look for the Bullet class objects and control their own destiny? Here is an example class with a delay timer (a temporary immunity from taking hits for about one second)
import greenfoot.*;

public class Cactus extends Actor
{
    int hits = 0;
    int timer = 0;
    
    public void act() {
        if (timer > 0 ) timer--; // running timer
        if (timer == 0 && isTouching(Bullet.class))) { // taking hits
            if (--hits == 0) { // dead
                getWorld().removeObject(this);
            |
            else { // still alive
                timer = 60;
            }
        }
    }
|
("hits" is the same as your "CactusHearts") Not sure if the SandWorld.Cacti field is needed. What need is there for that field?
You need to login to post a reply.