Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
Developer
Posts: 266
Registered: ‎04-01-2009
My Device: Not Specified
Accepted Solution

Object rotation

Hey Guys,

I am trying to rotate an object using:

 

private var object:Image;	
object.rotation = -7;		

 but it rotates it around the 0,0-coordinate but I want to rotate it through the width/2, height/2 coordinate - meaning through the middle of the object. is that possible?

 

Developer
Posts: 863
Registered: ‎10-27-2010
My Device: Z30 / PlayBook 64GB
My Carrier: Virgin Mobile UK

Re: Object rotation

You need to tell it where the rotation axis point is:

 

 

object.rotationX = object.width / 2;
object.rotationY = object.height / 2;

 

I was calling it from a parent object, so I think I had to account for the position on the stage as well... Can't entirely remember but you get the idea.

 


Follow me on Twitter1. If any post helps you please click the Like Button below the post(s) that helped you.
2. Please resolve your thread by marking the post "Solution?" which solved it for you!
Developer
Posts: 409
Registered: ‎12-10-2010
My Device: PlayBook
My Carrier: N/A

Re: Object rotation

[ Edited ]

no, i'm not buying a case for... oh wait... omg!  this is an actual developer question!  *astonished* 

 

setting the registration point for a display object is very possible.  here's an example for setting the registration point in the center of a square graphic.

 

 

var squareSize:uint = 100;

var square:Shape = new Shape();
square.graphics.beginFill(0xFF0000, 1.0);
square.graphics.drawRect(-squareSize / 2, -squareSize / 2, squareSize, squareSize);
square.graphics.endFill();

square.x = stage.stageWidth / 2;
square.y = stage.stageHeigh / 2; 

 

if you don't have access to drawing your object, such an a bitmap which always has a {0, 0} registration, you can place it inside a displayObjectContainer (sprite) and rotate the container:

 

 

var container:Sprite = new Sprite();
container.addChild(object);

object.x = -object.width / 2;
object.y = -object.height / 2;

container.rotaion = -7;

 

if you would rather not use a container to center-register your bitmaps, it's possible to use a Matrix to translate your BitmapData when you draw() it to a Bitmap.  it's a bit more confusing than using a container, but it some cases it's worth it.

 

[EDIT] by the way, my sarcasm wasn't at all directed towards you, bba.  thanks for your question.  seriously.

 


PlayBook Applications:
Drop Swatch
Developer
Posts: 409
Registered: ‎12-10-2010
My Device: PlayBook
My Carrier: N/A

Re: Object rotation

 


cas118 wrote:

You need to tell it where the rotation axis point is:

 

 

object.rotationX = object.width / 2;
object.rotationY = object.height / 2;

 

I was calling it from a parent object, so I think I had to account for the position on the stage as well... Can't entirely remember but you get the idea.

 


this is incorrect.  the code above is setting the rotationX and rotationY values of the object for rotation within a 3D space.  it does not set the object's registration point.

 


PlayBook Applications:
Drop Swatch
Developer
Developer
Posts: 266
Registered: ‎04-01-2009
My Device: Not Specified

Re: Object rotation

HAHA, no problem! The latest messages in this forum are just about shipment and stuff like this Smiley Wink

Developer
Posts: 863
Registered: ‎10-27-2010
My Device: Z30 / PlayBook 64GB
My Carrier: Virgin Mobile UK

Re: Object rotation

Ooops!

Apologies, now that you have put that I realised what I did.

Glad that someone's here to sort out my cockup!

Follow me on Twitter1. If any post helps you please click the Like Button below the post(s) that helped you.
2. Please resolve your thread by marking the post "Solution?" which solved it for you!
Developer
Developer
Posts: 266
Registered: ‎04-01-2009
My Device: Not Specified

Re: Object rotation

Hmm, its still turning "around" the 0,0 somehow.

 

I have an object in the middle of the screen and when I click a button, it should rotate "around itself" and not around 0,0.

 

I create it like this:

 

		private final function createShip():void{
			
			ball = new Image();
			ball.setImage("ship.png");
			container = new Container();
			container.addChild(ball);
			
			ball.x = -ball.width / 2;
			ball.y = -ball.height / 2;
			
			container.rotation = 0;
			container.x = 400;
			container.y = 300;
			
			addChild(container);
		}
		

 

and on click i turn it like this:

 

container.rotation = 10;

 

but then it still turns around the 0,0 ;-(

 

 

Developer
Posts: 29
Registered: ‎11-14-2010
My Device: Playbook

Re: Object rotation

You can also use this class for easy rotation around any point. I used it for my PB app and it works great.

 

http://www.everyday3d.com/blog/index.php/2008/05/28/rotating-a-sprite-around-any-point/

Developer
Posts: 409
Registered: ‎12-10-2010
My Device: PlayBook
My Carrier: N/A

Re: Object rotation

[ Edited ]

do you have a class called Container that extends Sprite?

 

 

ball = new Image();
ball.setImage("ship.png");
container = new Container(); /// new Container ?
container.addChild(ball);

 

if not, just use a plain container Sprite with no dimensions

 

 

ball = new Image();
ball.setImage("ship.png");
container = new Sprite();
container.addChild(ball);

 

the following example doesn't use the QNX Image component, rather i simply embedded an image asset into my code using Flex SDK's Embed metadata tag.  however, i'm pretty sure the QNX Image component should also work by center-registering it within a rotating container.

 

 

package
{
//Imports
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;

//SWF Metadata Tag
[SWF(width = "1024", height = "600", frameRate = "60", backgroundColor = "0xFFFFFF")]

//Class
public class RotateImage extends Sprite
	{
	//Variables
	[Embed (source = "MyImage.jpg")]
	private var MyImage:Class;
	private var container:Sprite;
	
	//Constructor
	public function RotateImage()
		{
		var myImage:DisplayObject = new MyImage();
		
		container = new Sprite();
		container.x = stage.stageWidth / 2;
		container.y = stage.stageHeight / 2;
		container.addChild(myImage);

		myImage.x = -myImage.width / 2;
		myImage.y = -myImage.height / 2;
		
		addChild(container);
		
		addEventListener(Event.ENTER_FRAME, rotateBitmapContainerHandler);
		}
		
	//Rotate Bitmap Container Handler
	private function rotateBitmapContainerHandler(evt:Event):void
		{
		container.rotation += 1;
		}
	}
}

 

[EDIT]  you may have noticed that i didn't use a BitmapAsset to display my .jpg.  it's more common to do so, since it offers the ability, amother other things, to set both pixelSnapping and smoothing of the embedded image, but it isn't required.  BitmapAsset is basically the same thing as the QNX Image component, and similarly i believe you will have to use QNX Image if you want to use the image with other QNX components, such as an icons of a QNX List.

 

 


PlayBook Applications:
Drop Swatch