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

2017/6/20

I'm trying to check in my Aang class if the Fireball class is not touching the Aang class

Sirpancakeking Sirpancakeking

2017/6/20

#
My plan is that if the fireball class is not touching the aang class, then the rotation of the fireball cannot be changed mid air. This is because the fireball shoots in the same direction as the aang class and if the aang class changes direction while the fireball is being shot the fireball will switch direction as well which I do not want. So if nothing else, I would like top know the code to check if a class is not touching another class.
Super_Hippo Super_Hippo

2017/6/20

#
A class is never touching a class. The fireball should not respond to key presses. If you want the fireball to be in the world before it is shot and move/rotate with the aang object, then the aang object should control the fireball. You can give the fireball a boolean to see if it was already shot:
private boolean shot = false;

public void act()
{
    if (!shot) return; //if not shot, do nothing
    //move straight
}
danpost danpost

2017/6/20

#
If it is happening as you say, then you are using 'setRotation' on the fireball in a place where you should not be using it. Best is not to have the 'setRotation' method invoked from within the class of the fireball at all.
Sirpancakeking Sirpancakeking

2017/6/20

#
Alright, let me rephrase this. What is the code to see if a class is intersecting with another class, and if it is, a boolean variable is true.
Yehuda Yehuda

2017/6/20

#
To check if the Aang class is touching the Fireball class you can put the following in the Fireball's act method.
if (isTouching(Aang.class)) { //same as having '== true' at the end
    //code to do if touching Aang
}
Nothing will happen when two classes intersect unless you make it.
danpost danpost

2017/6/21

#
Sirpancakeking wrote...
What is the code to see if a class is intersecting with another class, and if it is, a boolean variable is true.
boolean touchingAang = isTouching(Aang.class);
In other words, you do not need to write any code for that because there already is a method provided by the Actor class that does exactly that. The code is what 'isTouching' does and it returns the boolean value required. Its code is this:
protected boolean isTouching(Class cls)
{
    failIfNotInWorld();
    return getOneIntersectingObject(cls) != null;
}
which shows another way you could code it:
if (getOneIntersectingObject(Aang.class) != null)
or
boolean touchingAang = getOneIntersectingObject(Aang.class) != null;
Venbha Venbha

2017/6/21

#
hello by i want no thingsha
Sirpancakeking Sirpancakeking

2017/6/22

#
All right so I got it to shoot in the direction I want to, but the problem is, the fireball switches direction even if it's not touching the main class so I don't really know how to make it so when its in the air or really whenever it exists that it cannot switch direction. This is what I have so far.
// this is the fireball class
boolean shootdirection = false;
        move(10);
        if(isTouching(Gnaa.class))
        {
         shootdirection = true;
        }
        if(shootdirection = true)
        {
        if(Greenfoot.isKeyDown("up"))
         {
            setRotation(270);
         }
        if(Greenfoot.isKeyDown("down"))
         {
            setRotation(90);
         }
        if(Greenfoot.isKeyDown("left"))
         {
            setRotation(180);
         }
        if(Greenfoot.isKeyDown("right"))
         {
            setRotation(0);
         }
        }
//this is the main class
if(shootdirection = true)
        {
          if((Greenfoot.isKeyDown("up") && shoottimer < 1)){
            shooting = true;
            Actor fireball= new Fireball();
            getWorld().addObject(fireball, getX(), getY());
        }
        if((Greenfoot.isKeyDown("down") && shoottimer < 1)){
            shooting = true;
            Actor fireball= new Fireball();
            getWorld().addObject(fireball, getX(), getY());
        }
        if((Greenfoot.isKeyDown("left") && shoottimer < 1)){
            shooting = true;
            Actor fireball= new Fireball();
            getWorld().addObject(fireball, getX(), getY());
        }
        if((Greenfoot.isKeyDown("right") && shoottimer < 1)){
            shooting = true;
            Actor fireball= new Fireball();
            getWorld().addObject(fireball, getX(), getY());
        }
        }
Super_Hippo Super_Hippo

2017/6/22

#
We already told you that the fireball should not change its direction. By the way, when you want to compare something, you need to use == and not =.
//You can use
if (shootdirection)
//or
if (shootdirection == true)
//but not
if (shootdirection = true)
danpost danpost

2017/6/22

#
Remove everything in the Fireball class given above except for line 3. In the main class separate the movement code from the shooting code. Post the entire main class code for review when done.
You need to login to post a reply.