Copy link to clipboard
Copied
Hi
I want to rotate an object using the arrows on the keyboard when the mouse is over the object and it should not be possible to rotate the object when the mouse is not on the object.
I know how to rotate the object, when the mouse is over the object, but when I put the mouse outside the object I can still rotate the object. How do I make the rotation stop?
square1.addEventListener(MouseEvent.MOUSE_OVER, around);
square1.addEventListener(MouseEvent.MOUSE_OUT, normal);
function around(e:MouseEvent):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, rotate);
}
function rotate(event:KeyboardEvent):void {
switch (event.keyCode)
{
case Keyboard.RIGHT:
{
square1.rotation += 90;
break;
}
case Keyboard.LEFT:
{
square1.rotation -=90;
break;
}
}
}
function normal(e:MouseEvent):void {
trace("The object should not be rotating");
}
Thanks
use:
square1.addEventListener(MouseEvent.MOUSE_OVER, around);
square1.addEventListener(MouseEvent.MOUSE_OUT, normal);
function around(e:MouseEvent):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, rotate);
}
function rotate(event:KeyboardEvent):void {
switch (event.keyCode)
{
case Keyboard.RIGHT:
{
square1.rotation += 90;
break;
}
case Keyboard.LEFT:
{
square1.rotation -=90;
break;
}
}
}
function normal(e:MouseEvent):void {
stage.removeEventListener(KeyboardEvent.KEY_DOWN, rotate);
}
Copy link to clipboard
Copied
use:
square1.addEventListener(MouseEvent.MOUSE_OVER, around);
square1.addEventListener(MouseEvent.MOUSE_OUT, normal);
function around(e:MouseEvent):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, rotate);
}
function rotate(event:KeyboardEvent):void {
switch (event.keyCode)
{
case Keyboard.RIGHT:
{
square1.rotation += 90;
break;
}
case Keyboard.LEFT:
{
square1.rotation -=90;
break;
}
}
}
function normal(e:MouseEvent):void {
stage.removeEventListener(KeyboardEvent.KEY_DOWN, rotate);
}
Copy link to clipboard
Copied
It worked!!
Thank you very much![]()
Copy link to clipboard
Copied
you're welcome.
Copy link to clipboard
Copied
You need to remove keyboard event listener from stage in normal function.
Also, case in switch doesn't need curly brackets.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now