I would like to have several objects (for example, 20 of them), every time I hover over any of them, it moves up, and every time my mouse leaves, it moves down.
obj1.addEventListener(MouseEvent.MOUSE_OVER, moveMyself1);
obj1.addEventListener(MouseEvent.MOUSE_OUT, moveMyself2);
obj2.addEventListener(MouseEvent.MOUSE_OVER, moveMyself1);
obj2.addEventListener(MouseEvent.MOUSE_OUT, moveMyself2);
obj3.addEventListener(MouseEvent.MOUSE_OVER, moveMyself1);
obj3.addEventListener(MouseEvent.MOUSE_OUT, moveMyself2);
obj4.addEventListener(MouseEvent.MOUSE_OVER, moveMyself1);
obj4.addEventListener(MouseEvent.MOUSE_OUT, moveMyself2);
obj5.addEventListener(MouseEvent.MOUSE_OVER, moveMyself1);
obj5.addEventListener(MouseEvent.MOUSE_OUT, moveMyself2);
function moveMyself1(e:MouseEvent):void{
obj1.y -= 30;
}
function moveMyself2(e:MouseEvent):void{
obj1.y += 30;
}
I do not want to add an event listener for each of the objects, then I will have 40 methods! Is there a way to write a static method so that I can use for all objects?
And I realized that the object is moving up and down too fast. If you try to hover over the lower end of an object, you will see that it bounces very quickly. Is there a way I can control the speed of an object?
, . . . , . , . , ?
var elements : Array = new Array();
var elements2 : Array = new Array();
for (var i:int = 1; i <= 5; i++) {
elements[i] = this['obj' + i];
elements2[i] = this['tracking' + i];
}
for each(var element_1 : IEventDispatcher in elements){
element_1.addEventListener(MouseEvent.MOUSE_OVER, moveUp);
}
for each(var element_2 : IEventDispatcher in elements2){
element_2.addEventListener(MouseEvent.MOUSE_OUT, moveDown);
}
function moveUp(e:MouseEvent):void{
e.currentTarget.y -= 30;
}
function moveDown(e:MouseEvent):void{
elements[elements2.indexOf(e.currentTarget)].y += 30;
}
, , , - .