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

2012/9/18

how to check an objects rotation from an object

bqm11 bqm11

2012/9/18

#
I have an actor called "ball" and I want to be able to reference "ball"s current rotation from my other actor called "bullet" i want to do something like setRotation(whatever ball's checkRotation() is currently returning); from my bullet actor thanks
SPower SPower

2012/9/18

#
Well, checkRotation is already created by the greenfoot API, it's called getRotation. And to do what you want, you need a reference to a Ball object. Do you have an idea of how you're gonna do that?
Gevater_Tod4711 Gevater_Tod4711

2012/9/18

#
Well if you got only on "ball" at your world the simplest way would be to use static values:
private static int rotation = 0;

public void act() {
    ...
    rotation = getRotation();
    ...
}

public static int getActorsRotation() {
    return rotation;
}
But this only works if you got one actor from type ball. If you got more of them use this:
//In the class where you need the rotation of "ball";
//you first need to import java.util.List;
//also you need a integer in your "ball" class to check which ball you want to get the rotation;

private int ballsRotation = 0;

private ball b;

public void act() {
    if (b == null) {
        List<ball> list = getWorld().getObjects(ball.class);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getTheIntegerValue() == 1) { //or any other value. You just must be sure that this ball is the right one.
                b = list.get(i);
        }
    }
    else {
        //the code in which you need the rotation of ball.
    }
}
Gevater_Tod4711 Gevater_Tod4711

2012/9/18

#
Gevater_Tod4711 wrote...
Well if you got only on "ball" at your world the simplest way would be to use static values:
private static int rotation = 0;

public void act() {
    ...
    rotation = getRotation();
    ...
}

public static int getActorsRotation() {
    return rotation;
}
But this only works if you got one actor from type ball. If you got more of them use this:
//In the class where you need the rotation of "ball";
//you first need to import java.util.List;
//also you need a integer in your "ball" class to check which ball you want to get the rotation;

private int ballsRotation = 0;

private ball b;

public void act() {
    if (b == null) {
        List<ball> list = getWorld().getObjects(ball.class);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getTheIntegerValue() == 1) { //or any other value. You just must be sure that this ball is the right one.
                b = list.get(i);
            }
        }
    }
    else {
        //the code in which you need the rotation of ball.
    }
}
Gevater_Tod4711 Gevater_Tod4711

2012/9/18

#
oh I used the wrong link to correct my code sorry
You need to login to post a reply.