Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Rotating Sprite

New Here ,
Oct 01, 2010 Oct 01, 2010

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.

TOPICS
ActionScript
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 01, 2010 Oct 01, 2010

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.

*/

Translate
Community Expert ,
Oct 01, 2010 Oct 01, 2010

what's your sprite's reg point?


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 01, 2010 Oct 01, 2010

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 01, 2010 Oct 01, 2010

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 01, 2010 Oct 01, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 01, 2010 Oct 01, 2010

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.

*/

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 05, 2010 Oct 05, 2010

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 05, 2010 Oct 05, 2010

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 06, 2010 Oct 06, 2010

Ah, that's what I figured.  I started all of the shapes that will appear at 0,0 to fix that problem.

Thanks again.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 06, 2010 Oct 06, 2010
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines