Actionscript 3 Designer Toolkit

More design, less programming

Actionscript 3 Designer Toolkit header image 2

Getting tweets from Twitter: Tweets Class

November 30th, 2009 · No Comments · AS3 Custom Classes, Apps

Last week I discovered BackTweets.com which allows you to search their database of Twitter links.   BackTweets.com also offers developers access to their Twitter data through an api.  The Tweets Class uses this api to collect a set of data based on a user specified search string.   The data is then displayed with other Designer Toolkit classes (sample below).

tweets

The Tweets class – unlike all previous classes discussed on this site – does not create a display object; so that there is no need to define its’ x, y position or use the addChild() method.  What the class does, is construct and return an array – passing the Twitter results – to the calling program.  Note that an event listener is attached to the Tweets instance that waits for a message from the class that the array has finished loading.  The listener then call the onDataReady function which processes the data in the array (tw) into display objects. To display the tweets, the program uses the HTMLText, HTLMTextMultiline and ImageLoader classes.   

Examining the class code – in the download – notice there is a public function named getResultSet which simply returns the completed array; it is this function that is called within onDataReady.

// import required dtk classes
import com.dtk.Tweets;
import com.dtk.ImageLoader;
import com.dtk.HTMLText;
import com.dtk.HTMLTextMultiline;
 
// instance of Tweets class with event listener attached
var tw:Tweets = new Tweets ( 'your_api_key', 'as3', 10, 2 );
tw.addEventListener( Event.COMPLETE, dataReadyHandler );
 
// function responding to tweets loaded
function dataReadyHandler ( e:Event ):void
{
	// get array data returned from Tweets class
	var tweets:Array = tw.getResultSet();
 
	// init starting y display position 
	var yPos:uint = 20;
 
	// loop through result set from search query from backstweets.com
	for ( var i:uint=0; i<tweets.length; i++ )
	{
		// rounded rectangle background
		var bg:Sprite = new Sprite();
                bg.graphics.beginFill( 0xffffff );
                bg.graphics.lineStyle( 0, 0x999999 );
                bg.graphics.drawRoundRect( 15, yPos-5, 460, 80, 16 ); 
                bg.graphics.endFill();
                addChild(bg);
 
		// load twitter user image
		var loader:ImageLoader = new ImageLoader( tweets[i].image, 48, 48, 0xfd4221 );
		loader.x = 25;
		loader.y = yPos + 12;
		addChild( loader );
 
		// mask used to limit size of oversized profile images
		var masker:Sprite = new Sprite();
		masker.graphics.beginFill( 0x999999 );
		masker.graphics.drawRect( 25, yPos+12, 48, 48 );
		masker.graphics.endFill();
		loader.mask = masker;
 
		// display user name
		var user:HTMLText = new HTMLText( 100, 'Verdana', 11, 0x333333, 'LEFT', tweets[i].user );
		user.x = 90;
		user.y = yPos + 2;
		addChild( user );
 
		// display date & time created
		var ctime:HTMLText = new HTMLText( 100, 'Verdana', 11, 0x333333, 'LEFT', tweets[i].created );
		ctime.x = 320;  // 380
		ctime.y = yPos + 2;
		addChild( ctime );
 
		// display text of tweet including url
		var twText:HTMLTextMultiline = new HTMLTextMultiline( 380, 'Verdana', 11, 0x333333, 'LEFT', 0, tweets[i].twText );  
		twText.x = 90;
		twText.y = yPos + 24;
		addChild( twText );
 
		// increment yPos to next position
		yPos += 86;
	}
}

All the classes for the above program are included in the download below.

NOTE: in order to use backtweets.com, you must first get a developers api key at backtype.com/developers

Download files: Tweets.zip (182)

If you found this post helpful please consider a Retweet.  Thanks…

Post to Twitter

Tags: ···

No Comments so far ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment

Spam Protection by WP-SpamFree