Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411576 Posts in 69386 Topics- by 58445 Members - Latest Member: Mansreign

May 05, 2024, 10:38:46 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Centering bitmap graphics in as3
Pages: [1]
Print
Author Topic: Centering bitmap graphics in as3  (Read 4091 times)
G.I.L.
Guest
« on: August 23, 2009, 08:01:17 AM »

Just wondering if its possible. Thanks in advance.
Logged
bateleur
Level 10
*****



View Profile
« Reply #1 on: August 23, 2009, 08:12:54 AM »

Your question isn't very clear!

I mean sure, you can centre bitmap graphics in the sense that you can put them anywhere you like on the screen. But presumably that's not what you meant?
Logged

G.I.L.
Guest
« Reply #2 on: August 23, 2009, 08:17:05 AM »

Sorry I mean centering as in rotating bitmaps from the center instead of the top left corner
Logged
bateleur
Level 10
*****



View Profile
« Reply #3 on: August 23, 2009, 08:29:17 AM »

Just add it to another (empty) DisplayObject and then translate it until it's origin is at (0,0) in its parent's coordinate system. Then apply rotations to the parent.
Logged

G.I.L.
Guest
« Reply #4 on: August 23, 2009, 08:47:12 AM »

sorry being a noob to flash I don;t quite understand it. May I see code?
Logged
weasello
Level 1
*



View Profile WWW
« Reply #5 on: August 23, 2009, 10:07:27 AM »

Easiest way I've found: Just make your bitmap's top-left coordinate in flash 0,0. That's how I manage it in all my flash games.

if that doesn't work for you, you might be able to do something with:

image.x = image.width/2;
image.y = image.height/2;

but I'm not sure if that would work right.
Logged

IndieElite4Eva
John Nesky
Level 10
*****


aka shaktool


View Profile WWW
« Reply #6 on: August 23, 2009, 10:14:53 AM »

Code:
var sprite: Sprite = new Sprite();
sprite.addChild(myBitmap);
myBitmap.x = -myBitmap.width/2
myBitmap.y = -myBitmap.height/2
sprite.rotation = 42;
addChild(sprite);
Logged
Titch
Level 3
***


Cautiously Pragmatic


View Profile WWW
« Reply #7 on: August 24, 2009, 01:25:22 AM »

The other way of rotating a bitmap around a center is to use a translation matrix to move it, rotate it and then move it back again.

I used this tutorial as a referance to do it using matrix. The important part is this:

Code:
	var degrees:int=charObj.rotation;
var angle_in_radians = Math.PI * 2 * (charObj.rotation / 360);
var rotationMatrix:Matrix = new Matrix();
rotationMatrix.translate(-16,-16);
rotationMatrix.rotate(angle_in_radians);
rotationMatrix.translate(16,16);
var matrixImage:BitmapData = new BitmapData(32, 32, true, 0x00000000);
matrixImage.draw(charObj.sourceBD, rotationMatrix);

You get the rotation in degrees that you want to turn, convert it into radians. Create a new rotation matrix based on that angle. Before you rotate the sprite you translate (move) it by 1/2 it's width a height so the centre of the sprite sits in the top left of the canvas you are rotating. Once you've applied the rotation matrix you move the whole thing back.

Although, to be honest this whole method is probably more costly than just making it a sprite. So unless there is a desperate need to keep an object as a bitmap (ie, you are accsessing the bitmapData every frame and don't want to call another flash render function) then you should probably just nest it inside a sprite or movie clip object as suggested above.
Logged

grapefrukt
Level 1
*



View Profile WWW
« Reply #8 on: August 24, 2009, 03:23:06 AM »

i've had to use a translation matrix on occasion and it's WAY slower than just wrapping it in an extra sprite. so stay with the simple version if you can.
Logged
G.I.L.
Guest
« Reply #9 on: August 24, 2009, 06:53:34 AM »

the problem is a only have flashdevelop and can not make swf files...
but I will try the translation method thx
Logged
grapefrukt
Level 1
*



View Profile WWW
« Reply #10 on: August 24, 2009, 09:35:22 AM »

you can most certainly do this the clever way.!

attach your bitmap to a private class and add it as a child instead:
Code:
package  {

import flash.display.Bitmap;
import flash.display.Sprite;

public class Enemy extends Sprite {

[Embed(source='gardengnome.png')]
private static const GfxGnome:Class;
private var gfx:Bitmap;

public function Enemy() {
gfx = new Enemy.GfxGnome();
gfx.x = -gfx.width / 2;
gfx.y = -gfx.height / 2;
addChild(gfx);
}

}

}

this will, as i said, be faster and easier to handle.

i did a bigger writeup on this on my blog http://prototyprally.com/mgas3fd-4-more-embedding/
Logged
Titch
Level 3
***


Cautiously Pragmatic


View Profile WWW
« Reply #11 on: August 25, 2009, 05:24:59 AM »

the problem is a only have flashdevelop and can not make swf files...
but I will try the translation method thx

I only have FlashDevelop and I would -still- wrap my bitmap asset in a sprite class rather than try rotate it using a translation matrix. As I said in the previous post, it's only a method you want to use if you need to access the raw bitmap data pixels after rotating them.
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic