AS3 2D Sprite Basics - Realtime sprite rotation,scaling, alpha?

Discussion in 'Indie Basics' started by JGOware, Jan 26, 2009.

  1. JGOware

    Indie Author

    Joined:
    Aug 22, 2007
    Messages:
    1,578
    Likes Received:
    0
    I've been doing some basic research on AS3 and 2d sprite support. But I really havn't seen anything about doing realtime rotation,scaling, alpha, additive blending, etc. Is this even possible? Worse...for the best performance it seems pre-rendering the frames and using copypixels() is the preferred way. I havn't had to pre-render sprite frames since my Atari ST days! lol.
     
  2. Backov

    Original Member

    Joined:
    Oct 23, 2005
    Messages:
    812
    Likes Received:
    0
    You can apply transforms to bitmapdata, it's just slower than copypixels, which is a straight rectangular blit (with alpha).

    It's not like it's hard to cache the rotated bitmap (when it changes) using Draw() and a matrix and then blit it with copypixels.
     
    #2 Backov, Jan 26, 2009
    Last edited: Jan 26, 2009
  3. hippocoder

    Indie Author

    Joined:
    Mar 18, 2008
    Messages:
    591
    Likes Received:
    0
    AS3:

    1. transforms are very easy, this is done using bitmapdata.draw() method. You pass a matrix to it (m:Matrix = new Matrix() etc and transform).

    2. It gets slow - fast, when you start using transformations. So think pure "old school" when you're doing your drawing.

    In brief:

    Draw() method will allow full scaling, tinting, skewing and rotation.
    CopyPixels method will draw it as fast as possible without any transforms.

    Both use the CPU for all drawing operations (yes including flash 10), so if you're wanting to do a whizzbang affair you will be dissapointed fairly quickly in terms of speed on older machines.

    Flash 11+ are proposing hardware tnl stuff on the card, however be warned: it's for DX9+ cards only currently. Word is, there's no intention of supporting lesser graphics cards. No-one is sure why that is. It will gracefully fall back to software but I suspect the future changes are a slow but sure move to full screen 3D on the web, not hardware accelerated 2D.

    If you're going 2D you really want to maximise your audiance, and I currently don't advocate flash for full screen action. Indeed you can't even use the keyboard in full-screen games in flash 9, you will need to move to flash 10, and only then can you use non-input keys ie shift, cursors etc.

    I'm just guessing you're thinking of making a fast paced shootemup, but it won't be like whats on your site at the moment :)
     
  4. JGOware

    Indie Author

    Joined:
    Aug 22, 2007
    Messages:
    1,578
    Likes Received:
    0
    Thanks hippocoder for the info. ;) I'm having a very hard time finding a generic hello world type program that just moves a few sprites around the screen in AS3 using the built in Sprite primitive. I have both Flash Develop and the trial version of CS4 installed. (24 days left!) Do you know where there are any basic demos that use the built in sprite primitive? Complete projects, not just snippets of code. Thanks. :)
     
  5. Maupin

    Original Member

    Joined:
    Jun 28, 2006
    Messages:
    1,183
    Likes Received:
    0
    Here's a small Main.as that I wrote in my AS3 experiments. It uses the free mxmlc.exe compiler and creates a Flash movie 640x480 with a simple moving sprite over a background.

    Just save it as Main.as and put it in a folder with two files, image.png and background.png. Not sure if it will work out of the box with Flex because so far I've just been using the command line compiler.

    DOS:\mxmlc.exe -output Game.swf Main.as

    Code:
    package {
    
    import flash.display.*;
    import flash.events.*;
    import mx.core.BitmapAsset;
    
    [SWF(width="640", height="480", frameRate="100")]
    
    // begin program
    
    public class Main extends Sprite {
    
        [Embed(source="image.png")]
        private var GameImage:Class;
        
        [Embed(source="background.png")]
        private var Background:Class;
        
        public var backgroundImg:BitmapAsset;
        public var movingImage:BitmapAsset;
    
        public function Main() {
    
            backgroundImg = new Background();
            addChild(backgroundImg);
            
            movingImage = new GameImage();
            addChild(movingImage);
            
            //attach mainLoop to the enter frame event so it's called every frame
            addEventListener(Event.ENTER_FRAME, mainLoop);
    
        }
        
        public function mainLoop(e:Event):void {
            //game loop
            movingImage.x += 1;
            if (movingImage.x == 640) {
                movingImage.x = 0;
            }
        }
            
    }
    
    // end program
    
    }
     
  6. Backov

    Original Member

    Joined:
    Oct 23, 2005
    Messages:
    812
    Likes Received:
    0
    Don't use the "Sprite" class for a game - the name is deceptive, it's way too heavy. It's also not very performant.

    I can't provide a complete code sample, and am too tired to provide snippets right now, but here's the basics:

    Use Bitmapdata, and either embed your sprite data or load it with the Loader() class.

    Use copypixels for ALL of your blitting. If you want to scale, rotate, what have you, use draw() to draw it into a Bitmapdata cache, and then blit it to the canvas with copypixels. If you composite your sprites, same thing - cache the composite.

    Really, if you have any 2d programming experience at all, getting a 2d sprite engine running with decent performance in Flash should be a couple of hours, max.
     
  7. Alistair Hutton

    Alistair Hutton New Member

    Joined:
    Jun 13, 2007
    Messages:
    60
    Likes Received:
    0
    You want to head over to Kongregate Labs. They have a full basic, game, along with a step by step of how they put it together.

    http://www.kongregate.com/labs

    It is very basic, as it's aimed at people who've never programmed before but as a result it is very clear on all the little pieces of using the Flash tool and it shows some of the little quirks that Flash designers consider standard.

    EDIT: It's also doing everything with MovieClips and the timeline rather than using copyPixel and a full display area bitmap.
     
  8. oldschool

    Original Member

    Joined:
    Jan 23, 2006
    Messages:
    167
    Likes Received:
    0
    #8 oldschool, Jan 27, 2009
    Last edited: Jan 27, 2009
  9. dbest

    dbest New Member

    Joined:
    Oct 31, 2008
    Messages:
    70
    Likes Received:
    0
  10. JGOware

    Indie Author

    Joined:
    Aug 22, 2007
    Messages:
    1,578
    Likes Received:
    0
    Thanks for the info. Kind of weird really... I know 6502,68000,GFA Basic,VB3-VB6 (DX7,8),Php,Javascript,Blitzmax,C++ but this Flash AS3 stuff isn't jumping out at me for some reason. I guess I'm just going to have to shut the doors, dig in and don't come out till I have something up and running. lol. Thanks again for all the links, info, etc.
     

Share This Page

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer