Форум Flasher.ru

Форум Flasher.ru (http://www.flasher.ru/forum/index.php)
-   ActionScript 3.0 (http://www.flasher.ru/forum/forumdisplay.php?f=83)
-   -   Создание TextField с TextFormat в loop (for) (http://www.flasher.ru/forum/showthread.php?t=177205)

ader 26.03.2012 19:53

Создание TextField с TextFormat в loop (for)
 
Код:

for (var i:int = 0; i < xml.news.item.length(); i++) {
                               
                                //Creating the textfield object and naming it "myTextField" 
                                var myTextField:TextField = new TextField(); 
 
                                //Here we add the new textfield instance to the stage with addchild() 
                                page_mc.addChild(myTextField); 
                                //textArray.push(myTextField);
 
                                //Here we define some properties for our text field, starting with giving it some text to contain. 
                                //A width, x and y coordinates. 
                                //myTextField.name = i;
                                myTextField.htmlText = XML(xml.news).item[i].zag;
                                myTextField.width = 250; 
                               
                                if (i == 0) {
                                        myTextField.x = int(myTextField.width / 2 - myTextField.width + stage.stageWidth / 2) - 211;
                                        myTextField.y = int(myTextField.height / 2 - myTextField.height + stage.stageHeight / 2) + 70;
                                }
                                if (i == 1) {
                                        myTextField.x = int(myTextField.width / 2 - myTextField.width + stage.stageWidth / 2) + 174;
                                        myTextField.y = int(myTextField.height / 2 - myTextField.height + stage.stageHeight / 2) + 70;
                                        }
                                if (i == 2) { myTextField.x = int(myTextField.width / 2 - myTextField.width + stage.stageWidth / 2) - 205;
                                                                myTextField.y = int(myTextField.height / 2 - myTextField.height + stage.stageHeight / 2)+230;
                                }
                                if (i == 3) { myTextField.x = int(myTextField.width / 2 - myTextField.width + stage.stageWidth / 2) + 174;
                                                          myTextField.y = int(myTextField.height / 2 - myTextField.height + stage.stageHeight / 2) + 230;
                                }
                                //
                                //myTextField.alpha = 0;
                       
//Here are some great properties to define, first one is to make sure the text is not selectable, then adding a border. 
                                myTextField.selectable = false; 
                                myTextField.border = false; 
 
//This last property for our textfield is to make it autosize with the text, aligning to the left. 
                                myTextField.autoSize = TextFieldAutoSize.LEFT; 
 
//This is the section for our text styling, first we create a TextFormat instance naming it myFormat 

                                var myFormat:TextFormat = new TextFormat(); 
 
//Giving the format a hex decimal color code 
                                myFormat.color = 0x000000; 
                               
                                myFormat.font = "Myriad Pro";
 
//Adding some bigger text size 
                                myFormat.size = 14; 
 
//Last text style is to make it italic. 
                                myFormat.bold = true; 
 
//Now the most important thing for the textformat, we need to add it to the myTextField with setTextFormat. 
                                //myTextField.setTextFormat(myFormat);
                               
                               
                               

                                ////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                        }

Всё бы хорошо если б не строчка myTextField.setTextFormat(myFormat); - оно не отрабатывает.
P.S. как потом иметь доступ к myTextField за пределами for() ?!

Inet_PC 26.03.2012 20:17

Цитата:

P.S. как потом иметь доступ к myTextField за пределами for() ?!
Нужно складывать все созданные текстфилды в массив. А потом по индексу можно к любому из них обратиться.

ader 26.03.2012 22:48

как это сделать?

Добавлено через 2 часа 22 минуты
помогите пожалуйста

Добавлено через 2 часа 30 минут
как писать:
Код AS3:

page_mc.textArray[0].x = 500;

?

Inet_PC 27.03.2012 09:44

Как-то так:
Код AS3:

private var textArray: Array = [];
//...
for (var i:int = 0; i < xml.news.item.length(); i++)
{
 
        //Creating the textfield object and naming it "myTextField" 
        var myTextField:TextField = new TextField(); 
 
        //Here we add the new textfield instance to the stage with addchild() 
        page_mc.addChild(myTextField); 
        //textArray.push(myTextField);
 
        //Here we define some properties for our text field, starting with giving it some text to contain. 
        //A width, x and y coordinates. 
        //myTextField.name = i;
        myTextField.htmlText = XML(xml.news).item[i].zag;
        myTextField.width = 250; 
 
        if (i == 0)
        {
                myTextField.x = int(myTextField.width / 2 - myTextField.width + stage.stageWidth / 2) - 211;
                myTextField.y = int(myTextField.height / 2 - myTextField.height + stage.stageHeight / 2) + 70;
        }
        if (i == 1)
        {
                myTextField.x = int(myTextField.width / 2 - myTextField.width + stage.stageWidth / 2) + 174;
                myTextField.y = int(myTextField.height / 2 - myTextField.height + stage.stageHeight / 2) + 70;
        }
        if (i == 2)
        {
                myTextField.x = int(myTextField.width / 2 - myTextField.width + stage.stageWidth / 2) - 205;
                myTextField.y = int(myTextField.height / 2 - myTextField.height + stage.stageHeight / 2)+230;
        }
        if (i == 3)
        {
                myTextField.x = int(myTextField.width / 2 - myTextField.width + stage.stageWidth / 2) + 174;
                myTextField.y = int(myTextField.height / 2 - myTextField.height + stage.stageHeight / 2) + 230;
        }
        //
        //myTextField.alpha = 0;
 
//Here are some great properties to define, first one is to make sure the text is not selectable, then adding a border. 
        myTextField.selectable = false; 
        myTextField.border = false; 
 
//This last property for our textfield is to make it autosize with the text, aligning to the left. 
        myTextField.autoSize = TextFieldAutoSize.LEFT; 
 
//This is the section for our text styling, first we create a TextFormat instance naming it myFormat 
 
        var myFormat:TextFormat = new TextFormat(); 
 
//Giving the format a hex decimal color code 
        myFormat.color = 0x000000; 
 
        myFormat.font = "Myriad Pro";
 
//Adding some bigger text size 
        myFormat.size = 14; 
 
//Last text style is to make it italic. 
        myFormat.bold = true; 
 
//Now the most important thing for the textformat, we need to add it to the myTextField with setTextFormat. 
        //myTextField.setTextFormat(myFormat);
 
 
 
        textArray.push(myTextField);
                                ////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
 
//....
textArray[0].x = 500;


strangedk 27.03.2012 11:08

Мне очень режут глаза блоки if (i == n)
И что, никак не обойтись без ручной установки координат? всё равно криво получается.

убрал условия, да и в принципе, код еще почистить не мешало бы.

Код AS3:

import flash.geom.Point;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
 
var textArray:Array = [];
var textPositions:Array = /* of Point */ [
    new Point(-211, 70),
    new Point(174, 70),
    new Point(-205, 230),
    new Point(174, 230)
];
 
var quantity:int = 4; // xml.news.item.length()
var text:String = "Some text "; // XML(xml.news).item[i].zag
 
var page_mc:Sprite = new Sprite();
addChild(page_mc);
 
var myFormat:TextFormat = new TextFormat(); 
myFormat.color = 0x000000;
myFormat.font = "Myriad Pro";
myFormat.size = 14; 
myFormat.bold = true;
 
for (var i:int=0; i<quantity; i++) {
        var myTextField:TextField = new TextField();
        myTextField.htmlText = text + i;
        myTextField.width = 250; // height == ?
        myTextField.height = 100;
        myTextField.selectable = false; 
        myTextField.border = false; 
        myTextField.autoSize = TextFieldAutoSize.LEFT; 
        myTextField.x = (myTextField.width + stage.stageWidth) /2  - myTextField.width + textPositions[i].x;
        myTextField.y = (myTextField.height + stage.stageHeight) /2 - myTextField.height + textPositions[i].y;
        myTextField.setTextFormat(myFormat);
 
        page_mc.addChild(myTextField);
        textArray.push(myTextField);
}


ader 27.03.2012 21:20

Спасибо!!! . Подскажите пожалуйста как иметь доступ к mytextfield за пределами for() ?
команда textArray[0].x = 500; ругается на меня :(

Добавлено через 4 минуты
всё работает - спасибо! :) нужно делать арей в privat var иначе нет доступа за пределами for();

strangedk 27.03.2012 22:04

Цитата:

Сообщение от ader (Сообщение 1071424)
Спасибо!!! . Подскажите пожалуйста как иметь доступ к mytextfield за пределами for() ?
команда textArray[0].x = 500; ругается на меня :(

Добавлено через 4 минуты
всё работает - спасибо! :) нужно делать арей в privat var иначе нет доступа за пределами for();

Я прислал вам пример быстро набранный в Flash IDE, без классов
думаю нужно было сразу присылать целый класс, тогда бы и порешали как нужно

ader 11.04.2012 20:09

Столкнулся с проблемой!
Код AS3:

page_mc.addChild(myTextField);

По неведомой причине myTextFiled не ембедидтся в page_mc
Я так понял проблема кроется в
Код AS3:

textArray.push(myTextField);

Но как по синтаксису правильно пушнуть myTextField так, чтобы он находился в page_mc я незнаю.
Помогите плиз! У меня прокрутка экрана стоит на page_mc - текст не прокручивается :(


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

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