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

2012/7/28

problems with getOneObjectAtOffset, and text

1
2
danpost danpost

2012/8/2

#
Look at the first line closely! That is an example of how NOT to get a reference to the tank object ('getLocation' does not return a 'Tank' object; or anything close to it, for that matter).
sparrow72 sparrow72

2012/8/4

#
thanks, so then I have a question for you then. I am trying to get a 'Top' of a tank to turn to face my 'tank' at all times. I have made it possible to turn one time but I seem to not be able to reference it more than one time in the code:
Tank tank = new Tank();
            Enemy enemy = new Enemy();
            Top top = new Top();

            getWorld().addObject(tank, 547, 346);
            tank.setRotation(180);
            getWorld().addObject(enemy, 49, 53);
            getWorld().addObject(top, enemy.getY(), enemy.getY());
            top.turnTowards(tank.getX(),tank.getY());
I realize it is only coded to turn to the tank one time but how would I make a reference to the tank in the top without making a new tank?
danpost danpost

2012/8/4

#
In the act() method of Top class, call 'turnTop();'. Then add the method to the class:
private void turnTop()
{
    if (getWorld().getObjects(Tank.class).isEmpty()) return;
    Tank tank = (Tank) getWorld().getObjects(Tank.class).get(0);
    turnTowards(tank.getX(), tank.getY());
}
Now, the Top will always turn toward the Tank (as long as the Tank is in the world).
sparrow72 sparrow72

2012/8/4

#
Thanks! I owe you one :)
You need to login to post a reply.
1
2