Skip to content

Commit

Permalink
Added util for changing frame indexes safely
Browse files Browse the repository at this point in the history
  • Loading branch information
saturn-volv authored and charlesisfeline committed May 22, 2024
1 parent d77829b commit 848a108
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 11 additions & 0 deletions source/objects/HealthIcon.hx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ class HealthIcon extends FlxSprite
}

private var iconSize:Int = 0;
/**
* An index which decides which frame of the icon to use.
*/
@:isVar public var iconIndex(get, set):Int;
public function get_iconIndex() {
return iconIndex = animation?.curAnim.curFrame ?? 0;
}
public function set_iconIndex(i:Int):Int {
return iconIndex = animation.curAnim.curFrame = (i % iconSize);
}

private var iconOffsets:Array<Float> = [0, 0];
public function changeIcon(char:String, ?allowGPU:Bool = true) {
if(this.char != char) {
Expand Down
4 changes: 2 additions & 2 deletions source/states/PlayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1844,8 +1844,8 @@ class PlayState extends MusicBeatState
var newPercent:Null<Float> = FlxMath.remapToRange(FlxMath.bound(healthBar.valueFunction(), healthBar.bounds.min, healthBar.bounds.max), healthBar.bounds.min, healthBar.bounds.max, 0, 100);
healthBar.percent = (newPercent != null ? newPercent : 0);

iconP1.animation.curAnim.curFrame = (healthBar.percent < 20) ? 1 : 0; //If health is under 20%, change player icon to frame 1 (losing icon), otherwise, frame 0 (normal)
iconP2.animation.curAnim.curFrame = (healthBar.percent > 80) ? 1 : 0; //If health is over 80%, change opponent icon to frame 1 (losing icon), otherwise, frame 0 (normal)
iconP1.iconIndex = (healthBar.percent < 20) ? 1 : 0; //If health is under 20%, change player icon to frame 1 (losing icon), otherwise, frame 0 (normal)
iconP2.iconIndex = (healthBar.percent > 80) ? 1 : 0; //If health is over 80%, change opponent icon to frame 1 (losing icon), otherwise, frame 0 (normal)
return health;
}

Expand Down

0 comments on commit 848a108

Please sign in to comment.