Форум Flasher.ru

Форум Flasher.ru (http://www.flasher.ru/forum/index.php)
-   ActionScript 1.0/2.0 (http://www.flasher.ru/forum/forumdisplay.php?f=93)
-   -   Возникло недопонимание интервальных функций (http://www.flasher.ru/forum/showthread.php?t=112519)

orcpochta 30.05.2008 13:07

Возникло недопонимание интервальных функций
 
Пример-программа из библии по AS2.0, где генерируются мерцающие буквы в случайном месте сцены. В среде авторских работ создается динамическое текстовое поле, куда монтируется несколько букв определенного шрифта для дальнейшего программного доступа к ним.
Далее пишется следующий код:
Код:

function displayLetter():Void {

  // Create a random integer to yield one of the indices from the
  // aLetters array.
  var nRandomIndex:Number = Math.ceil(Math.random() * aLetters.length) - 1;

  // Create random numbers to use for the x and y coordinates of the
  // letter TextField.
  var nRandomX:Number = Math.random() * 550;
  var nRandomY:Number = Math.random() * 400;

  // Get the next available depth;
  var nDepth:Number = this.getNextHighestDepth();

  // Create a new TextField object at the random x and y coordinates.
  this.createTextField("tLetter" + nDepth, nDepth, nRandomX, nRandomY, 0, 0);

  // Assign a reference to a variable to make it more convenient to
  // work with the TextField.
  var tLetter:TextField = this["tLetter" + nDepth];

  // Set the autoSize and text properties so the random letter
  // displays.
  tLetter.autoSize = "left";
  tLetter.text = aLetters[nRandomIndex];

  // Set a custom property called fadeDirection that determines
  // the increment by which the alpha will change. And set the
  // alpha to 0 initially.
  tLetter.fadeDirection = 5;
  tLetter._alpha = 0;

  // Tell Flash to embed the font for the TextField.
  tLetter.embedFonts = true;

  // Create a TextFormat object that tells Flash to use the
  // Verdana font and set the size to 15.
  var tfFormatter:TextFormat = new TextFormat();
  tfFormatter.font = "Verdana";
  tfFormatter.size = 15;

  // Assign the TextFormat to the TextField.
  tLetter.setTextFormat(tfFormatter);

  // Set an interval at which the letter will fade in and out.
  tLetter.nInterval = setInterval(this, "alphaFade", 10, tLetter);
}

function alphaFade(tLetter:TextField):Void {

  // Increment the letter TextField's alpha.
  tLetter._alpha += tLetter.fadeDirection;

  // Check to see if the letter has faded in completely. If so
  // set the fadeDirection property to -5 so that the TextField
  // starts to fade out. Otherwise, if the letter has faded out
  // completely...
  if(tLetter.fadeDirection > 0 && tLetter._alpha >= 100) {
    tLetter.fadeDirection = -5;
  }
  else if(tLetter.fadeDirection < 0 && tLetter._alpha <= 0) {
    // ... clear the interval and remove the TextField.
    clearInterval(tLetter.nInterval);
    tLetter.removeTextField();
  }

  // Make sure to update the screen.
  updateAfterEvent();
}

// Create the array of letters.
var aLetters:Array = ["a", "b", "c", "d", "e", "f"];

// Set the interval at which a new letter should be displayed.
var nDisplayInterval:Number = setInterval(this, "displayLetter", 1);

Бегло пробежался по коду и стал его воспроизводить на свой манер и тут возникла проблема - белое поле и ничего не происходит. Первым делом потрейсил, все ли функции запускаются? - запускаются все. Далее стал искать, проблему в инициализации текстового поля - нашел, что
Код:

var nDepth:Number = this.getNextHighestDepth();
trace(nDepth);

- выдает undefined.
Стал искать серьезные отличии в кодах и увидел, что у меня запуск интервальных функций инициализирован не
Код:

tLetter.nInterval = setInterval(this, "alphaFade", 10, tLetter);
var nDisplayInterval:Number = setInterval(this, "displayLetter", 1);

, а
Код:

tLetter.nInterval = setInterval(alphaFade, 10, tLetter);
var nDisplayInterval:Number = setInterval(displayLetter, 1);

- к чему я, собственно, изучая эту книгу и привык.
Т.е. в книге говорится, что синтаксис запуска функций: setInterval(functionName, interval, params );
объектных методов: setInterval(objectName, "objectMethod", interval, params);

Посмотрел мануал, действительно, есть синтаксис setInterval(this, "functionName", interval, params); - я так понимаю, что он идентичен синтаксису setInterval(functionName, interval, params );, но почему же тогда при синтаксисе через this все работает, а при другом нет?
Объясните разницу, если она есть, плиз.

Извините, что так много букв...)))

