简体   繁体   中英

Animate XML data in Flash with Actionscript 3

I have a question about getting a specific animation added to some xml data I'm pulling. I need some advice on how to make the data move from left to right or vise versa.Just for example I downloaded a rss feed from BBC world news, so it's just an xml file. Both the flash and xml are saved in the same folder and I can get in flash and display the data. Here's are the code so far:

import flash.text.TextField;
import flash.sampler.StackFrame;
import flash.display.MovieClip;

var yPlacement:int = 20;
var xPlacement:int = 30;
var distance:int = 60;

var loader:URLLoader = new URLLoader();
loader.load(new URLRequest("bbc-worldnews-rss.xml"));
loader.addEventListener(Event.COMPLETE, handleComplete);


function handleComplete(event:Event):void
{
var rawXML:XML = new XML(loader.data);
rawXML.ignoreWhite = true;
//trace(rawXML.channel.image.url);

var items:XMLList = rawXML.channel.item;
//trace("Total new items", items.length());

for each (var item:XML in items)
{
    //trace(item.title);
    var feedTitle:String = item.title.toString();

    var myText:TextField = new TextField();
    myText.text = feedTitle;
    myText.autoSize = TextFieldAutoSize.LEFT;
    myText.x = 2;
    myText.y = 2;

    var clip_mc = new MovieClip();
    clip_mc.addChild(myText);

    addChild(clip_mc);

    clip_mc.y = yPlacement;
    clip_mc.x = xPlacement;

    yPlacement = yPlacement + distance;


 }
    //trace("First item title:", item[0].title);


}

I also know the code that makes text move side to side but I don't know how to incorporate it my codes above:

  onClipEvent ( load ) {
  startPoint = 1280; //this is where the clip will start
  endPoint = -1080;  //this is where the clip will end, and restart to the startPoint.
  speed = 5; //this is how many pixels the text will move each frame.
}

onClipEvent ( enterFrame ) {
   this._x -= speed; //you are telling the MC to move to the left 5 pixels each frame.

  if (this._x <= endPoint ) { //if your clip goes beyond the end point.
    this._x = startPoint; //go back to the starting point.
  }
}

I hope I'm not confusing anyone, I just need to get the data I get from xml file to move side to side... I may be complete off course but I would GREATLY appreciate anyone's help!

Thank you,

For starters, the second snippet of code you posted is actually ActionScript 2, not 3.

You'd need to update that snippet to AS3 in order for this to work. Try something like this:

var startPoint:int = 1280;
var endPoint:int = -1080;
var speed:int = 5;    

function moveMC(mc:MovieClip):void {
    mc.addEventListener(Event.ENTER_FRAME, tick);
}

function tick(e:Event):void {
    e.currentTarget.x -= speed;

    if (e.currentTarget.x <= endPoint) {
        e.currentTarget.x = startPoint;
    }
}

You could then call moveMC() after you've added your newly created MovieClip to the stage.

Edit: You can use that snippet right in your for each loop like this:

for each (var item:XML in items)
{
    //trace(item.title);
    var feedTitle:String = item.title.toString();

    var myText:TextField = new TextField();
    myText.text = feedTitle;
    myText.autoSize = TextFieldAutoSize.LEFT;
    myText.x = 2;
    myText.y = 2;

    var clip_mc = new MovieClip();
    clip_mc.addChild(myText);

    addChild(clip_mc);

    clip_mc.y = yPlacement;
    clip_mc.x = xPlacement;

    yPlacement = yPlacement + distance;

    //takes in reference to MovieClip, start point, end point and speed
    moveMC(clip_mc, 1280, -1080, 5);
 }

function moveMC(mc:MovieClip, startPoint:int, endPoint:int, speed:int):void {

    mc.startPoint = startPoint;
    mc.endPoint = endPoint;
    mc.speed = speed;

    mc.addEventListener(Event.ENTER_FRAME, tick);
}

function tick(e:Event):void {
    var mc:MovieClip = e.currentTarget as MovieClip;
    mc.x -= mc.speed;

    if (mc.x <= mc.endPoint) {
        mc.x = mc.startPoint;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM