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

2014/2/3

Spotting system

joris0127 joris0127

2014/2/3

#
In my stealth game i need to have something that checks from a coordinate on a object (preferably customizable) if it can see my "Hero". My hero is a circel. I thought maybe something with lines to the perimeter of the circel and that it checks if there is something intersecting that line. But I really dont know how to program this so any help is welcome ;). A picture for explanation: Picture It is my first discussion so any feedback on how to be more clear is welcome
danpost danpost

2014/2/3

#
There is an Actor class method called 'getObjectsInRange' which could be used in the Hero class (since the objects created from that class are circular) to look for the object you are checking for intersection with. The range would be half the width (or height) of the image of the hero (provided there is no buffering transparency around the image of the circle).
joris0127 joris0127

2014/2/3

#
I think I haven't been all that clear, my apologies for that. I mean that an object, for example: "Enemy", can 'see' my hero. So Enemy needs to check if it can have a direct line of sight on Hero from a certain coordinate on the object. Do you know some code wich can locate certain coordinates of a objects? the visual perimeter will also do. Because it could fire invisable objects at those coordinates who can say if they collide with something.
jimboweb jimboweb

2014/2/3

#
The easiest way is to just set a constructor and pass the hero to it when you create the object. Then the object can always access the hero. Say you have class BadGuy, at the top of the class you'd write: Hero myHero; public BadGuy(Hero hero) { myHero = hero; } Then when you create the badguy in the world you do it like this: Hero hero = new Hero(); BadGuy badguy = new BadGuy(hero); then when you want the x and y coordinates in the BadGuy code you can just say: int heroX = myHero.getX(); int heroY = myHero.getY();
jimboweb jimboweb

2014/2/3

#
Using that, you could create an invisible projectile and use the turnTowards() method and see if it hits something before it hits Hero. But there's probably a better way, I will tell you if I think of it.
joris0127 joris0127

2014/2/3

#
Thank you very much i will use this untill I find something better.
You need to login to post a reply.