Closed
Description
- Flixel version: 4.1.0
- OpenFL version: ?.?.?
- Lime version: ?.?.?
- Affected targets:
Code snippet reproducing the issue:
package;
import flixel.FlxState;
class PlayState extends FlxState
{
override public function create():Void
{
var mastergroup:FlxGroup = new FlxGroup();
var subgroup:FlxGroup = new FlxGroup();
var testsprite:FlxSprite = new FlxSprite();
var testsprite2:FlxSprite = new FlxSprite();
var testsprite3:FlxSprite = new FlxSprite();
var testsprite4:FlxSprite = new FlxSprite();
mastergroup.add(testsprite);
mastergroup.add(testsprite2);
mastergroup.add(subgroup);
subgroup.add(testsprite3);
subgroup.add(testsprite4);
mastergroup.forEachOfType(FlxSprite, this.traceSprite, true);
}
public function traceSprite(MySprite:FlxSprite):Void
{
trace(MySprite);
}
Observed behavior:
When setting the recursive flag to true, subgroups of the respective group will still not be searched, despite what the function descriptiuon says. Therefore, only the testsprites 1 and 2 will be traced, the ones in the subgroup will be ignored.
Expected behavior:
Obviously, with Recursive set to true, one would expect 4 traces.
The Problem
if (basic != null && Std.is(basic, ObjectClass))
This line will obviously not be true for a FlxGroup (as long as we are looking for FlxSprite or Strings or anything else not being a FlxGroup)
Suggested Solution
/**
* Applies a function to all members of type Class<K>
*
* @param ObjectClass A class that objects will be checked against before Function is applied, ex: FlxSprite
* @param Function A function that modifies one element at a time
* @param Recurse Whether or not to apply the function to members of subgroups as well
*/
public function forEachOfType<K>(ObjectClass:Class<K>, Function:K->Void, Recurse:Bool = false)
{
var i:Int = 0;
var basic:FlxBasic = null;
while (i < length)
{
basic = members[i++];
if (basic != null && Recurse && Std.is(basic, FlxGroup))
{
var group = resolveGroup(basic);
if (group != null)
{
group.forEachOfType(ObjectClass, cast Function, Recurse);
}
}
if (basic != null && Std.is(basic, ObjectClass))
{
Function(cast basic);
}
}
}
Metadata
Metadata
Assignees
Labels
No labels
Activity