1

大家好,我正在使用 actionscript 从 XML 文件中引入数据。我可以使用 AS3 中的下一步和后退按钮循环浏览 XML 文件。此信息正在加载到动态文本框中。当到达 xml 文件的末尾时,我正在努力循环遍历xml 文件!例如,当我单击 as3 中的下一个按钮时,会显示下一个地点,但是当显示最后一个地点时,如果有意义,我希望按钮循环回到 XML 文件的开头。

我已经配置了 AS3,因此它使用下一个按钮循环回到开头,但是当单击上一个按钮时,我正在努力加载最后一个场地。

function nextVenue(e:Event) {
    //Looks at length cycles to next venue
    if (currentVenue < myXML.venue.length()) {
        //cycle through venues when next_btn is clicked
        currentVenue +=length; 
    } else {
        //loop to beginning
        (currentVenue =length) 
    }
    //Output to title textbox
    myTextBoxTitle.text = myXML.venue.name[currentVenue -1] ;

    //Output to description textbox
    myTextBoxDes.text = myXML.venue.description[currentVenue -1];
}

//previous event function / when back button is clicked
function prevVenue(e:Event) {    
    if (currentVenue >length) {
        currentVenue -=length; //cycle back through venue when prev_btn is clicked -1
    }  
    //Output to title textbox
    myTextBoxTitle.text = myXML.venue.name[currentVenue -1];

    //Output to description textbox
    myTextBoxDes.text = myXML.venue.description[currentVenue -1] 
 }

文件

http://pastebin.com/gzc4t2WR

先感谢您 :)

4

1 回答 1

0

在这段代码中:

 if (currentVenue < myXML.venue.length()) {
    //cycle through venues when next_btn is clicked
    currentVenue +=length; 
  } else {
    //loop to beginning
    (currentVenue =length) 
  } 

您正在将 currentVenue 设置为当前长度 - 但您的评论说循环开始。如果是这种情况,那么您需要将 currentVenue 重置为 0 - 长度是多少?

** 新代码 **

package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.text.TextField;

public class testing extends Sprite
{

    public var switch1:String = 'a';
    public var switch2:String = "3";
    public var myXML:XML;

    public var nextButton:Sprite;
    public var prevButton:Sprite;
    public var currentVenue:int;

    public var myTextTitle:TextField;
    public var myTextDes:TextField;

    public function testing() {

        this.addEventListener(Event.ADDED_TO_STAGE, this.init);
        var myLoader:URLLoader = new URLLoader();
        myLoader.load(new URLRequest("./test.xml"));
        myLoader.addEventListener(Event.COMPLETE, this.processXML);
    }

    public function init(e:Event):void {
        this.removeEventListener(Event.ADDED_TO_STAGE, this.init);
        this.currentVenue = 0;=
        this.graphics.beginFill(0x333333);
        this.graphics.drawRect(0,0,this.stage.stageWidth, this.stage.stageHeight);
        this.graphics.endFill();

        this.nextButton = this.createSpriteButton("next");
        this.addChild(this.nextButton);
        this.nextButton.x = 10;
        this.nextButton.y = 10;

        this.prevButton = this.createSpriteButton("prev");
        this.addChild(this.prevButton);
        this.prevButton.x = 100;
        this.prevButton.y = 10;

        this.myTextTitle = new TextField();
        this.addChild(this.myTextTitle);
        this.myTextTitle.background = true;
        this.myTextTitle.backgroundColor = 0xFFFFFF;
        this.myTextTitle.text = "Title: ";
        this.myTextTitle.x = 10;
        this.myTextTitle.y = 40;
        this.myTextTitle.width = 100;
        this.myTextTitle.height = 20;

        this.myTextDes = new TextField();
        this.addChild(this.myTextDes);
        this.myTextDes.background = true;
        this.myTextDes.backgroundColor = 0xFFFFFF;
        this.myTextDes.text = "Des: ";
        this.myTextDes.x = 10;
        this.myTextDes.y = 70;
        this.myTextDes.width = 100;
        this.myTextDes.height = 20;

        this.nextButton.addEventListener(MouseEvent.CLICK, this.nextVenue);
        this.prevButton.addEventListener(MouseEvent.CLICK, this.prevVenue);
    }

    private function createSpriteButton(btnName:String):Sprite {
        var sBtn:Sprite = new Sprite();
        sBtn.name = btnName;
        sBtn.graphics.beginFill(0xFFFFFF);
        sBtn.graphics.drawRoundRect(0,0,80,20,5);
        var sBtnTF:TextField = new TextField();
        sBtn.addChild(sBtnTF);
        sBtnTF.text = btnName;
        sBtnTF.x = 5;
        sBtnTF.y = 3;
        sBtnTF.selectable = false;
        sBtn.alpha = .5;

        sBtn.addEventListener(MouseEvent.MOUSE_OVER, function(e:Event):void { sBtn.alpha = 1 });
        sBtn.addEventListener(MouseEvent.MOUSE_OUT, function(e:Event):void { sBtn.alpha = .5 });

        return sBtn;
    }

    private function processXML(e:Event):void {
        this.myXML = new XML(e.target.data);
        this.updateData();
    }

    private function updateData():void {
        this.myTextTitle.text = "Title: " + this.myXML.venue.name[currentVenue];
        this.myTextDes.text = "Des: " + this.myXML.venue.description[currentVenue];
    }

    private function nextVenue(e:Event):void {
        this.updateCurrentVenue(1);
        this.updateData();
    }

    private function prevVenue(e:Event):void {
        this.updateCurrentVenue(-1);
        this.updateData();
    }

    private function updateCurrentVenue(step:int):void {
        if(step > 0) {
            this.currentVenue < this.myXML.venue.length()-1 ? this.currentVenue++ : this.currentVenue = 0;
        } else {
            this.currentVenue != 0 ? this.currentVenue-- : this.currentVenue = this.myXML.venue.length()-1;
        }
    }
}

}

于 2012-11-29T13:55:54.727 回答