AS3错误文本链接到XML [英] AS3 error text linkage to xml
问题描述
所以我TESTIN各种方式进行联动的动态滚动文本和xml文件,我想穿上级双文本滑块他们每个人将获取的XML文件,但在这里得到随机误差不同的数据是code:
//卷动速度
VAR scrolling_speed:INT = 2;
VAR scrolling_speed2:INT = 4;
//文本来滚动
VAR text_to_scroll:字符串=www.posta.ba;
VAR xmlLoader:的URLLoader =新的URLLoader();
VAR的myData:XML;
VAR myItems:XMLList中;
VAR位置:单元;
xmlLoader.addEventListener(引发Event.COMPLETE,的onComplete);
xmlLoader.load(新的URLRequest(text0.xml));
功能的onComplete(五:事件):无效{
myData的=新的XML(的URLLoader(e.currentTarget).DATA);
myItems = myData..item;
}
//建立场
VAR my_text:文本字段=新的TextField();
VAR mySmallTextField:文本字段=新的TextField();
//添加字段到舞台
的addChild(my_text);
的addChild(mySmallTextField);
//设置文本
my_text.text = text_to_scroll;
mySmallTextField.text = text_to_scroll;
//设置x坐标关闭右侧阶段
my_text.x = stage.stageWidth;
mySmallTextField.x = stage.stageWidth;
//设置y坐标在舞台中间(约)
my_text.y =(stage.stageHeight / 1) - (my_text.height / 2.5);
mySmallTextField.y =(stage.stageHeight / 1) - (my_text.height / 1.7);
//不可选
my_text.selectable = FALSE;
mySmallTextField.selectable = FALSE;
//无边框
my_text.border = FALSE;
mySmallTextField.border = FALSE;
//场秤更多的文字
my_text.autoSize = TextFieldAutoSize.LEFT;
mySmallTextField.autoSize = TextFieldAutoSize.LEFT;
//设置格式
VAR my_text_format:的TextFormat =新的TextFormat();
VAR mySmallTextField_format:的TextFormat =新的TextFormat();
//设置颜色的十六进制
my_text_format.color = 0x000000处;
mySmallTextField_format.color = 0x000000处;
//设置字体大小
my_text_format.size = 28;
mySmallTextField_format.size = 22;
//设置字体面
my_text_format.font =富利马里兰州BT;
mySmallTextField_format.font =富利马里兰州BT;
//应用格式
my_text.defaultTextFormat = my_text_format;
my_text.setTextFormat(my_text_format);
mySmallTextField.defaultTextFormat = mySmallTextField_format;
mySmallTextField.setTextFormat(mySmallTextField_format);
//添加监听滚动
的addEventListener(Event.ENTER_FRAME,onMoveTexts);
//滚动功能
功能onMoveTexts(五:事件):无效{
my_text.x- = scrolling_speed;
mySmallTextField.x- = scrolling_speed2;
如果(my_text.x< -my_text.width){
my_text.x = stage.stageWidth;
}
如果(mySmallTextField.x< -mySmallTextField.width){
mySmallTextField.x = stage.stageWidth;
}
//设置下一个文本
如果(++位置> = myItems.length()){
位置= 0;
}
my_text.text = myItems [位置]
mySmallTextField.text = myItems [位置]
}
和xml文件(text0)至极我抓住从长相文字是这样的:
< XML版本=1.0&GT?;
<数据>
<新闻>
<项目><![CDATA [文本新闻1]>< /项目>
<项目><![CDATA [文本消息2]]>< /项目>
<项目><![CDATA [文本消息3]>< /项目>
<项目><![CDATA [文本消息4]>< /项目>
<项目><![CDATA [文本消息5]>< /项目>
<项目><![CDATA [文本消息6]>< /项目>
< /新闻>
< anotherData>
<信息><![CDATA [文本的小型文本]]>< /信息>
< / anotherData>
< /数据>
您的code几个问题:
您不要等到XML,并开始与ENTER_FRAME访问 myItems
目前在XML的其他信息没有意义的,因为你申请同一文本的两个领域。
函数onMoveTexts(五:事件):无效{
my_text.x - = scrolling_speed;
mySmallTextField.x - = scrolling_speed2;
如果(my_text.x< -my_text.width){
my_text.x = stage.stageWidth;
如果(myItems!= NULL){
//设置下一个文本
如果(++位置> = myItems.length()){
位置= 0;
}
my_text.text = myItems [位置]
}
}
如果(mySmallTextField.x< -mySmallTextField.width){
mySmallTextField.x = stage.stageWidth;
如果(myData的!= NULL){
mySmallTextField.text = myData.anotherData.info;
}
}
}
或者开始新的循环后,你的XML:
函数的onComplete(五:事件):无效{
myData的=新的XML(的URLLoader(e.currentTarget).DATA);
myItems = myData..item;
//添加监听滚动
的addEventListener(Event.ENTER_FRAME,onMoveTexts);
}
SO i was testin various ways for linkage dynamic scrolling text and xml file i wanted to put on stage double text sliders each of them will fetch different data from xml file but getting random error here is the code:
//SCROLLING SPEED
var scrolling_speed:int = 2;
var scrolling_speed2:int = 4;
//TEXT TO SCROLL
var text_to_scroll:String = "www.posta.ba";
var xmlLoader: URLLoader = new URLLoader();
var myData: XML;
var myItems: XMLList;
var position: uint;
xmlLoader.addEventListener(Event.COMPLETE, onComplete);
xmlLoader.load(new URLRequest("text0.xml"));
function onComplete(e : Event): void{
myData = new XML(URLLoader(e.currentTarget).data);
myItems = myData..item;
}
//establish the field
var my_text:TextField = new TextField();
var mySmallTextField:TextField = new TextField();
//add the field to stage
addChild(my_text);
addChild(mySmallTextField);
//set the text
my_text.text = text_to_scroll;
mySmallTextField.text = text_to_scroll;
//set the x coord off right side of stage
my_text.x = stage.stageWidth;
mySmallTextField.x = stage.stageWidth;
//set y coord in middle of stage (about)
my_text.y = (stage.stageHeight/1)-(my_text.height/2.5);
mySmallTextField.y = (stage.stageHeight/1)-(my_text.height/1.7);
//not selectable
my_text.selectable = false;
mySmallTextField.selectable = false;
//no border
my_text.border = false;
mySmallTextField.border = false;
//field scales with more text
my_text.autoSize = TextFieldAutoSize.LEFT;
mySmallTextField.autoSize = TextFieldAutoSize.LEFT;
//set a format
var my_text_format:TextFormat = new TextFormat();
var mySmallTextField_format:TextFormat = new TextFormat();
//set the color to the hex
my_text_format.color = 0x000000;
mySmallTextField_format.color = 0x000000;
//set the font size
my_text_format.size = 28;
mySmallTextField_format.size = 22;
//set the font face
my_text_format.font = "Futura Md BT";
mySmallTextField_format.font = "Futura Md BT";
//apply formatting
my_text.defaultTextFormat = my_text_format;
my_text.setTextFormat(my_text_format);
mySmallTextField.defaultTextFormat = mySmallTextField_format;
mySmallTextField.setTextFormat(mySmallTextField_format);
//add the listener to scroll
addEventListener(Event.ENTER_FRAME, onMoveTexts);
//scroll function
function onMoveTexts(e:Event):void {
my_text.x-=scrolling_speed;
mySmallTextField.x-=scrolling_speed2;
if(my_text.x<-my_text.width){
my_text.x=stage.stageWidth;
}
if(mySmallTextField.x<-mySmallTextField.width){
mySmallTextField.x=stage.stageWidth;
}
//Set next text
if(++position >= myItems.length()){
position = 0;
}
my_text.text = myItems[position];
mySmallTextField.text = myItems[position];
}
And xml file(text0) wich i grab text from looks like this:
<?xml version="1.0"?>
<data>
<news>
<item><![CDATA[Text for news 1]]></item>
<item><![CDATA[Text for news 2]]></item>
<item><![CDATA[Text for news 3]]></item>
<item><![CDATA[Text for news 4]]></item>
<item><![CDATA[Text for news 5]]></item>
<item><![CDATA[Text for news 6]]></item>
</news>
<anotherData>
<info><![CDATA[Text for small sized text]]></info>
</anotherData>
</data>
You have several problems in your code:
You don't wait for XML, and start ENTER_FRAME with access to myItems
There is no sense in additional information in the XML, because you apply same texts to the both fields.
function onMoveTexts(e:Event):void {
my_text.x -= scrolling_speed;
mySmallTextField.x -= scrolling_speed2;
if (my_text.x < -my_text.width) {
my_text.x = stage.stageWidth;
if (myItems != null) {
//Set next text
if (++position >= myItems.length()) {
position = 0;
}
my_text.text = myItems[position];
}
}
if (mySmallTextField.x < -mySmallTextField.width) {
mySmallTextField.x = stage.stageWidth;
if(myData != null){
mySmallTextField.text = myData.anotherData.info;
}
}
}
Or start your loop after you get XML:
function onComplete(e:Event):void {
myData = new XML(URLLoader(e.currentTarget).data);
myItems = myData..item;
//add the listener to scroll
addEventListener(Event.ENTER_FRAME, onMoveTexts);
}
这篇关于AS3错误文本链接到XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!