A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [RESOLVED] AS 2.0 xml news ticker - scripting problem

  1. #1
    Junior Member
    Join Date
    Aug 2008
    Posts
    5

    resolved [RESOLVED] AS 2.0 xml news ticker - scripting problem

    Hi there!

    I'm making a very simple news ticker that uses XML to load four items: copy text, an image, date and a URL. The script was pieced together from tutorials and a bit of self-teaching, so I know it's probably not great. The AS is pasted below. Everything loads just fine, but the button script returns an "undefined" error. I assume this is because the item is no longer found in the array, but I'm not entirely sure. I also think it may be that the loadMovie method isn't working well with a specific item in the array. Also the link does not work. Any ideas?

    *Note* I know there are probably more advanced ways to tackle this but I'd like to keep this at my learning level so I can grow with it. :-) Thanks in advance for any advice!



    AS2:

    Actionscript Code:
    function loadXML(loaded) {
        if (loaded) {
            xmlNode = this.firstChild;
            pic = [];
            date = [];
            copy = [];
            link = [];
            total = xmlNode.childNodes.length;
            for (i=0; i<total; i++) {
                pic[i] = xmlNode.childNodes[i].childNodes[0].firstChild.nodeValue;
                date[i] = xmlNode.childNodes[i].childNodes[1].firstChild.nodeValue;
                copy[i] = xmlNode.childNodes[i].childNodes[2].firstChild.nodeValue;
                link[i] = xmlNode.childNodes[i].childNodes[3].firstChild.nodeValue;

            }
            first_item();
        } else {
            content = "file not loaded!";
        }
    }

    var xmlData = new XML();
    xmlData.ignoreWhite = true;
    xmlData.onLoad = loadXML;
    xmlData.load("features.xml");

    function first_item() {
        delay = 5000;
        p = 0;
        display(p);
        p++;
    }
    function timer() {
        myInterval = setInterval(ticker, delay);
        function ticker() {
            clearInterval(myInterval);
            if (p == total) {
                p = 0;
            }
            fadeout();
        }
    }
    function display(pos) {
        over = new TextFormat();
        over.underline = true;
        //
        out = new TextFormat();
        out.underline = false;
        //
        newsMC.newsText._alpha = 100;
        newsMC.image.loadMovie(this.pic[pos]);
        newsMC.post_date.text = date[pos];
        newsMC.newsText.text = copy[pos];
        newsMC.link.onRelease = function() {
            getURL(link[pos], "_parent");
        };
        newsMC.onRollOver = function() {
            this.link.setTextFormat(over);
        };
        newsMC.onRollOut = function() {
            this.link.setTextFormat(out);
        };
        timer();
    }

    function fadeout() {
        this.onEnterFrame = function() {
            if (newsMC.newsText._alpha>=0) {
                newsMC.newsText._alpha -= 5;
            } else {
                display(p);
                p++;
                delete this.onEnterFrame;
            }
        };
    }

    item1_btn.onRelease = function() {
        newsMC.newsText._alpha = 100;
        newsMC.image.loadMovie(this.pic[1]);
        newsMC.post_date.text = date[0];
        newsMC.newsText.text = copy[0];
        newsMC.link.onRelease = function() {
            getURL(link[0], "_parent");
        };
        newsMC.onRollOver = function() {
            this.link.setTextFormat(over);
        };
        newsMC.onRollOut = function() {
            this.link.setTextFormat(out);
        };
        timer();
    };

    item2_btn.onRelease = function() {
        newsMC.newsText._alpha = 100;
        newsMC.image.loadMovie(this.pic[1]);
        newsMC.post_date.text = date[1];
        newsMC.newsText.text = copy[1];
        newsMC.link.onRelease = function() {
            getURL(link[1], "_parent");
        };
        newsMC.onRollOver = function() {
            this.link.setTextFormat(over);
        };
        newsMC.onRollOut = function() {
            this.link.setTextFormat(out);
        };
        timer();
    };



    and here is a piece of the XML:

    <news>

    Code:
    <item>
    <pic>images/img1.jpg</pic>
    <date>2 . 26 . 2010</date>
    <copy>This will be our first news item. Hopefully we'll be able to have enough "featured" items to keep a consistent line of news. Updated items are important.</copy>
    <link>http://www.google.com</link>
    </item>
    </news>
    Last edited by Ameryl; 03-02-2010 at 11:24 PM.

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Can you post a stripped down version of your .fla here?

  3. #3
    Junior Member
    Join Date
    Aug 2008
    Posts
    5
    Sorry this took a little while; I was having problems logging into my account. I've attached the .fla file below but couldn't upload the XML. Thank you for any help you can provide!!
    Attached Files Attached Files

  4. #4
    :
    Join Date
    Dec 2002
    Posts
    3,518
    wrap the 'link' text field, inside a movie clip, give it an instance name of 'more'
    so 'newsMC' > 'more' > 'link'
    Code:
    function loadXML(loaded) {
    	if (loaded) {
    		xmlNode = this.firstChild;
    		pic = [];
    		date = [];
    		copy = [];
    		link = [];
    		total = xmlNode.childNodes.length;
    		for (var i=0; i<total; i++) {
    			pic[i] = xmlNode.childNodes[i].childNodes[0].firstChild.nodeValue;
    			date[i] = xmlNode.childNodes[i].childNodes[1].firstChild.nodeValue;
    			copy[i] = xmlNode.childNodes[i].childNodes[2].firstChild.nodeValue;
    			link[i] = xmlNode.childNodes[i].childNodes[3].firstChild.nodeValue;
    
    		}
    		first_item();
    	} else {
    		content = "file not loaded!";
    	}
    }
    
    var xmlData = new XML();
    xmlData.ignoreWhite = true;
    xmlData.onLoad = loadXML;
    xmlData.load("features.xml");
    
    function first_item() {
    	delay = 5000;
    	display(p=0);
    }
    function timer() {
    	clearInterval(myInterval);
    	myInterval = setInterval(ticker, delay);
    	function ticker() {
    		clearInterval(myInterval);
    		fadeout();
    	}
    }
    //
    over = new TextFormat();
    over.underline = true;
    out = new TextFormat();
    out.underline = false;
    //
    newsMC.more.onRollOver = function() {
    	this.link.setTextFormat(over);
    };
    newsMC.more.onRollOut = function() {
    	this.link.setTextFormat(out);
    };
    newsMC.more.onRelease = function() {
    	getURL(link[p], "_parent");
    };
    //
    function display(pos) {
    	newsMC.newsText._alpha = 100;
    	newsMC.image.loadMovie(pic[pos]);
    	newsMC.post_date.text = date[pos];
    	newsMC.newsText.text = copy[pos];
    	timer();
    }
    
    function fadeout() {
    	newsMC.onEnterFrame = function() {
    		if (newsMC.newsText._alpha>=0) {
    			newsMC.newsText._alpha -= 5;
    		} else {
    			if (++p == total) {
    				p = 0;
    			}
    			display(p);
    			delete this.onEnterFrame;
    		}
    	};
    }
    
    item1_btn.onRelease = function() {
    	display(p=0);
    };
    item2_btn.onRelease = function() {
    	display(p=1);
    };
    item3_btn.onRelease = function() {
    	display(p=2);
    };
    item4_btn.onRelease = function() {
    	display(p=3);
    };
    Last edited by dawsonk; 03-09-2010 at 05:42 PM.

  5. #5
    Junior Member
    Join Date
    Aug 2008
    Posts
    5
    dawson:

    Thank you - for the solution and the code clean-up! This makes much more sense, and I see how I was being redundant by repeating the rollover function for each button's code. I can also see how the order of operations in the timer function (along with the heavy button code) was causing the problem. You've been a great help! Many thanks!

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