游戏开发 - 查找 img 精灵坐标 Haxe +哈克西夫利塞尔

发布于 2025-01-11 19:34:07 字数 380 浏览 0 评论 0原文

我正在尝试在 haxe 中制作一个战斗系统,我制作了精灵,现在我需要找出它们的 uv 坐标,我该如何实现呢?

我现有代码的示例 :

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);

我尝试过 :

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

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 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

调妓 2025-01-18 19:34:07

Haxeflixel 的精灵片系统不使用 UV 坐标,它假设片中的所有精灵大小相同且间隔一致。

请查看 FlxSprite.loadGraphic 的 API 参考。动画教程使用如下所示的函数:

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

这​​意味着精灵处于 16x16 中网格,并且它们从左到右、从上到下进行索引(最左边的索引为 0)。

以下是坐标如何映射到索引的直观示例:

One row

这是同一个精灵,但有两行而不是一行:

两行

将这些索引与示例代码中的值进行比较:

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);

add 是组成动画的索引数组。

因此,要添加打孔动画,只需将帧放入现有网格中,并使用它们在整个 spritesheet 中的索引来引用它们。

至于实现“战斗系统”,那是一个更复杂的事情。如果您只想播放攻击动画,可以将其设为非循环动画,并使用 回调函数来检测何时结束。示例:

//...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 _:
     }
}

然后您可以检查 is_punching 是否为 true,以防止玩家在出拳时行走,或者在敌人出拳时与玩家碰撞时对敌人造成伤害。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文