AS3 случайное движение символа l oop с ослаблением? - PullRequest
0 голосов
/ 26 февраля 2020

Я собрал скрипт для создания случайного перемещения символа ie по сцене. Моя цель - заставить его медленно парить в одном месте.

Проблема в том, что он довольно глючный, и положение символа всегда начинается в левом верхнем углу сцены. Мне бы очень хотелось найти способ добавить замедление в сценарий тоже.

Любая помощь будет принята с благодарностью!

//Declare Globals
var currentFrameCount: int = 300;
var totalFrameCount: int = 300;
this.x = Math.round(Math.random() * 200);
this.y = Math.round(Math.random() * 200);
var destinationX: Number = this.x;
var destinationY: Number = this.y;
var initialX: Number;
var initialY: Number;
var distanceX: Number;
var distanceY: Number;
var xProg: Number;
var yProg: Number;

var countFrame: int = 0;
var delay: int = 0;

addEventListener(Event.ENTER_FRAME, callDelay);
function callDelay(e: Event) {
    countFrame++;
    delayEvent(delay);
}
//Code to move the object to a random location and give it a random target to move to.
function spawnObject() {
    destinationX = Math.round(Math.random() * 200); //random destination
    destinationY = Math.round(Math.random() * 200);
    currentFrameCount = 30;
    initialX = this.x;
    initialY = this.y;
    distanceX = destinationX - initialX; //distance between destination and initial
    distanceY = destinationY - initialY;
    yProg = distanceY / totalFrameCount;
}

function delayEvent(period) {
    if ((countFrame) >= period) {
        removeEventListener(Event.ENTER_FRAME, callDelay);
        timedEvent();
        countFrame = 0;
        return;
    }
}

function timedEvent(): void {
    currentFrameCount = totalFrameCount;
    this.x = destinationX;
    this.y = destinationY;
    spawnObject(); //move the object to a new location and give new destination
    this.addEventListener(Event.ENTER_FRAME, moveTo); //add an event listener to move the object around
}

function moveTo(e: Event): void {
    currentFrameCount++;
    if (currentFrameCount < totalFrameCount) {
        this.x += xProg; //incrase x by the x step value
        this.y += yProg; //increase y by the y step value
    } else {
        this.removeEventListener(Event.ENTER_FRAME, moveTo); // remvoe this event listener
        addEventListener(Event.ENTER_FRAME, callDelay);
    }
}
...