FOSS: Recording w/ BitmapData
FOSS: Main
What we’re trying to do…
Is make a pseudo-recording of the stage, and play it back when you want it to stop recording. This can be done in various methods, the best way would be filling up an array with coordinates of each object.
NOTE: This shouldn’t be used for gaming, it’ll cause trouble
Think about it this way, each pixel requires 4 bits of ram for each channel (R,G,B,A). That sounds like nothing, but if the stage is 550x400 (default), that’s 550x400x4 bits, which is about 107kb. If you’re running this at 24 FPS and record 10 seconds, you’re wasting about 50 MB of RAM! Forgive my RAM conversion in the fla, I realize it’s wrong.
Code & Explanation
First and foremost, we have to import some important classes, BitmapData being the only one we’re actually going to use. The rest are for passing required arguments that need those classes.
import flash.display.BitmapData
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.geom.ColorTransform;
I also import the filters.BlurFilter class to add some effect to our little square.
Here I have a little function that creates a movieclip, draws the stage, and stores it far away for us to playback later. Looping through all the pixels to get their values to store in an array would lag a lot, and take up the same amount of RAM.
createBitmap = function(){
var bmc = createEmptyMovieClip("Bm_Mc"+d(),d())
var bm = new BitmapData(w,h,true)
bmc.attachBitmap(bm,bmc.d())
bm.draw(_root, new Matrix(), new ColorTransform(), false, new Rectangle(0,0,w,h))
//Store far away
bmc._x = bmc._y = -1300
return bmc
}
Explanation… it creates a movieclip, creates a BitmapData that attaches to the MC, the BitmapData draws the stage, and it moves the movieclip away from the screen. You also notice I used d(), I just don’t feel like typing getNextHighestDepth() 20,000 times, and I sure as hell wasn’t going to stoop to making currentDepth++ or some shit like that, so I created a little function that you have to type 3 characters for.
Object.prototype.d = function() { return this.getNextHighestDepth() }
I also made some constant vars just for simplicity
w = Stage.width; h = Stage.height
Now, to record and playback. I made it so when you click the mouse, it starts recording and when you click again it plays back. You can change to keys or whatever the fuck suits you, I don’t care.
onMouseDown = function(){
recording = !recording
recording ? frames=[] : null
playback = !recording
playback?index=0:null
}
What that says:
Recording = (is recording false)
If recording, frames=[]
Playback = (is recording false)
If playback, index=0
Okay, now that we got that down, we need to say what happens when recording is true, or when playback is true. So…
Object.prototype.cleanup = function() { this.bm.dispose(); this.removeMovieClip() }
onEnterFrame = function() {
if(recording) frames.push(createBitmap())
if(playback){
prevmc.cleanup()
var len = frames.length
playback=index!=len
var mc = prevmc = frames[index]
mc._x = mc._y = 0
index++
}
}
If it’s recording, it pushes into the frame array, a movieclip with the drawing for that frame.
For playback, it first calls the function dispose, which removes the last MC it created, so it can free up that RAM we love. It gets the constant len(constant for the duration of that playback) and gets the movieclip from the array. It puts the movieclip in it’s origin, so it can show it fully, then ups the index for us to see the next frame when the time comes.
Source code & conclusion
Like I said, this is not a good method to make recording for games, it is just a little experiment I thought I’d share.
Source code: http://www.fweflash...lash/movieRecord.fla
Comments, questions, whatever, don’t hesitate (unless you’re NC, I won’t take your suggestions and won’t care for whatever little smart ass thing you have to say,) thanks.