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] 
 }

XML ファイル

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 に答える