Hi, I'm looking for some help with an xml based slide show.
As it is the code is a bit sloppy and it's not entirely completed, but atm I just can't get past the current error i'm receiving.
The error i'm getting is (translated) something like: TypeError: Error #1009: It's not possible to to get access to a property or method with the object referens null.
I'm guessing this originates from the calls to the event listener swapSlide(). Would very much appreciate some help (and of course other hints on what i may be doing wrong, other than that the code looks horrible ofc ;) ).
My code in it's full so far:
package {
import caurina.transitions.Tweener;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.text.TextFormatAlign;
/**
* ...
* @author PQ
*/
public class XmlSlide extends MovieClip {
// object properties
private var _currentContainer:Sprite; // current container, is in the front
private var _currentActiveSlide:int = -1; // current active slide
private var _slideCount:int; // amount of slides
private var _slideTimer:Timer; // Timer calls the swap function at set time
private var _slideContainer1:Sprite;
private var _slideContainer2:Sprite;
private var _slideLoader:Loader; // loades the slides
private var _xmlPath:String = "xmlfile.xml"; // xml URL
private var _xmlLoader:URLLoader; // loads xml file
private var _xmlData:XML; // holds the XML data
private var _slideDelay:uint; // delay between slides
private var _textDelay:uint; // delay before text appears
private var _imgWidth:uint;
private var _imgHeight:uint;
private var _randomize:uint; // display slides randomly or not
private var _useSwapSlide:uint; // change slides or use 1 and the same
private var _imgURL:String; // imgurl + imgname
private var _startLoc:String; // top, right, bottom, left, (default (middle))
private var _startXY:Array; // holds the x and y for sliding imgs startpos
private var _textTimer:Timer;
private var _textWidth:uint; // width of text field
private var _textHeight:uint; // height of text field
private var _textContainer:TextField;
private var _formatsText:TextFormat;
private var _mainContainer:MovieClip; // the main movieclips that holds everything.
// object constructor
public function XmlSlide() {
// stage inställningar
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
_mainContainer = new MovieClip;
addChild(_mainContainer);
_mainContainer.x = 0;
_mainContainer.y = 0;
var _textContainer = new TextField();
var _formatsText = new TextFormat();
// startar xml laddning
_xmlLoader = new URLLoader();
_xmlLoader.addEventListener(Event.COMPLETE, onXmlLoadComplete)
_xmlLoader.load(new URLRequest(_xmlPath));
}
private function onXmlLoadComplete(e:Event):void {
_xmlData = new XML(e.target.data);
// set XML data
_slideCount = _xmlData.*.length();
_randomize = _xmlData.@randomize;
_useSwapSlide = _xmlData.@swapimg;
_slideDelay = _xmlData.@slidedelay;
_textDelay = _xmlData.@textdelay;
_imgWidth = _xmlData.@imgwidth;
_imgHeight = _xmlData.@imgheight;
_textContainer.width = _imgWidth;
_slideTimer = new Timer(_slideDelay);
_slideTimer.addEventListener(TimerEvent.TIMER, swapSlide);
_slideContainer1 = new Sprite();
_slideContainer2 = new Sprite();
_mainContainer.addChild(_slideContainer1);
_mainContainer.addChild(_slideContainer2);
_currentContainer = _slideContainer2;
swapSlide(null);
}
private function swapSlide(e:Event = null):void {
if (_slideTimer.running) {
_slideTimer.stop();
}
//
if (_randomize == 1) {
_currentActiveSlide = randomNum();
//trace("randno1: " + randomNo);
} else {
if (_currentActiveSlide + 1 < _slideCount) {
_currentActiveSlide++;
} else {
_currentActiveSlide = 0;
}
}
if (_currentContainer == _slideContainer2) {
_currentContainer = _slideContainer1;
} else {
_currentContainer = _slideContainer2;
}
var startImage:String = _xmlData.image[_currentActiveSlide].imageSrc;
var _imgUrl = _xmlData.@imageurl + startImage;
_startLoc = _xmlData.image[_currentActiveSlide].enterfrom;
_startXY = setStartXY();
_mainContainer.swapChildren(_slideContainer2, _slideContainer1);
_slideLoader = new Loader();
// add event listener when slide is loaded
_slideLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, fadeSlideIn);
// load the next slide
_slideLoader.load(new URLRequest(_imgUrl));
}
private function fadeSlideIn(e:Event):void {
_slideLoader.width = _imgWidth;
_slideLoader.height = _imgHeight;
_slideLoader.x = _startXY[0];
_slideLoader.y = _startXY[1];
//trace(_slideLoader.content);
// add loaded slide from slide loader to the
// current container
_currentContainer.addChild(_slideLoader.content);
Tweener.addTween(_currentContainer, { x:0, y:0, time:2 } );
//Tweener.addTween(currentContainer, { alpha:1, time:FADE_TIME, onComplete:function() { slideTimer.start(); }} );
//_loaderSwapImg, { x:0, y:0, time:3 }
_textTimer = new Timer(_textDelay);
_textTimer.addEventListener(TimerEvent.TIMER, swapText);
_textTimer.start();
}
private function swapText(e:Event):void {
_textContainer.x = _startXY[0];
_textContainer.y = _startXY[1];
_textTimer.stop();
applyTextFormats();
Tweener.addTween(_textContainer, { x:150, y:75, time:2, onComplete:function() { _slideTimer.start(); } } )
}
private function applyTextFormats():void {
_textContainer.height = 40;
_textContainer.text = _xmlData.image[_currentActiveSlide].text;
_formatsText.bold = _xmlData.image[_currentActiveSlide].bold;
_formatsText.size = _xmlData.image[_currentActiveSlide].fontsize;
_formatsText.align = TextFormatAlign.CENTER;
_textContainer.setTextFormat(_formatsText);
}
private function randomNum():uint {
var randomNo:uint;
randomNo = uint(Math.random() * _slideCount);
return randomNo;
}
private function setStartXY():Array {
//anger startplats för bild, 1 av 5 möjliga
var xCoordinat:Number;
var yCoordinat:Number;
//trace("Enter from: " + _startLoc);
switch (_startLoc) {
case "top":
xCoordinat = 0;
yCoordinat = -5 - _imgHeight;
//trace("image start position: top");
break;
case "right":
xCoordinat = stage.width + 5;
yCoordinat = 0;
//trace("image start position: right");
break;
case "bottom":
xCoordinat = 0;
yCoordinat = stage.height + 5;
//trace("image start position: bottom");
break;
case "left":
xCoordinat = -5 - _imgWidth;
yCoordinat = 0;
//trace("image start position: left");
break;
default:
xCoordinat = 0;
yCoordinat = 0;
//trace("image start position: middle");
}
var locArray:Array = new Array(xCoordinat, yCoordinat);
return locArray;
}
}
}