dcsimg
A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: XML problem, please help :(

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    4

    XML problem, please help :(

    Hi guys,

    I just starded working with AS3 and want to make my personal website with images in background. Now, I'm having one image... but I want to have more images in random order... Maybe my code can help. I tried everything... but I get stucked... I know I should use Math.random, but no progress

    Access of undefined property _id.
    Access of undefined property _id.
    Access of undefined property listLoader.
    Access of undefined property stImage.


    Here is my code:
    XML.ignoreComments = false;
    XML.ignoreProcessingInstructions = false;
    var _xml:XML;
    var _xmlLdr:URLLoader = new URLLoader( new URLRequest("dat/xml/bg.xml") );

    listLoader.addEventListener(Event.COMPLETE, gotList);

    function gotList(evt:Event):void {
    var xmlData:XML = _xml.Data.child("dat");
    var numImages:Number = _id.length();

    var stImage:String = _id[Math.floor(numImages*Math.random())].toString();
    }
    _xmlLdr.load(new URLRequest(stImage));

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Well, like the error messages say, you did not define listLoader or _id. You did define stImage, but you declared it as a local variable in gotList, so it is undefined outside that function.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    I vave tried with this, but still nothing

    Code:
    var _xml:XML;
    var _xmlLdr:URLLoader = new URLLoader();
    _xmlLdr.load(new URLRequest("XML_NOTES.xml"));
    _xmlLdr.addEventListener(Event.COMPLETE, _xmlLded);
    
    function _xmlLded(e:Event):void {
        var loader:URLLoader = e.target as URLLoader;
        _xml = new XML(loader.data);
    }
    function getRandomItem():XML {
        var elementsCount:int = _xml.length();
        var randomIndex:int = Math.floor( Math.random() * (elementsCount-1) );
        return myXML[randomIndex];
    }



    This is my original code, but it is showing only 1 image in background. initFnc is making me so problems


    Code:
    import caurina.transitions.*;
    var mc;
    function _ovrFnc(e:MouseEvent) {
    	Tweener.addTween(e.target, {alpha:0.5, time:0.5});
    }
    function _outFnc(e:MouseEvent) {
    	if (!e.target._mdwn) {
    		Tweener.addTween(e.target, {alpha:1, time:0.5});
    	}
    }
    function _prsFnc(e:MouseEvent) {
    	if (mc!=undefined) {
    		mc.buttonMode=mc.mouseEnabled=true;
    		mc._mdwn=false;
    		Tweener.addTween(mc, {alpha:1, time:0.5});
    	}
    	MovieClip(root)._bgImg._go(_dta[e.target.i]._pth,_dta[e.target.i]._pthx,_dta[e.target.i]._pthxx,_dta[e.target.i]._typ);
    	mc=e.target;
    	mc._mdwn=true;
    	mc.buttonMode=mc.mouseEnabled=false;
    	Tweener.addTween(mc, {alpha:0.5, time:0.5});
    }
    //------------
    var _dta:Array=new Array();
    function _initFnc() {
    	var _id=_xml.Data.child("dat");
    	var _tot:Number=_id.length();
    	for (var i:Number=0; i<_tot; i++) {
    		var _btn:_btnMc=new _btnMc();
    		_btn.x=-(i*14);
    		_btn.y=-11;
    		_btn.i=i;
    		if (i==0) {
    			_btn._mdwn=true;
    			_btn.buttonMode=_btn.mouseEnabled=false;
    			Tweener.addTween(_btn, {alpha:0.5, time:0.5});
    			mc=_btn;
    		} else {
    			_btn._mdwn=false;
    			_btn.buttonMode=_btn.mouseEnabled=true;
    		}
    		addChild(_btn);
    		//-----
    		_btn.addEventListener(MouseEvent.MOUSE_DOWN,_prsFnc);
    		_btn.addEventListener(MouseEvent.MOUSE_OVER, _ovrFnc);
    		_btn.addEventListener(MouseEvent.MOUSE_OUT, _outFnc);
    		//-----
    		_dta.push({_typ:_id[i].@type,_pth:_id[i].@path,_pthx:_id[i].@path2,_pthxx:_id[i].@path3});
    	}
    	MovieClip(root)._bgImg._go(_dta[0]._pth,_dta[0]._pthx,_dta[0]._pthxx,_dta[0]._typ);
    }
    //------------
    XML.ignoreComments = false;
    XML.ignoreProcessingInstructions = false;
    var _xml:XML;
    var _xmlLdr:URLLoader= new URLLoader();
    function _xmlLded(e:Event) {
    	try {
    		_xml = XML(e.target.data);
    		_initFnc();
    	} catch (e:Error) {
    		return;
    	}
    }
    _xmlLdr.addEventListener(Event.COMPLETE,  _xmlLded);
    _xmlLdr.load(new URLRequest("dat/xml/bg.xml"));
    Last edited by mobscene071; 11-17-2011 at 04:59 PM. Reason: Bad format

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Please format your code with [code] tags. It's pretty unreadable as is.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    oh, sorry, I have reedit

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    In the first code from your last post, myXML is undefined. It looks like you might have meant _xml. Also, you should not subtract 1 from elements count in the random line, since that means that the last element will never be selected.

    In the second, I don't know what you're trying to do. But that's at least partially because you have not provided the XML that's being operated on, the error messages you are receiving, or what _bgImg or _go are.

    Also, what's with all the underscores? Those are some ugly identifiers.

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    OK, I have fixed code and this is working with this code

    Code:
    var _xml:XML;
    var _xmlLdr:URLLoader = new URLLoader();
    _xmlLdr.load(new URLRequest("dat/xml/bg.xml"));
    _xmlLdr.addEventListener(Event.COMPLETE, _xmlLded);
    
    function _xmlLded(e:Event):void {
        var loader:URLLoader = e.target as URLLoader;
        _xml = new XML(loader.data);
    	_initFnc();
    }
    function getRandomItem():XML {
        var elementsCount:int = _xml.length();
        var randomIndex:int = Math.floor( Math.random() * (elementsCount) );
        return _xml[randomIndex];
    }
    But it is always showing me only first image :S It is still not random. And my xml is
    Code:
    <DynamicWeb>
    <!--
    	type - static or dynamic
    -->
    	<Data>
    			<dat type="static" path="dat/Images/Bg/001.jpg"/>
    			<dat type="static" path="dat/Images/Bg/002.jpg"/>
    			<dat type="static" path="dat/Images/Bg/003.jpg"/>
    			<dat type="static" path="dat/Images/Bg/004.jpg"/>
    	</Data>
    </DynamicWeb>

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Nothing ever calls getRandomItem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center