as3 - перемещение объектов через разные позиции - PullRequest
0 голосов
/ 26 февраля 2020

Я пишу простую видеоигру в Adobe Animate с as3. Я пытаюсь переместить мои объекты с помощью действия смахивания в 3 различных положениях, например:

Swipe 1: mc1 moves to 1st position (x = 600; y = 300).
Swipe 2: mc2 moves to 1st position (x = 600; y = 300) and mc1 move to 2nd position (x = 300, y = 300).
Swipe 3: mc3 moves to 1st position (x = 600; y = 300) and mc2 move to 2nd position (x = 300, y = 300) and mc1 moved to 3rd position ((x = -100, y = -100).
Swipe 4: mc4 moves to 1st position (x = 600; y = 300) and mc3 move to 2nd position (x = 300, y = 300) and mc2 moved to 3rd position ((x = -100, y = -100)...and so on. 

Я также хотел бы иметь возможность вернуться к началу l oop (Размах 1) через нажмите, чтобы повторить весь процесс в любой момент.

Я использую массивы для своих объектов, например,

var A:Array = 
[ 
mc1,
mc2,
mc3,...mc50
];

Я пробовал это:

var anObject:DisplayObject;

//move object through swipe action

Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener (TransformGestureEvent.GESTURE_SWIPE, fl_SwipeHandler_1);
function fl_SwipeHandler_1(event:TransformGestureEvent):void
{
    switch(event.offsetX)
    {
        case -1:
    {for each (anObject in A) {
    for (var n:int = 0; n < A.length; n++){

    A[n].x = 600 // how do I get this right?
    A[n].y = 300
    A[n-1].x = 300
    A[n-1].y = 300
    A[n-2].x = -100
    A[n-2].y = -100

    }}

    }   break;
    }

}

//return to beginning of loop (Swipe 1)

mc_return_to_A0.addEventListener(MouseEvent.CLICK, return_to_A0);
function return_to_A0 (event:MouseEvent):void
{n==0} //how do I get this right? ```

Any help would be much appreciated!
Thanks

1 Ответ

0 голосов
/ 26 февраля 2020

Я не могу помочь вам с жестами, потому что я никогда не работал с ними напрямую, но позиционирование логично ... не сложно.

У вас есть Массив объектов и Массив позиций. После 1-го пролистывания вы хотите, чтобы только первый объект go занял первое заданное положение:

Positions: ...  (6)  (5)  (4)  (3)  (2)  (1)
Objects:                                 (1)  (2)  (3)  (4)  (5)  (6)  ...

После 2-го пролистывания:

Positions: ...  (6)  (5)  (4)  (3)  (2)  (1)
Objects:                            (1)  (2)  (3)  (4)  (5)  (6)  ...

После 5- th swipe:

Positions: ...  (6)  (5)  (4)  (3)  (2)  (1)
Objects:             (1)  (2)  (3)  (4)  (5)  (6)  ...

Итак, скрипт должен go выглядеть следующим образом (вроде):

// A list of your objects here.
var A:Array = [mc1, mc2, ... , mc50];

// A list of designated points here. I kinda advise to design
// these on stage with empty MovieClips appropriately named.
// This way you will have a visual representation of these
// positions, not just lots of numbers on your code.
var P:Array = [p1, p2, ... , p50];

// You need to keep a number of successful swipes, it is,
// literally, the number of objects you need to re-position.
var N:int = 0;

// So, after you successfully register a new swipe,
// you call this method to apply the re-positioning.
function arrangeThings():void
{
    // So we take first N objects, one by one.
    for (var i:int = 0; i < N; i++)
    {
        // Figure the index of the P for the current designated position. 
        var anIndex:int = N - i - 1;

        // Get the position object itself.
        var aPos:DisplayObject = P[anIndex];

        // Get the object to position.
        var anObject:DisplayObject = A[i];

        // Assume the designated position.
        anObject.x = aPos.x;
        anObject.y = aPos.y;
    }
}

Тогда я уже объяснил вам, как сброс вещей в комментарии здесь . Проще говоря, вы просто сохраняете начальные координаты в начале, а затем присваиваете их обратно при необходимости.

...