Волгоградец 30.05.2008 13:36

Все упирается в область видимости имен. Если в функции displayLetter () есть ссылка с помощью this, то в методе setInterval () эта ссылка даст другое значение. Когда используешь такую сигнатуру:
Код:

setInterval(objectName, "objectMethod", interval, params);
То сам задаешь область видимости функции.

orcpochta 30.05.2008 13:58

Извините, ничего не понял..((

Волгоградец 30.05.2008 14:25

http://www.flasher.ru/forum/showthread.php?t=104635

Alex_beginner 30.05.2008 14:55

Если Вы проанализируете работу вот этого кода то поймете почему.
Код:

myObject:Object = {};
myObject.method = function(par:String):Void
    {
        trace(par);
    };
setInterval(myObject, "method", 100, "Привет orcpochta");


orcpochta 30.05.2008 15:02

я примерно понял, но осталось уяснить какую роль играет this в самом первом вызове интервальной функции var nDisplayInterval:Number = setInterval(this, "displayLetter", 1); - чем this там является?

Цитата:

Сообщение от Alex_beginner (Сообщение 742677)
Если Вы проанализируете работу вот этого кода то поймете почему.
Код:

myObject:Object = {};
myObject.method = function(par:String):Void
    {
        trace(par);
    };
setInterval(myObject, "method", 100, "Привет orcpochta");


забыли var в объявлении myObject..))
опять же тут все ясно: создается объект, ему присваивается метод, который затем и вызывается в интервале - но это никак меня не приблизило к пониманию моего вопроса..))

Alex_beginner 30.05.2008 15:15

Код:

забыли var в объявлении myObject..))
Виноват, исправляюсь! Сильно торопился Вам помочь, что даже ненаписал var.:)

Волгоградец 30.05.2008 15:45

Если здесь
Код:

var nDisplayInterval:Number = setInterval(this, "displayLetter", 1);
опустить слово this, то получится вот что:
вот эта строчка
Код:

this.createTextField("tLetter" + nDepth, nDepth, nRandomX, nRandomY, 0, 0);
не будет работать, т.к. в этом случае слово this будет относиться к методу setInterval (), т.е. для компилятора строчка будет выглядеть как
Код:

setInterval.createTextField()
А т.к. у setInterval нет такого метода, то и появляется ошибка.

orcpochta 01.06.2008 17:18

Спасибо! Разобрался..))

etc 09.06.2008 15:25

Цитата:

Сообщение от Волгоградец (Сообщение 742701)
Если здесь
Код:

var nDisplayInterval:Number = setInterval(this, "displayLetter", 1);
опустить слово this, то получится вот что:
вот эта строчка
Код:

this.createTextField("tLetter" + nDepth, nDepth, nRandomX, nRandomY, 0, 0);
не будет работать, т.к. в этом случае слово this будет относиться к методу setInterval (), т.е. для компилятора строчка будет выглядеть как
Код:

setInterval.createTextField()
А т.к. у setInterval нет такого метода, то и появляется ошибка.

Неправда.


Часовой пояс GMT +4, время: 20:37.

Copyright © 1999-2008 Flasher.ru. All rights reserved.
Работает на vBulletin®. Copyright ©2000 - 2026, Jelsoft Enterprises Ltd. Перевод: zCarot
Администрация сайта не несёт ответственности за любую предоставленную посетителями информацию. Подробнее см. Правила.