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

2019/1/28

Help on generic pointers

RenoJackson RenoJackson

2019/1/28

#
I'm creating a program that contains multiple instances of an object. These instances spawn in a while loop and I want to say if ANY one of those instances is touching object x then do delete all further instances which I plan to do by using the break method in the loop. The problem I'm finding is that there is no direct way to say if ANY of the instances are touching object x. I was wondering how you guys would work around this problem. I had a similar problem like this in this same program and I fixed it by creating a for loop that runs equal to the maximum number of times that instance can be on the field and in that loop, it says to create a separate pointer to each of the instances and then give them all the condition I want them to be in. Note however that I said a similar problem, and that I don't think that this method will work on my given example.
danpost danpost

2019/1/28

#
RenoJackson wrote...
I'm creating a program that contains multiple instances of an object. These instances spawn in a while loop and I want to say if ANY one of those instances is touching object x then do delete all further instances which I plan to do by using the break method in the loop. The problem I'm finding is that there is no direct way to say if ANY of the instances are touching object x. I was wondering how you guys would work around this problem.
Let me get this straight. You want to stop creating these object once any placed into the world ends up touching this object x. If so, you could simply put the following in the class of the objects being spawned:
public boolean isTouchingActor(Actor actor)
{
    return intersects(actor);
}
Then for the while loop, you could have:
Spawned spawned = null; // for actor being spawned (adjust class name)
while (spawned == null || !spawned.isTouchingActor(objectX))
{
    spawned = new Spawned();
   // add spawned into world here
}
With this, the objectX must be in the world and there must be some chance that a spawned actor can be placed so as to touch it. Otherwise, the loop will run indefinitely.
You need to login to post a reply.