How to use static function in ActionScript 3.0?

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);
//and etc...

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;
}

, , , - .

+3
3

, :

var elements : Array = [obj1, obj2, obj3, obj4];

for each(var element : IEventDispatcher in elements)
{
    element.addEventListener(MouseEvent.MOUSE_OVER, function(e:Event) { moveUp(this); } );
    element.addEventListener(MouseEvent.MOUSE_OUT, function(e:Event) { moveDown(this); });
}

function moveUp(element : UIElement)
{
    element.y += 30;
}

function moveDown(element : UIElement)
{
    element.y -= 30;
}

, , ?

+3

, , "" , :

function moveUp(e:MouseEvent):void
{
    e.currentTarget.y -= 30;
}

function moveDown(e:MouseEvent):void
{
    e.currentTarget.y += 30;
}

, , , , , MOUSE_OUT, , MOUSE_OVER .. :

y = 5 (). ( y = 5). MOUSE_OVER β†’ (y = 35) β†’ MOUSE_OUT β†’ (y = 5) β†’ , y = 5, MOUSE_OVER β†’ .

, , y , , "teletransporting" .

+6

add all the clips you want to listen to in the container:

var container:Sprite = new Sprite;
addChild(container);
// rinse and repeat:
container.addChild(objN);

Then add an event listener to this container:

container.addEventListener(MouseEvent.MOUSE_OVER, handleContainerMouseOver } );
container.addEventListener(MouseEvent.MOUSE_OUT, handleContainerMouseOut });

function handleContainerMouseOver(e:MouseEvent):void{
    e.target.y -= 30;
}

function handleContainerMouseOut(e:MouseEvent):void{
    e.target.y += 30;
}

As a bonus: if you have many objects named sequentially, you can go like this:

for (var i:int = 0; i <= 20; i++) {
    container.addChild(this['obj' + i]);
}

this['obj' + i] obj1, obj2 etc. will be allowed.

+1
source

Source: https://habr.com/ru/post/1725693/


All Articles