HaxeFlixel / HaxeFlixel/flixel

FlxBar rendering issue when wanting to have it follow a parent, but not set a parentVariable

Open
#2,289 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Haxe
Stars
2.2k
Forks
522
Avg merge
34m
Merged PRs (30d)
1

Description

  • Haxe version: 4.1.4
  • Flixel version: 4.8.1
  • Lime version: 7.8.0
  • Affected targets: macOS 10.15.6

There is a bug in FlxBar where the bar being drawn renders with the "empty" color value and doesn't update if you set a parent FlxSprite object for it to follow, but no 'parentVariable'. This occurs due to the undefined behavior of FlxBar.update's Reflect.getProperty(parent, parentVariable) check when no parentVariable is set.

E.g.: In the videos/project below I have an Enemy HP bar that uses Red for the fill, and Black for the empty colors, but with the FlxBar issue the entire bar always renders Black and doesn't update its value (although it will still follow the parent position).

Old code in FlxBar.hx:

	override public function update(elapsed:Float):Void {
		if (parent != null) {
			if (Reflect.getProperty(parent, parentVariable) != value) {  // <-- This is the offending line
				updateValueFromParent();
			}

			if (!fixedPosition) {
				x = parent.x + positionOffset.x;
				y = parent.y + positionOffset.y;
			}
		}

		super.update(elapsed);
	}

New code in FlxBar.hx with a fix:

	override public function update(elapsed:Float):Void {
		if (parent != null) {
			if (Reflect.hasField(parent, parentVariable) && Reflect.getProperty(parent, parentVariable) != value) {
				updateValueFromParent();
			}

			if (!fixedPosition) {
				x = parent.x + positionOffset.x;
				y = parent.y + positionOffset.y;
			}
		}

		super.update(elapsed);
	}

Repro steps:

Running lime test html5 with my specialized side-branch of my game project can reproduce the issue, as shown with the videos below:

https://github.com/AlejandorLazaro/MiniQuestHaxe/tree/testing/FlxBarRenderingIssueWithATrackedParentButNoParentVariable

An example snippet that shows how this would reproduce in another project is here:

// Within a class that `extends FlxTypedGroup<FlxSprite>`

	public function addNewEnemyHealthBar(enemy:Enemy)
	{
		enemyHealthBar = new FlxBar(0, 0, LEFT_TO_RIGHT, 12, 3);
		// I didn't want to use 'setParent' because it forces the use of a variable to be tracked,
		// but I only wanted the bar to follow an FlxSprite instead, while being in full control
		// of the actual bar values separately. (e.g.: freely increasing the max HP value)
		// enemyHealthBar.setParent(enemy, "", true, 0, 0);
		enemyHealthBar.parent = enemy;
		enemyHealthBar.trackParent(0, 0);
		enemyHealthBar.value = 100; // the enemySprite's health bar starts at 100%
		enemyHealthBar.killOnEmpty = true;
		enemyHealthBar.createFilledBar(FlxColor.BLACK, FlxColor.RED, true, FlxColor.WHITE);
		add(enemyHealthBar);

Video showing old FlxBar bad rendering behavior:

https://user-images.githubusercontent.com/1635011/104145620-97a2a180-537c-11eb-9f7a-ef257f3a1848.mov

Video showing new FlxBar behavior with a new Reflect.hasField(parent, parentVariable) check:

https://user-images.githubusercontent.com/1635011/104145754-126bbc80-537d-11eb-9518-ad487ab49e08.mov

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in FlxBar.hx and inspect update(), then reproduce the behavior with the supplied example using lime test html5. Verify the bar can follow a parent without a parentVariable while retaining its configured colors and value updates, and confirm the existing parent-tracking behavior still works.

Written by the indexing model from the issue text.

Assessment

Domain
game-dev
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.