-
Notifications
You must be signed in to change notification settings - Fork 438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added a swap function to FlxArrayUtil #2685
Conversation
just in case you're unaware, FlxArrayUtil.swap(members, members.indexOf(card1), members.indexOf(card2)); with a simpler line of: members.swap(members.indexOf(card1), members.indexOf(card2)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I think your example illustrates that there should also be a way to swap elements of an array based on references to those elements rather than their indices. I would call that
swap
, and this should beswapIndices
orswapByIndex
. example:
public static function swap<T>(array:Array<T>, item1:T, item2:T):Array<T>
{
return swapIndices(array, array.indexOf(item1), array.indexOf(item2));
}
public static function swapIndices<T>(array:Array<T>, index1:Int, index2:Int):Array<T>
{
// ...
}
- All new features in this class should have unit tests, in FlxArrayUtilTest.hx. Normally I add these, but this should be pretty easy, I'm the busiest I'll ever be, and you seem to know what your doing.
Check out the unit test readme.md. if you need help testing, let me know.
note that you can do things like:
Assert.isTrue([0, 1, 2, 3, 4, 5].swap(2, 4).equals([0, 1, 5, 3, 4, 2]));
I've added in some tests as well. Using |
What do you mean adding to your imports? Do you mean adding "using bla.bla.Bla" to the playstate class?? |
yeah I mean adding a using statement at the top of the file (where the imports go), like so: allowing you to use these methods as static extensions rather than static methods, like so: |
This pr adds a simple swap function to FlxArrayUtil, which should be useful for re-ordering sprites like this:
layers.mp4
The code: