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

2014/10/9

in range detection

Maricano Maricano

2014/10/9

#
hi all:) we are a grup of 3 peopel making a tower defence game in a 24 hours scholl challenge and we are stuck whit the tower:) it shoot and turns and almost works as it should... but we cant get it to only shoot in for a specefik range and only shot if there are enemys in that range. please help:)
danpost danpost

2014/10/9

#
Use the Actor class 'getObjectsInRange' method and if the list returned is not empty, use the distance formula to determine the closest object listed, and fire in its direction. Wrap the above with a timer so that you do not spray bullets. You should have a field for the maximum range value so different towers can have different ranges.
Maricano Maricano

2014/10/9

#
Thanks:) we have an generel idear now:) hope we get it to work:)
Maricano Maricano

2014/10/9

#
Got another problem now. and it is anoying the shooting and follow og range is working. but cant remove the arrow we shot for the tower when it hits the enemy code: public void death() { Level1 lvl1=(Level1)getWorld(); if(getOneIntersectingObject(ArrowBullet.class) !=null) { getWorld().removeObject(this); getWorld().removeObject(ArrowBullet.class); lvl1.addGold(plusGold); } it is written in the enemy and that part works:) please help:)
danpost danpost

2014/10/9

#
The World 'getObject' method needs a specific actor to remove from the world, not just a class. Also, you need to remove the Arrow object before removing the enemy; although, you could just use 'lvl1.removeObject(...)' (in your second 'removeObect' line, 'getWorld' will return 'null' and you will throw a NullPointerException because you have removed the actor from the world in the first 'removeObject' line, so it has no World). There are 'isTouching' and 'removeTouching' methods in Actor that you can use for this:
1
2
3
4
5
6
if (isTouching(ArrowBullet.class))
{
    removeTouching(ArrowBullet.class);
    lvl1.removeObject(this);
    lvl1.addGold(plusGold);
}
danpost danpost

2014/10/9

#
You will probably end up with problems later when you add more levels. The line 'Level1 lvl1 = (Level1)getWorld();' will throw a casting exception (you cannot cast a 'Level2' world to type 'Level1').
Maricano Maricano

2014/10/9

#
tanks:) that fiksed one thing know we get this error message java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. and it comes from this line of code. if (isTouching(ArrowBullet.class)) the hole metode looks like this. public void death() { Level1 lvl1=(Level1)getWorld(); if (isTouching(ArrowBullet.class)) { removeTouching(ArrowBullet.class); lvl1.removeObject(this); lvl1.spawnDecrease(spawnD); lvl1.addGold(plusGold); } }
Maricano Maricano

2014/10/9

#
we solved it:) thanks for all the help:)
You need to login to post a reply.