简体   繁体   English

HaxeFlixel:将图形叠加到单个精灵中

[英]HaxeFlixel: Overlaying Graphics into Single Sprite

I have code to generate a player avatar spritesheet by pulling each individual body/clothing item and overlaying it in a particular order. 我有代码通过拉动每个单独的身体/服装项目并以特定顺序覆盖它来生成玩家化身spritesheet。 Essentially, I'm trying to combine the clothing sheets into a single body by laying them over each other. 基本上,我试图将衣服片放在一起,将它们放在一起。

But the final output is ONLY the last item of clothing drawn. 但最终的输出只是最后一件衣服。

Here is the two pieces of code I'm using to do it: 这是我用来做的两段代码:

public function new(_username:String) 
{
    super();

    itemArray = new Array<String>();
    itemArray[0] = "Body";
    itemArray[1] = "Shoes";
    this.pixels = new BitmapData(Std.int(itemRect.width), Std.int(itemRect.height));

    for (itemName in itemArray)
    {
        //this.pixels.draw(prepareItem(itemName).pixels);
        var itemSprite:FlxSprite = prepareItem(itemName);
        stamp(itemSprite);
    }
}

private function prepareItem(assetName:String):FlxSprite
{
    var assetSprite:FlxSprite = new FlxSprite();
    assetSprite.loadGraphic("assets/images/" + assetName + ".png");

    assetSprite.pixels.threshold(assetSprite.pixels, itemRect, new Point(0, 0), "==", 0xFF00FF00);
    assetSprite.pixels.threshold(assetSprite.pixels, itemRect, new Point(0, 0), "==", 0xFFFF0000);

    return assetSprite;
}

As you can see, I've tried using draw() to draw pixels onto the existing BitmapData , and I'm also trying to stamp sprites generated from the clothes images. 正如你所看到的,我已经尝试使用draw()将像素绘制到现有的BitmapData ,而且我还试图标记从衣服图像生成的精灵。

I'm attaching an example to clarify my goal. 我附上一个例子来澄清我的目标。 Notice how the shoes are layered OVER the body. 注意鞋子是如何在身体上分层的。 But with my current code, the shoes replace the body entirely. 但是根据我目前的代码,鞋子完全取代了身体。

例

身体 鞋

But the final output is ONLY the last item of clothing drawn. 但最终的输出只是最后一件衣服。

With your code, I'm actually seeing the two assets be drawn on top of each other - I guess that might not be noticable depending on the assets you were using. 与您的代码,我实际上看到的两种资产来彼此顶部绘制的-我想这取决于你使用的资产可能不是noticable。

The fix is simple: you need to offset subsequent sprites in your stamp() call, eg like this if you want to stack the assets vertically: 修复很简单:你需要在stamp()调用中偏移后续的精灵,例如,如果你想垂直堆叠资源,就像这样:

var currentY = 0;
for (itemName in itemArray)
{
    var itemSprite:FlxSprite = prepareItem(itemName);
    stamp(itemSprite, 0, currentY);
    currentY += Std.int(itemSprite.height);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM