Search code examples
canvasfor-looptilesbitmapdata

AS3 Bitmapdata Canvas spritesheet loader help needed


I'm very new to AS3 and I don't really know all of the basic syntax yet, even though I read through some guides every day. I am using this bitmapdata canvas to load spritesheets onto a canvas, and using a foor loop, I've tried to get the map to populate with tiles but I've had no luck.

        for (var i:int = 0; i < columns; i++)
        {
            for (var j:int = 0; j < rows; j++)
            {
                smb3SpriteSheet = new SMB3(new SMB3SpriteSheet(), smb3XML.animation, 175);
                smb3SpriteSheet.x = startX;
                smb3SpriteSheet.y = startY;
                canvas.addSprite(smb3SpriteSheet);
                startX = startX + 16
            }
            startX = 0
            startY = startY + 16
        }

This code is supposed to create a new bitmap data that it'll add onto a blank background bitmap, setup the x and y coordinates and then move down a space and repeat, but when I compile, the only tile that appears is the last tile. I have looked online but there is no information that I could find about using bitmap data in this way. I am supposed to use it for the assignment.


Solution

  • I don't know what SMB3 class should do, but i think you are receiving the last image only because iterating over sprite sheets you call each time the same canvas into you are trying to draw. So each time you call the canvas, actually you are replacing the current content with the new one, even if you are creating new SMB3. You need to consider that a display object cannot have more than a parent.

    So to make your code working either you use a separate Sprite for each image, which is absolutely not recommended, or to use the BitmapData and perform the image drawing of the canvas, and only after that assigning the Bitmap to the Sprite.

    You have a few methods for this operation. You can use BitmapData's copyPixels, draw or clone methods.

    Here are two resources which are related to your issue:

    Adding multiple instances of a Sprite?

    Show another instance of a sprite