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

2020/1/20

Numbers keeps increasing

trevo trevo

2020/1/20

#
So I'm trying to make it so when one projectile turn towards a apple or cactus. But when I run it the number goes up until its the max number. The snake makes a new projectile and depending what button is pressed it either goes for the apples or the cacti. If you want the snake code, just tell me.
public boolean toGetTF;
    Snake sn = new Snake();
    //int maxCac = getWorld().getObjects(Cactus.class).size();
    //int maxApp = getWorld().getObjects(Apple.class).size();
    public void act() 
    {
        move(2);
        if(isAtEdge()) {
            getWorld().removeObject(this);
        }
        try{
            Actor en = getOneIntersectingObject(Cactus.class);
            if (en!=null && toGetTF){
                getWorld().removeObject(en);
                getWorld().removeObject(this);
                setCactus(getCactus()-1);
            }
            Actor en2 = getOneIntersectingObject(Apple.class);
            if (en2!=null&& !toGetTF){
               getWorld().removeObject(en2);
               getWorld().removeObject(this);
               setApple(getApple()-1);
            }
            whoToGet();
        }catch(IllegalStateException ill){}catch(NullPointerException nul){}catch(IndexOutOfBoundsException iob){}
}
    
    public void whoToGet(){ // make it so the number is in another class so i can get the nubmer like that
        if(toGetTF){
            System.out.println(getCactus());
            turnTowards(getWorld().getObjects(Cactus.class).get(getCactus()).getX(), getWorld().getObjects(Cactus.class).get(getCactus()).getY());
            setCactus(getCactus()+1);
        }else{
            System.out.println(getApple());
            turnTowards(getWorld().getObjects(Apple.class).get(getApple()).getX(), getWorld().getObjects(Apple.class).get(getApple()).getY());
            setApple(getApple()+1);
        }
    }
    public int getCactus(){
        return sn.cactusNum;
    }
    public void setCactus(int given){
        sn.cactusNum = given;
    }
    public int getApple(){
        return sn.appleNum;
    }
    public void setApple(int given){
        sn.appleNum = given;
    }
danpost danpost

2020/1/20

#
If this code is for the projectile, I cannot see why you would be creating a Snake object here, at all.
trevo trevo

2020/1/20

#
In the snake class is where the cactusNum and appleNum are stored. I just thought about it and is there a way to get the snake in the worlds current variables?
danpost danpost

2020/1/20

#
trevo wrote...
In the snake class is where the cactusNum and appleNum are stored. I just thought about it and is there a way to get the snake in the worlds current variables?
In world:
// field
public Snake snake;

// in prepare
snake = new Snake();
addObject(snake, 100, 100); // (wherever)
trevo trevo

2020/1/20

#
Thank you and sorry I didn't know how to word it. How would I get the current snake in the world while in the Projectile class. And also my problem is in the whoToGet method. The variables just keep going until the total apple/cactus value instead of just being assigned to that number. If you need me to re-word it please ask. Thanks.
danpost danpost

2020/1/20

#
trevo wrote...
Thank you and sorry I didn't know how to word it. How would I get the current snake in the world while in the Projectile class.
You can use (not as a field; but in action code):
Snake snake = ((MyWorld)getWorld()).snake;
also my problem is in the whoToGet method. The variables just keep going until the total apple/cactus value instead of just being assigned to that number. If you need me to re-word it please ask. Thanks.
What do these numbers represent? It looks like you decrease their values when encountering an enemy, but increase it continually while chasing one.
danpost danpost

2020/1/20

#
As a side note: you should not use try/catch structures on things you can control without the use of them. Only use them in situations that cannot be conditionally regulated. You can move lines 16 and 22 both up two lines (to lines 14 and 20) and remove the try/catch structure.
trevo trevo

2020/1/22

#
Thank you so much for your help!
trevo trevo

2020/1/22

#
And the numbers represent which target it goes to. I couldn't figure out how to make it stop increasing.
danpost danpost

2020/1/22

#
trevo wrote...
And the numbers represent which target it goes to. I couldn't figure out how to make it stop increasing.
If the target for a projectile is fixed, why did you add lines that change the number?
You need to login to post a reply.