Форум 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=86694)

snorik 20.10.2006 21:32

Собственный класс реализующий дерево
 
Вложений: 1
Есть задача: Создать класс, который реализует дерево определенной глубины с заданным кол-вом разветвлений от каждой ветви.

Сделал, но неправильно работает Array в классе, подскажите как решить проблему!

мой класс выглядит так:
Код:

class cTree {
  private var branchName:String;
  private var currentDepth:Number;
  private var numberOfBranches:Number;
  private var childBranches:Array = new Array();

  public function cTree(brname:String, cdepth:Number, branchnes:Number)
  {
    this.branchName = brname;
    this.currentDepth = cdepth;
    this.numberOfBranches = branchnes;
    if(this.currentDepth > 0) {
      for(var n = 0; n < this.numberOfBranches; n++)
        this.childBranches.push(new cTree(this.branchName + " - " + n,this.currentDepth - 1, this.numberOfBranches));
    }
  }

  public function traceTree():Void
  {
    trace("depth:" + this.currentDepth + "  nodeName:" + this.branchName);
    if(this.currentDepth > 0) {
    for(var n = 0; n < numberOfBranches; n++)
      childBranches[n].traceTree();
    }
  }
}


silin 20.10.2006 22:57

надо в конструкторе явно прописать создание нового массива
Код:

if(currentDepth > 0) {
        childBranches=[];
        for(var n = 0; n < numberOfBranches; n++)
                childBranches.push(new cTree(branchName + " - " + n,currentDepth - 1, numberOfBranches));
}


snorik 21.10.2006 02:32

огромное спасибо! заработало! тогда, как я понял, new Array() вначале уже не нужен


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

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