-
Trainee coder
[F8] Scaling Bitmaps
Hi,
It's been a while since I've used Flash, and so I'm still stuck in the past trying to get comfortable with AS2.0 again. I'm trying to find out how to scale a bitmap image through ActionScript, but since AS3.0 is so dominant I cant find anything useful through google.
Basically, I'm trying to keep things as dynamic as possible, and so I'd like to use random sizes for my objects (in a game). I've found that to keep the framerate high I'll need to copypixel my objects onto screen, and then wipe them (with fillRect) every frame. This has worked like a charm, but I see no easy way to have objects of varing sizes without manually drawing differently sized 'reference bitmaps', which will take a long time (hence, i'd rather AS do this on start-up)!!
Instead of the above method, I've read in some old threads where Iopred and some of the other oldies were talking about copyPixel'ing into movieclips and doing everything normally (ie. use _xscale/_yscale) but I don't understand how that would work???
Here's a code snippet, if it helps
PHP Code:
_root.onEnterFrame = function():Void {
add = 0; // target frame (value = standard/rest frame);
_root.bmp.fillRect(stageRect, 0);
for (var i = 0; i < allBalls.length; i++) {
//enterFrame () for balls
var b = allBalls[i];
b.lSpan++
b.x += Math.sin((b.lSpan-b.y*5) / 100);
if(b.y<Stage.height) {
b.y += 1;
} else {
b.y = -50;
}
if (b.lSpan%fR==0) { //change frame (move contents) frame-rate (40)/fR
if (_root.add==0) {
if (b.frame < 20) {
b.frame++;
} else if (b.frame > 20) {
b.frame--;
}
} else {
b.frame +=Math.round((_root.add-b.frame)*.5);
}
}
b.rect.x = b.frame * 55; //55 = width of frame
_root.bmp.copyPixels(myBitmapData, b.rect, new Point(b.x,b.y), null, null, true);
// }
}
};
Thanks for any help...
-
M.D.
they mean adding bitmapData's to movieclips, drawing the contents into those bitmapData's then scaling up their container movieClips.
-
Trainee coder
Originally Posted by mr_malee
they mean adding bitmapData's to movieclips, drawing the contents into those bitmapData's then scaling up their container movieClips.
yeah, that's how I understood it; but what I should've said is that I don't understand how this could be quick, since from my understanding the mc's are being re-drawn every frame as per usual(!?)
I did try it out, and my suspisions were confirmed (it was doing 25 fps/40 at 100 objects). Here's some code that I made for your suggested method :
PHP Code:
var bmp = new BitmapData(50, 50, true, 0x00000000); //MUTE
var allBalls:Array = new Array(); // array to remember all balls
var ballCounter:Number = 0; // makes sure each ball gets a new name //MUTE
newBall(100);
function newBall(howMany:Number) {
for (var i = 0; i < howMany; i++) {
var mc:Object = new Object();
mc.x = Math.random()*Stage.width;
mc.g = _root.createEmptyMovieClip("i",i);
mc.g.attachBitmap(bmp, 0);
var gwidth = Math.random()*50 +50;
mc.g._width = gwidth;
mc.g._height = gwidth;
mc.y = Math.random()*Stage.height;
_root.allBalls.push(mc);
mc.lSpan = 0; //timer for life span
mc.frame = 20; //default (normal) frame
mc.rect = new Rectangle(0, 0, 50, 50);
}
};
aswell as changing the end of the previous post's code to this:
PHP Code:
b.rect.x = b.frame * 55; //55 = width of frame
_root.bmp.copyPixels(myBitmapData, b.rect, new Point(0,0), null, null, true);
b.g._x = b.x;
b.g._y = b.y;
I hope it all makes sense. Even though it works, I've probably done something wrong (I'm new to all this bitmapdata stuff, if you can't tell). I'll attach the .fla if needed.
-
M.D.
the only other options to scale a bitmap from memory:
bitmapData.draw using a matrix
graphics.bitmapFill using a matrix
if you really want speed switch to AS3 and use Bitmap objects. Using MovieClips creates unnecessary overhead in both languages. If for some reason you really really... really really need to use AS2, make some array's of scaled bitmapData objects and use copy pixels to print them on screen.
-
Trainee coder
Thanks for your help, malee.
I just read up on using a transformed matrix with draw() as you suggested and it seems like it will answer my initial question. So, at this point I figure at start-up I'll duplicate my gfx bitmap into several increasingly larger/smaller bitmaps, and use copyPixel onto a giant screen sized bitmap using these pre-made bitmaps.
What I don't get is why there was talk in old threads of this movieclip method being better than what I'm doing??? Btw, I'm not using AS3.0 'cause I don't have the money the splash out on upgrading, and this is just a neat little mini-game anyway.
cheers.
-
M.D.
can you link to the threads? I know that in AS3 using Bitmaps can be better than blitting in certain situations. They could also be talking about rendering complex graphics at a smaller scale and then scaling up the container movieclip. Although this produces a more pixelated effect, it dramatically increases rendering performance. For example a realistic shader that simulates water or convolution filter.
-
Trainee coder
This was the thread I skimmed through: http://board.flashkit.com/board/show...ects+particles
There was another one where Iopred was suggesting the same deal with mc's, but I cant find that one atm.
Ages ago I read this one too: http://board.flashkit.com/board/showthread.php?t=732354 and tried my own tests in AS2.0. I found (from slowest to fastest):
1. plain old mc's to be slowest - slooooooooooowww
2. (as I've just found out) copying into mc's - 30fps/40 at 100 balls
3. ScrollRect() - avg 37fps/40 at 100 objects; 15/40 at 200 objects... HOWEVER, this game me the nicest/easiest gfx, since it was all still vector art but cached
4. CopyPixel() - no noticable lag with 200
EDIT: reading through that first link I also see mention of scrolling being a nightmare when using a screensize bitmap to draw to, but I already found a way around that (though it may be a bit cpu intense, hence why I'm holding onto the idea of using mc's if it can perform better)
Last edited by Viza; 09-29-2010 at 09:34 AM.
-
Senior Member
Mc's can't perform better, so you solution is bitmap with transform matrix..... and switch to as3, atleast it will give much better performance compared to as2. Get yourself flashdevelop+flexSDK(tutorial on my blog), both are free, and are perfect for someone interested in programming rather than arts.
If you like me, add me to your friends list .
PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!
My Arcade My Blog
Add me on twitter:
-
Pumpkin Carving 2008
Senocular's Transformation Matrix tutorial will teach you basically everything you could want to know about using the Bitmap in AS2 with the Matrix. Should at the very least point you in the right direction.
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6
-
Trainee coder
Thanks for all the tips guys. @bluemagica: I already use FD, but I haven't properly checked out Flex. Did some reading on it and it definately seems handy. I'm trying to get this game completed during holidays (so I might stick with as2 for now), but I think I'll do some other AS3 learning experiments with Flex. thanks.
Thanks for the link IP. I got it all working now.
Anyone know of a program which can convert a basic tween animation into a spritesheet??? I can probably make something that does it at start-up, but there's no way of keeping a copy of this outside of the .swf this way (ie. i'd like to have a seperate .bmp file for template spritesheets).
-
Pumpkin Carving 2008
Using AIR, it'd be very possible to simply force the animation to play frame-by-frame and copy each frame into a bitmap of a predetermined size and then save that image to the hard drive.
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6
-
Trainee coder
Y'know I was thinking about it, and maybe its best to just keep it all dynamicly created at start-up. Though it has its cons, I think the end result will look better 'cause I'm scaling vectors and then transforming it into bmp's. erghhh, the workload gets bigger and bigger, and I only need this stuff as an extra appeal to graphics (ie. its seperate from the gameplay).
btw IP, you got me email right?
-
Pumpkin Carving 2008
Yep, got your email. Go ahead and shoot those files over to me I'll check that out sometime later.
By the way, are you seriously going with a .bmp file format for your external sprite sheets? I think it'd be in your better interest to use something like .jpg or my personal favorite, .png.
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6
-
Trainee coder
No, I'm using .png's. Just said .bmp 'cause it would be less confusing I suppose.
sent you those files btw
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|