Wednesday, July 17, 2019

actionscript 3 - Rotation of objects from different axes in Flash AS3


In my game, I have made 4 lines with different colors to form a square but the colored lines are different objects. I want to rotate the lines in a way that the lines will still form a square. Eg: enter image description here


But I'm only able to rotate them on the central axis, so, it forms a cross: enter image description here


The code for leftPressed is:


if(leftPressed)
{
black.rotation -= 90;
blue.rotation -= 90;
yellow.rotation -= 90;
red.rotation -= 90;

}

So, I need to know how to rotate objects on different axes.



Answer



addChild() each line into a new parent DisplayObject called square. Then rotate square instead of each of the children, individually. Flash will handle the correct rotation of the children therein.


Do this once:


var square:DisplayObject = new Sprite(); //Can't instantiate DisplayObject - it's abstract. So use Sprite.

//you'll need to get references to each of the lines used here (left to you):
square.addChild(black);

square.addChild(blue);
square.addChild(yellow);
square.addChild(red);

Do this every update:


if (leftPressed)
{
square.rotation -= 90;
}

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...