A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: [Discuss][AS3] 2D Game Coding Using CopyPixels(),Draw()

  1. #1
    Member
    Join Date
    Jan 2009
    Location
    Texas
    Posts
    75

    [Discuss][AS3] 2D Game Coding Using CopyPixels(),Draw()

    Collision Checking: I'm planning on adding width/height/radius properties to my sprite class and then just use brute code (circle or box check) to do the collision checking. Is this the preferred way?

    Font Handling: I'm considering just using CopyPixels() to draw some pre-rendered bitmap letters. You don't get any fancy fx with the letters, but it seems easy to do. Is there a better way?

    Screen Fades: Don't have a clue. Is there a way to draw a black square over the entire scene and adjust the alpha so it can fadein or fadeout? Seems like this would be the easiest way? Maybe .Draw() could be useful here?

    .Draw() I've never used this yet. I've read that it can do realtime fx but I haven't found any plain english examples yet how to use this routine. I'm wondering if this would be useful for title screen fx where you might want to rotate, zoom in a logo, etc?

    Thanks for your thoughts.
    Last edited by as3newb; 01-28-2009 at 07:32 PM.

  2. #2
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    You sneaked in more

    Collision Checking:
    All depends. For arena shooters I came up with "Distance based broadphase",
    http://blog.gamingyourway.com/PermaL...e75aaaafa.aspx
    Which is working well here,
    http://www.gywgames.com/xDev/
    If you're looking at uses a lot of sprites which I assume you are, brute force soon gets nasty in Flash, even when using flip flops on your checks to skip frames.

    Font Handling:
    An easier way is to use textFields with effects on them. With the AntiA set to "readability" you even keep great looking text when the game is running in low-quality mode, although at a cost to cpu time.
    But if you want to use a bitmap font, copyPixels is fine.

    Screen Fades:
    Yep alpha. You can use draw() to copy the entire stage, attach that, update the screen behind and then use an alpha fade on that attached grab, giving a simple cross-fade effect.

    Draw()
    Done to death in the live docs. It's slower than copyPixels as it uses the Flash vector plotter rather than hitting bitmap data directly, but it allows copying from any source you like ( MovieClips, flv as well as bitmaps ) and you can pass a matrix along with other params to do more things in one hit ( There is a tipping point where Draw + quite a few different params is quicker than copyPixels, although I don't know what it is off the top of my head ).

    Squize.

  3. #3
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    It sounds like your very "externally" operated.

    I take it you don't have access to Flash CS3? if you did life would be much easier. Flash supports tweening (to much to go into here) but basically you can animate and structure you graphics in the Flash IDE. But without that I would do the following:

    Collision Checking: yes, separate collision from graphic. Use Vector math. In you objects define a collision shape object. i.e AABB, Circle, Polygon. Implement a CollisionManager class which handles collisions using these shape definitions.

    Font Handling: Just use TextField, much simpler. You can embed fonts using code TextFormat is a very powerful class to handle the appearance of text in a textField.

    ScreenFades: Type "AS3 Tween Library" into google . You could write your own tween library, but there is no point really, the ones out there are great for UI stuff. Most support filters (blur, glow, dropShadow) and things like alpha fade and brightness fade. Heaps of ways to animate by code.

    Draw: Draw simply renders an object to a bitmapData using Flash's Vector renderer. So you can do all the stuff you would to a normal Vector shape, like rotation, scale, color change. Draw is also much slower than CopyPixels, CopyPixels just does a simple copy operation, no transformation can be applied other than position.

    In saying all this BitmapData is not the solution for everything In come cases where the size of the image being copied is too large flash will perform badly. This is true for both Draw and CopyPixels. What I recommend is to use the DisplayList. Forget drawing to a bitmapData for everything, you'll end up having more trouble than its worth (MouseEvents!).

    So, create your UI using Sprites and MovieClips, add buttons to them... When playing your game use bitmapData to draw many small sprites on screen. If there are large moving sprites which rotate scale and do all crazy stuff, just use the displayList.

    phew, 3 years in one post.
    Last edited by mr_malee; 01-28-2009 at 07:41 PM.
    lather yourself up with soap - soap arcade

  4. #4
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    We think as one don't we ?

    Squize.

  5. #5
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    like some sort of freaky membrane.
    lather yourself up with soap - soap arcade

  6. #6
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    ...or conjoined twins ( But nice ones, joined at the wrist or something, like a fleshy bracelet, not my head next to your groin or anything nasty like that ).

    Squize.

  7. #7
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    haha, exactly like that.

    ...anyway.
    lather yourself up with soap - soap arcade

  8. #8
    formerly hooligan2001 :) .hooligan's Avatar
    Join Date
    Mar 2008
    Posts
    405
    But nice ones, joined at the wrist or something
    20 fingers and 2 brains. Programmers dream!
    World Arcade | Facebook | Twitter - Follow MEEEE! or not......

  9. #9
    Member
    Join Date
    Jan 2009
    Location
    Texas
    Posts
    75
    Here's a potentially stupid question. Has anyone coded an AS3 game using nothing but copypixels() and draw()? Because that's what I was planning to do as my first test game. Something along the lines of a galaga or asteroids game.

    All graphic assests are png files. (sprites, backgrounds, buttons, fonts, etc)

    Have 2 repeating backgrounds, parallax style.
    Have the main player mouse controlled.
    Have a few dozen ships/asteroids etc zipping around.
    Shoot, bang, explode, etc.
    Fonts, all bitmap, drawn using copypixels.

    For all UI stuff, just loop through my button sprites and see if the mouse is colliding with them if so, react accordingly.

    The whole game done in 1 long main() with vars controlling game state, etc, functions, classes, etc, etc, etc. This is how I code in Blitzmax and it's very easy.

    However...after reading this thread and checking out a few of those tweening libraries, especially "Tweensy", wow..I guess there is superior ways to get even better performance. Some of those Tweensy demos were obviously doing real time rotation,scaling, additive blending, etc, etc. Some demos were drawing 5000 objects with a decent frame rate where my code slows to a crawl with anything over 2000 objects.

    So, I guess I was nuts to think copypixels and draw was actually enough to write games in AS3?

    Added: Also, in this page:

    http://mikegrundvig.blogspot.com/200...n-sprites.html

    He's getting incredible performance. Is he re-drawing the entire background everyframe? If not, perhaps I'm missing something, because how would you use copypixels() if you don't first redraw the entire frame? Wouldn't the old objects still be on screen?
    Last edited by as3newb; 01-29-2009 at 06:55 AM.

  10. #10
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    Jeff at www.8bitrocket.com has the start of an asteroids game using nothing but blitting.

    My asteroids game that I linked to uses attached bitmaps rather than blitting ( As it's quicker, see the gamepoetry link from the other day ) but I still use sprites for things, because to be honest it's silly not to.
    My player ship for example is a sprite, same as the player bullets. The particles are copyPixel blitted.

    You use what's best for that task, you don't need to be fixed rigid on one display system. My game throws a hell of a lot of data around the screen on later levels and doesn't suffer at all.

    As to Mikes experiment ( And all others actually ), ignore them. They don't relate to real life. There are so many experiments with 5000 + particles in them, but you'll never get close to that in an actual game ( To refer to my own stuff yet again, if you look at the 651 demo in my sig you'll see a section with 500 3D balls with a depth of field effect. I couldn't include something like that in a game as all the mainloop is spent just running that effect, plus it uses other tricks which means it's not very robust ).

    Could I just suggest you take a couple of hours out of coding and catch up on some of these links you've been hit with ? I know you've had information overload the past couple of days, but it feels like there's a bit of repetition going on now. Reading is nicer than coding anyway

    Squize.

  11. #11
    Member
    Join Date
    Jan 2009
    Location
    Texas
    Posts
    75
    wow...Squize....those demos kick some major a$$! Kind of really makes my humble codings.....suck. I'm sure alot of coders think they're doing some cool things and then look at demos like yours and they just go...jeesh...I can't compete with that!! lol. But onward I must go...so...

    Couple of questions:

    What resolution should a Flash game be in by the way? (typical sizes,etc)

    When you Stage.AddChild(object) objects added last are the last things drawn on the stage, right?

    For generic fades I added a shape drawrect and then I just manipulate the alpha levels in code. Is this a typical way to do this? It's dog easy.
    Last edited by as3newb; 01-29-2009 at 10:37 PM.

  12. #12
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Quote Originally Posted by as3newb
    What resolution should a Flash game be in by the way? (typical sizes,etc)
    If you want to spread the game into many portals you would want your game to fit there without scaling. 600x400 or 640x480 are popular resolutions. Bigger size makes it of course look better but will also make it slower.

    Quote Originally Posted by as3newb
    When you Stage.AddChild(object) objects added last are the last things drawn on the stage, right?
    Objects added last are drawn on top of older objects meaning they will cover up anything below. To attach object to specific index you can use
    AddChildAt(n)
    where n=0 would attach new object below everything else.

  13. #13
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    "wow...Squize....those demos kick some major a$$!"

    Thanks... but as I said, it's not real life. There are a lot of cheats going on there ( Again most of the effects are covered in the blog, under "experiments" from memory ) so shouldn't be compared to making a game.

    Squize.

  14. #14
    Member
    Join Date
    Jan 2009
    Location
    Texas
    Posts
    75
    Good stuff as usual, thanks for the comments. And btw, I'd like to thank you guys for taking the time to respond to my questions. Very, very cool of you. You don't get that kind of input all the time in the dev world. Nice to see it here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center