1

With this code in "Actions" in Flash CS5.5 (AS3) I'm trying to show tweets from a specific user. When I test the movie I see the tweets in "Output", but not on the stage. How do I make them visible on the stage? I guess I need a TextArea or something on the stage to show them, but I'm not good at writing AS3 and can't seem to make the TextArea and the Actions-code connect.

/**** 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!");
}
  • "can't seem to make the TextArea and the Actions-code connect." What have you tried? – Jonatan Hedborg Mar 26 '13 at 8:58
  • @JonatanHedborg I threw that code away, and I don't remember it. But is that what the problem is, that I need to connect the Actions with a TextArea? – user1390252 Mar 26 '13 at 10:54
  • something like yourTextAreaInstance.text = "Your Message"; ? – George Profenza Mar 26 '13 at 11:22
  • @GeorgeProfenza What do you mean I should do with that? – user1390252 Mar 26 '13 at 13:57
  • say you have a TextArea component on stage and you've setup the instance name "yourTextAreaInstance" simply use the .text property to set the text of your tweet. See tired's answer bellow as well (make sure you have the TextArea component in your document's library 1st of course) – George Profenza Mar 26 '13 at 14:04
-1

What you're looking for is something like this:

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

What I recommend when using tutorials is taking them apart and understanding each piece. Then it should become obvious what need to do. Simply copy/pasting code without understanding it is not enough.

Using a basic TextField could also work. Here's a simplified version:

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;
}
| improve this answer | |
0

I do not understand exactly what the problem with the textarea

Example of a dynamic 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);

I hope this is what you're looking for

| improve this answer | |
  • Hmm, my problem is I can't see the tweets on the stage (only in "Output"), and I guess that means I need something like for example a TextArea to show it. So I need to CONNECT my Actions-code with a TextArea. – user1390252 Mar 26 '13 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.