Copy link to clipboard
Copied
I have a sprite on stage that I want to rotate, The sprite itself contains a shape (a triangle) with these coordinates (10,10)(10,50)(20,30).
When I rotate the sprite (as it is now) using spritename.rotate=nn, it rotates, but around the (0,0) position of the parent container (a movieclip). In an attempt to figure out what was going on, I set up some trace statements, including one to check the width and height of the sprite. It changes with each rotation.
Not sure what is going on here, or why the sprite rotates using the parent mc, but if someone could point me in the right direction, I would appreciate it.
use:
var sp:Sprite = new Sprite();
with(sp.graphics){
beginFill(0xaa0000);
moveTo(0,0);
lineTo(0,40);
lineTo(10,20);
lineTo(0,0);
}
sp.x = -sp.width/2;
sp.y = -sp.height/2;
var p:Sprite = new Sprite();
p.addChild(sp);
p.x = 10+p.width/2;
p.y = 10+p.height/2;
addChild(p);
/*
now p will act like your sprite with its reg point changed to the center. just rotate and otherwise manipulate p instead of sp.
*/
Copy link to clipboard
Copied
what's your sprite's reg point?
Copy link to clipboard
Copied
It's being creating programmatically, so it's empty until the shape is created and added to it. I would use the simple shape, but it need to be able to receive mouse events.
Right now, the shape is created at the coordinates above. Does that affect the sprite's registration point? And is there a way to get that reg. point from flash?
Copy link to clipboard
Copied
yes. your triangle probably starts at 10,10 in your sprite and extends down and to the right.
you want your triangle to rotate about its center or one of its vertices or elsewhere?
Copy link to clipboard
Copied
It would be terrific if I could get it rotate around it's own center.
I think I can fix the one problem by drawing the shape starting at (0,0) and then moving the sprite onto the stage, correct?
Rotating around its own center is a little more of a mystery to me.
Copy link to clipboard
Copied
use:
var sp:Sprite = new Sprite();
with(sp.graphics){
beginFill(0xaa0000);
moveTo(0,0);
lineTo(0,40);
lineTo(10,20);
lineTo(0,0);
}
sp.x = -sp.width/2;
sp.y = -sp.height/2;
var p:Sprite = new Sprite();
p.addChild(sp);
p.x = 10+p.width/2;
p.y = 10+p.height/2;
addChild(p);
/*
now p will act like your sprite with its reg point changed to the center. just rotate and otherwise manipulate p instead of sp.
*/
Copy link to clipboard
Copied
kglad,
Thanks very much for your help. I didn't mean to take so long to reply, I just need some time to put your solution into action. It works perfectly.
One question though: why do you add 10 to the rotating sprite's x and y?
Copy link to clipboard
Copied
because that's where you had your triangle's upper left. if it were at 120,222 then you would add 120 and 222 instead of 10 and 10.
Copy link to clipboard
Copied
Ah, that's what I figured. I started all of the shapes that will appear at 0,0 to fix that problem.
Thanks again.
Copy link to clipboard
Copied
you're welcome.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more