Twitter + AS3?

时间:2013-03-26 08:24:55

标签: actionscript-3 twitter flash-cs5

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

/**** 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!");
}

2 个答案:

答案 0 :(得分:0)

我完全不明白textarea的问题是什么

动态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);

我希望这就是你要找的东西

答案 1 :(得分:-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;
}