'Game dev -Finding img sprites coords Haxe + Haxeflixel

I'm trying to make a combat system in haxe I made my sprites and now I need to find out the uv coordinates for them how do I achieve that ?

Example from my existing code :

animation.add("lr", [3, 4, 3, 5], 6, false);
animation.add("u", [6, 7, 6, 8], 6, false);
animation.add("d", [0, 1, 0, 2], 6, false);

I tried :

if (FlxG.mouse.justPressed) {
    // Attack code here 
}


Solution 1:[1]

Haxeflixel's sprite sheet system doesn't use UV coordinates, it assumes all sprites in the sheet are the same size and consistently spaced apart.

Look at the API reference for FlxSprite.loadGraphic. The animation tutorial uses that function like this:

loadGraphic(AssetPaths.player__png, true, 16, 16);

This means the sprites are in a 16x16 grid, and they're indexed from left to right, top to bottom (with the left-most being index 0).

Here's a visual example of how the coordinates map to indices:

One row

And here's that same sprite, but with two rows instead of one:

Two rows

Compare those indices to the values in the sample code:

animation.add("lr", [3, 4, 3, 5], 6, false);
animation.add("u", [6, 7, 6, 8], 6, false);
animation.add("d", [0, 1, 0, 2], 6, false);

The second parameter to add is an array of indices that make up the animation.

So to add a punch animation, just put the frames into the existing grid, and reference them using their indices in the overall spritesheet.

As far as implementing a "combat system", that's a more involved thing. If you just want to play an attack animation, you can make it a non-looping animation, and use a callback function to detect when it ends. Example:

//...defined somewhere in your sprite class
var is_punching:Bool = false;

//...somewhere inside your constructor maybe
animation.finishCallback = function(name:String){
    switch(name){
        case "punch-u", "punch-lr", "punch-d":
            is_punching = false;
        default:
            trace("Some other animation finished: " + name);
    }
}

//...somewhere in your update function
if (FlxG.mouse.justPressed && !is_punching) {
    is_punching = true;
     switch (facing)
     {
         case LEFT, RIGHT:
             animation.play("punch-lr");
         case UP:
             animation.play("punch-u");
         case DOWN:
             animation.play("punch-d");
         case _:
     }
}

Then you can check if is_punching is true to prevent the player from walking while punching, or to damage an enemy if they collide with the player while they're punching.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user16978892