Skip to content
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

Merged
merged 5 commits into from
Dec 2, 2022

Conversation

UncertainProd
Copy link
Contributor

This pr adds a simple swap function to FlxArrayUtil, which should be useful for re-ordering sprites like this:

layers.mp4

The code:

package;

import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.text.FlxText;
import flixel.util.FlxArrayUtil;
import flixel.util.FlxColor;

class PlayState extends FlxState
{
	var card1:FlxSprite;
	var card2:FlxSprite;
	var helpText:FlxText;

	override public function create()
	{
		super.create();
		card1 = new FlxSprite().makeGraphic(100, 100, FlxColor.RED);
		card1.screenCenter();
		card1.x -= 20;

		card2 = new FlxSprite().makeGraphic(100, 100, FlxColor.YELLOW);
		card2.screenCenter();
		card2.x += 20;
		card2.y += 20;

		helpText = new FlxText(0, FlxG.height - 20, 0, 'Press ENTER to toggle which card is at the front');
		helpText.setFormat(null, 12, FlxColor.BLACK);
		helpText.screenCenter(X);
		add(helpText);

		FlxG.camera.bgColor = FlxColor.WHITE;
		add(card1);
		add(card2);
	}

	override public function update(elapsed:Float)
	{
		super.update(elapsed);
		if (FlxG.keys.justPressed.ENTER)
		{
			FlxArrayUtil.swap(members, members.indexOf(card1), members.indexOf(card2));
		}
	}
}

@Geokureli
Copy link
Member

just in case you're unaware, FlxArrayUtil is meant to be used via static extension, or by adding using flixel.util.FlxArrayUtil to your imports. by adding a using statement to your example, you could replace

FlxArrayUtil.swap(members, members.indexOf(card1), members.indexOf(card2));

with a simpler line of:

members.swap(members.indexOf(card1), members.indexOf(card2));

Copy link
Member

@Geokureli Geokureli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 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 be swapIndices or swapByIndex. 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>
{
	// ...
}
  1. 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]));

@UncertainProd
Copy link
Contributor Author

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 be swapIndices or swapByIndex

swapByIndex sounds good, I've added functions for swapping based on index and based on the element itself, and added a safe version of them as well which will validate the input elements/indices

All new features in this class should have unit tests, in FlxArrayUtilTest.hx.

I've added in some tests as well. Using haxe test-cpp.hxml on my machine works for me but for some reason test-web.hxml shows an error saying 'ERROR: Local results server appeared to hang so test reporting was cancelled.', maybe it's because I don't have flash installed?

@Geokureli Geokureli merged commit 02c0831 into HaxeFlixel:dev Dec 2, 2022
Geokureli added a commit that referenced this pull request Dec 2, 2022
@UncertainProd UncertainProd deleted the array-swap branch December 2, 2022 18:27
@JOEpapatsk
Copy link

just in case you're unaware, FlxArrayUtil is meant to be used via static extension, or by adding using flixel.util.FlxArrayUtil to your imports. by adding a using statement to your example, you could replace

FlxArrayUtil.swap(members, members.indexOf(card1), members.indexOf(card2));

with a simpler line of:

members.swap(members.indexOf(card1), members.indexOf(card2));

What do you mean adding to your imports? Do you mean adding "using bla.bla.Bla" to the playstate class??

@Geokureli
Copy link
Member

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:
https://github.com/HaxeFlixel/flixel/blob/dev/tests/unit/src/flixel/util/FlxArrayUtilTest.hx#L5

allowing you to use these methods as static extensions rather than static methods, like so:
https://github.com/HaxeFlixel/flixel/blob/dev/tests/unit/src/flixel/util/FlxArrayUtilTest.hx#L88

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants