Hi all,
my first post here, I hope you'll be able to help me with my problem.
I'm making a simple application that takes users input (text), formats it the way user specified, and displays text on stage. Later on, user can move (drag & drop) text all over the stage and can rotate it as well. The problem came with this rotation functionality. After user clicks Generate button, I create a TextField and place it into a MovieClip. When I try to rotate the MovieClip, it suddenly dissapears. I can't understand why this is so, 'cause I can access (and set) the .x and .y properties without a problem, but .rotation gives me problems. Later on I realised that it's not the MovieClip (the "container") that reacts on different events, it's the TextField inside it that reacts to MOUSE_DOWN/UP and other events. Why is this so? Anyway, here's the code:
Code:
function generateMC(event:MouseEvent):void {
if(input.text != "" && color != "" && cbFonts.selectedIndex > -1 && cbSize.selectedIndex > -1){
var myFormat:TextFormat = new TextFormat();
myFormat.font = font;
myFormat.size = size;
myFormat.color = color;
var container:MovieClip = new MovieClip();
container.x = 100;
container.y = 300;
var writing:TextField = new TextField();
writing.text = input.text;
writing.setTextFormat(myFormat);
writing.autoSize = TextFieldAutoSize.LEFT;
writing.selectable = false;
container.addChild(writing);
stage.addChild(container);
container.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
container.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
stage.addEventListener(MouseEvent.MOUSE_UP, stopRotating);
function startDragging(event:MouseEvent):void
{
offsetX = event.stageX - container.x;
offsetY = event.stageY - container.y;
localX = container.mouseX;
if(localX > (container.width * 0.9)){ /* if the mouse was pressed at the last 10% of containers width */
rotateBool = true;
stage.addEventListener(MouseEvent.MOUSE_MOVE, rotateContainer);
} else {
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragContainer);
}
}
function rotateContainer(event:MouseEvent):void {
container.rotation += 2; /* this isn't working! Why?
event.updateAfterEvent();
}
function stopRotating(event:MouseEvent):void
{
if(rotateBool == true){
rotateBool = false;
stage.removeEventListener(MouseEvent.MOUSE_MOVE, rotateContainer);
}
}
function dragContainer(event:MouseEvent):void
{
container.x = correctX(event.stageX - offsetX, container.width); /* functions correctX/Y are used to prevent the user from dragging mc out of stage */
container.y = correctY(event.stageY - offsetY, container.height);
event.updateAfterEvent();
}
function stopDragging(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragContainer);
}
}
}
Thanx, hope I was clear. Cheers from Croatia!
Ivan
p.s. this is a very cool site/forum, I think it's a great resource for all level AS programmers, so keep up the good work!