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

2016/5/7

Get more than one intersecting object?

redteam55 redteam55

2016/5/7

#
So basically, I have an explosion gif that deals damage to all the actors that it touches. Up until now, all the projectiles or bullets use "getOneIntersectingObject" since they only deal damage to one actor then disappears. But with the explosion, it must deal damage to all intersecting objects. So is there a method similar to getOneIntersectingObject but more than one? so maybe getInterSectingObject? Edit: this is giving me errors GoalMover goalmover = (GoalMover)getIntersectingObjects(GoalMover.class); the error is java.util.ArrayList cannot be cast to GoalMover
Super_Hippo Super_Hippo

2016/5/7

#
You can use the method like this:
for (Object obj : getIntersectingObjects(GoalMover.class))
{
    GoalMover goalmover = (GoalMover) obj;
    //do something with every object in the list
}
redteam55 redteam55

2016/5/7

#
Oh, I've never actually seen the : symbol being used in Java yet haha, alright thanks
redteam55 redteam55

2016/5/7

#
for(GoalMover goalmover : getIntersectingObjects(GoalMover.class)) { goalmover.health = goalmover.health-getDamage(); setDamage(0); //this makes sure it only deals damage once } this is what im using right now but it doesn't seem to work, still only removes the hp of one GoalMover
danpost danpost

2016/5/7

#
redteam55 wrote...
Oh, I've never actually seen the : symbol being used in Java yet haha, alright thanks
It is used in at least three different ways (or types of places). As used above when iterating through a list of objects; after each 'case' in a 'switch' block; and in a expressional 'OR' structure:
// list iterating
for (Object object : list)

// switch block
switch (value)
{
    case 0:  action0(); break;
    case 1:  action1(); break;
}

// expressional 'OR'
value = condition ? trueValue : falseValue;
I think the issue you are dealing with is that either the goalmovers are taking way too much damage because of continuous intersection with the explosion or only the first one takes damage once when using the 'setDamage' method. The problem is due to the length of time the explosion is in the world and that it could intersect new objects while in the world. Maybe you should just list the object that it intersects and when removing the explosion from the world iterate through that list to deal damage to each goalmover listed. You can use the 'contains' List class method to avoid duplicate entries into the list:
if (!list.contains(object)) list.add(object);
redteam55 redteam55

2016/5/7

#
Ah, I don't quite understand the "OR" and "switch" usage yet, but I now understand the first usage, thank you. And it seems that in the end, the problem was, I set the damage to 0 when the list only repeated once, so then only 1 goalmover took full damage, its fixed now. Thank you super_Hippo and danpost!
danpost danpost

2016/5/7

#
WIth explosions, I would usually use the 'getObjectsInRange' method with half the image size as the range:
for (GoalMover goalMover : getObjectsInRange(getImage().getWidth()/2, GoalMover.class))
This would be more realistic as the explosion emanates from the central location and the corners of your explosion image(s) is/are probably transparent.
redteam55 redteam55

2016/5/7

#
Oh... I guess that /would/ make everything easier... thank you for your insight, I'll probably use that in the future instead then.
You need to login to post a reply.