1

使用 Flash CS5.5 (AS3) 中“操作”中的此代码,我试图显示来自特定用户的推文。当我测试这部电影时,我在“输出”中看到了推文,但在舞台上却没有。如何让它们在舞台上可见?我想我需要一个 TextArea 或舞台上的东西来展示它们,但我不擅长编写 AS3 并且似乎无法使 TextArea 和 Actions-code 连接。

/**** vars ****/
var user:String;
var url:String;
var tweetCount:int;
var tweets:Array;
var times:Array;

/**** setup ****/
user = "leifpagrotsky";
tweetCount = 10;
loadTweets();

/**** start getting tweets ****/
function loadTweets()
{
url = "http://search.twitter.com/search.atom?q=+from:"+user+"&rpp="+tweetCount;
var urlReq:URLRequest = new URLRequest(url);
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, getTweets);
loader.addEventListener(IOErrorEvent.IO_ERROR, IOError);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, SError);
loader.load(urlReq);
}


function getTweets(e:Event):void
{
if ( e.target.data )
{
tweets = new Array(); times = new Array();
var twitterXML:XML = new XML(e.target.data);
var tweetList:XMLList = twitterXML.children();
var tweetItem:String; var timeItem:String;
for (var i:int = 0; i < tweetList.length(); i++)
{
tweetItem = tweetList[i].*::title;
timeItem = tweetList[i].*::published;
if ( tweetItem != "" )
{
tweets.push(tweetItem);
trace(tweetItem);
}
if ( timeItem != "" )
{
timeItem = timeItem.split("T").join(" - ");
timeItem = timeItem.split("Z").join("");
times.push(timeItem);
trace(timeItem);
}
}
trace(tweets.length);
}
}

function IOError(e:Event):void
{
trace("io error!");
}
function SError(e:Event):void
{
trace("security error!");
}
4

2 回答 2

0

我不明白textarea到底有什么问题

动态文本区域示例

import fl.controls.TextArea; 
var aTa:TextArea = new TextArea(); 
aTa.move(100,100); 
aTa.setSize(200, 200); 
aTa.condenseWhite = true; 
aTa.text = "sample"
addChild(aTa);

我希望这就是你要找的

于 2013-03-26T12:21:26.590 回答
-1

你正在寻找的是这样的:

import fl.controls.TextArea;
/**** vars ****/
var user:String;
var url:String;
var tweetCount:int;
var tweets:Array;
var times:Array;
var textArea:TextArea;

/**** setup ****/
user = "leifpagrotsky";
tweetCount = 10;
setupText();//setup a text area component
loadTweets();

/**** start getting tweets ****/
function loadTweets()
{
    url = "http://search.twitter.com/search.atom?q=+from:" + user + "&rpp=" + tweetCount;
    var urlReq:URLRequest = new URLRequest(url);
    var loader:URLLoader = new URLLoader();
    loader.addEventListener(Event.COMPLETE, getTweets);
    loader.addEventListener(IOErrorEvent.IO_ERROR, IOError);
    loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, SError);
    loader.load(urlReq);
}
function setupText():void{
    textArea = addChild(new TextArea()) as TextArea;//create and add it to the stage - make sure the component is in the library
    textArea.setSize(stage.stageWidth,stage.stageHeight);
    textArea.editable = false;
}

function getTweets(e:Event):void
{
    if (e.target.data)
    {
        var displayText:String = "";//make a string to collect all the text you want to display
        tweets = new Array();
        times = new Array();
        var twitterXML:XML = new XML(e.target.data);
        var tweetList:XMLList = twitterXML.children();
        var tweetItem:String;
        var timeItem:String;
        for (var i:int = 0; i < tweetList.length(); i++)
        {
            tweetItem = tweetList[i].*::title;
            timeItem = tweetList[i].*::published;
            if ( tweetItem != "" )
            {
                tweets.push(tweetItem);
                trace(tweetItem);
                displayText += tweetItem+"\n";//add the details you need like the message
            }
            if ( timeItem != "" )
            {
                timeItem = timeItem.split("T").join(" - ");
                timeItem = timeItem.split("Z").join("");
                times.push(timeItem);
                trace(timeItem);
                displayText += timeItem+"\n\n";//...and time
            }
        }
        trace(tweets.length);
        textArea.text = displayText;//finally after all the text is accumulated into a string, send it to the text area
    }
}

function IOError(e:Event):void
{
    trace("io error!");
}
function SError(e:Event):void
{
    trace("security error!");
}

在使用教程时,我建议将它们分开并理解每一部分。然后应该很明显需要做什么。简单地复制/粘贴代码而不理解它是不够的。

使用基本的 TextField 也可以。这是一个简化版本:

var output:TextField = addChild(setupText()) as TextField;//add text
loadTweets("leifpagrotsky",10);//call tweeter

function loadTweets(user:String,count:int):void{
    new URLLoader(new URLRequest("http://search.twitter.com/search.atom?q=+from:" + user + "&rpp=" + count))
                 .addEventListener(Event.COMPLETE,tweetsLoaded);//load and wait for the response

}
function tweetsLoaded(e:Event):void{
    if(e.target.data){//if we have some data
        var out:String = "";//make a new blank string we can later plug into the text on screen
        var stream:XML = new XML(e.target.data);//plug the data into an XML object
        var tweets:XMLList = stream.*;//get the tweet nodes
        var numTweets:int = tweets.length();//count them once
        for(var i:int = 0 ; i < numTweets; i++) //for each tweet message
            if(String(tweets[i].*::title).length) //if there the message's length is > 0
                out += tweets[i].*::title+"\n<b>"+tweets[i].*::published+"</b>\n\n";//add it to our string
        output.htmlText = out;//finally plug the string to the text field
    }
}

function setupText():TextField {//set up the text field
    var t:TextField = new TextField();
    t.defaultTextFormat = new TextFormat("Verdana",11,0);
    t.multiline = true;
    t.autoSize = TextFieldAutoSize.LEFT;
    t.width = stage.stageWidth;
    t.height = stage.stageHeight;
    t.border = true;
    return t;
}
于 2013-03-27T11:38:34.103 回答