PDA

View Full Version : [AS3] Bitmap Draw Strategy



handofra
July 9th, 2009, 04:44 PM
Hello,

I'm making a game where it involves a player moving around a big map. A camera will follow the player around, and reveal only a small section of the map (think RTS).

I'm planning to add the player, enemies, items, background, etc into one Sprite object, and do collision checks within the Sprite (with HitTest). This Sprite object will not be drawn to the stage. Then, I will use BitmapData to copyPixels the part of the Sprite that the camera focuses on. This bitmap will be drawn on the stage.

Is this an efficient way of drawing part of a big map unto the screen?

Thanks.

mxrider108
July 9th, 2009, 05:26 PM
That would probably work. For my game I came up with a size for my "arena" and do all of my game logic with x and y values that are relative to this arena and not actually the stage. Then I created a Camera class that also has a set of x, y coordinates and moves to follow my main player (with easing).

What makes it work is that the rest of my objects extend a RenderObject class which has a global render() method that subtracts the camera coordinates to come up with actual Flash stage coordinates. To prevent having to render offscreen objects is simple, I just check if the object's x or y exceeds the camera's x or y (plus or minus the stage width/height depending on which side) and set a boolean.

The other positive thing about a system such as this is it is *really* helpful in separating your game logic from rendering. Let me know if you need any help!

flyingmonkey456
July 9th, 2009, 06:57 PM
use vcam, it's awesome. here's an as2 link:
http://www.oreillynet.com/pub/a/javascript/2004/08/17/flashhacks.html?page=last&x-showcontent=text
and as3:
http://bryanheisey.com/blog/?p=1

the as3 link has a good demonstration of it's capabilities. this is definitely the easiest way to simulate a camera. i'm not sure if it's efficient for games because it was developed for movies, so you'd have to ask someone else on that :)

therobot
July 9th, 2009, 07:23 PM
vcam

You should be able to write your own camera class that will be more efficient than vcam, as vcam may have some features you probably don't need.

Also, I don't know if vcam works with bitmapdata. There are some tutorials for setting up a camera type system using bitmapdata, namely:

http://www.8bitrocket.com/newsdisplay.aspx?newspage=17171

Essentially you have to figure out where your "camera" is, how big its viewing area is, and only copy the tiles that will appear within its viewing area, using offsets.

TOdorus
July 9th, 2009, 07:23 PM
Is this an efficient way of drawing part of a big map unto the screen?

Quite, but there might be a little bump.

Like mxrider said this is a good way to seperate model from view. I can advise you to always try to do this. The problem with how you described your method, is that you want to use copypixel(). A very very cheap method so that's a good idea. The copypixel method cannot be used on movieClips or sprites, but only on bitmapData's. You'd have to use draw() to copy from a movieClip (dunno if that method works on sprites).

As far as I can come up with you've got two options:
Do all the game logic and then use one draw to draw from the sprite to the bitmapData (I don't really think this is cheaper then just using sprites directly). Have a seperate set of bitmapdata's that function as a spritesheet to copypixel from and render all the visible objects to the bitmapdata.

bluemagica
July 9th, 2009, 07:38 PM
The first thing is, I am not sure you will be able to do "hitTest", when your "sprite" is not on stage(hence not directly in memory)! And If you do load the entire thing in memory(not show on stage, but keep in memory), then copyPixel would only be a overhead cause, it will be eating the same amount of memory as if it were on stage(well, cpu usage will be lower cause it isn't rendering the entire thing)!

TOdorus
July 9th, 2009, 08:33 PM
The first thing is, I am not sure you will be able to do "hitTest", when your "sprite" is not on stage(hence not directly in memory)! And If you do load the entire thing in memory(not show on stage, but keep in memory), then copyPixel would only be a overhead cause, it will be eating the same amount of memory as if it were on stage(well, cpu usage will be lower cause it isn't rendering the entire thing)!

The gain actually is in cpu usage. That's a advantage of seperating model from view. You can use effecient methods for either and don't have to compromise between the demands of each. Using copypixel this way, you can quite easily render a few thousand objects onscreen while doing hittesting between them. Such a speed gain I wouldn't quite consider extra overhead.

bluemagica
July 9th, 2009, 08:53 PM
well, i would say there won't be a overhead, if the hitTest was done with the bitmapData rather than the mc, or if it was a distance check or something like that! But otherwise, there will still be some overhead!

handofra
July 10th, 2009, 12:12 AM
Hey,

Thanks for the replies guys! Apparently right after I posted my idea, I realize that what I proposed is not possible (I cannot draw a portion of a Sprite into BitmapData). However, I just created new system that separates model from view like TOdorus said. Basically my camera is a Rectangle that check intersection with the bounding box of objects in my model to decide if it is needed to be drawn. Then my CanvasData (which is the BitmapData that is drawn on my stage) does some copyPixels from my objects' BitmapData and place them relative to the camera's position.

Another thing is that my game loop is based on Jeff Fulton's tutorial on creating an optimized game loop (http://www.8bitrocket.com/newsdisplay.aspx?newspage=10248). A problem is that my stage size is 800x600 (too big?), and drawing the BitmapData on that size freezes my player. Anything beyond 640x480 will guarantee a freeze, while below it will not. Is there a limit to how fast you can draw bitmaps at certain size?

TOdorus
July 10th, 2009, 12:41 AM
Probably depends on the amount of copypixels and very slightly on the size of the data bieng copied. But it's got to be something else in this case, as I'm using a bitmap of 700 by 510 pixels (I have NO idea why I took that size). Could you post a swf to see if it's your player?