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

2012/11/11

Intersecting object

1
2
danpost danpost

2012/11/12

#
Where are you calling this method from (show the method that calls this one)? Also, what class is this 'newDragon' method in?
kevv kevv

2012/11/12

#
newDragon is in the Dragon.class, I am calling newDragon() from the act method
kevv kevv

2012/11/12

#
I'll just open the source code. Then you can see the mess....
kevv kevv

2012/11/12

#
http://www.greenfoot.org/scenarios/6523 There is still a problem wtih the counter, keeps on addind while it touches the dragon object. Should only do it once.
danpost danpost

2012/11/12

#
OK, I see why you are having problems. One way to deal with this, is to NOT remove and add dragons, just relocate the one when killed back to (0, 400).
kevv kevv

2012/11/12

#
Ok and add adding a new object of dead dragon would be a good idea?
danpost danpost

2012/11/12

#
With the counter (I have not looked at your code), you will need to do something like the following.
// add an instance boolean to the class
private boolean onDragon = false;
// code for counter
if (!onDragon && !getIntersectingObjects(Dragon.class).isEmpty())
{
    // increment counter
    // any other actions
    onDragon = true;
}
if (onDragon && getIntersectingObjects(Dragon.class).isEmpty()) onDragon = false;
danpost danpost

2012/11/12

#
Or better might be to make sure the dragon is alive before adding to the counter (so you do not get counted for hitting one that is not alive).
danpost danpost

2012/11/12

#
kevv wrote...
Ok and add adding a new object of dead dragon would be a good idea?
This has new information (which would have helped, if I had known this previously). OK, we need to start over as far as 'adding/removing/deleting' dragons. You will need a field in the Dragon class, but it will need to be a 'static' field. It can hold either a Dragon object or be a boolean. Being a dragon object could make other coding easier. If holding a dragon object: It will hold the dragon object that IS alive. It should be changed to 'null' when the live dragon is killed. It should be checked by the world act to add a new dragon object if neccessary (if 'null'). The constructor of the Dragon class should set 'this' (the dragon being created) to the Dragon field.
// Dragon class field
static Dragon liveDragon = null;
// in Dragon constructor
liveDragon = this;
// when killed
liveDragon = null;
// for the act method
if (this == liveDragon)
{
    // code for live dragon
    // when dragon dies
    liveDragon = null;
}
else
{
    // code for dead dragon
}
if (liveDragon == null) getWorld().addObject(new Dragon(counter), 0, 400);
Hope this helps.
You need to login to post a reply.
1
2