在这段代码中:
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;
}
}
}
}