2013-03-26 49 views
1

在Flash CS5.5(AS3)的「動作」中使用此代碼我試圖顯示來自特定用戶的推文。當我測試電影時,我在「輸出」中看到推文,但不在舞臺上。 如何讓他們在舞臺上可見?我想我需要一個TextArea或舞臺上的東西來展示它們,但我不擅長編寫AS3,並且似乎無法使TextArea和Actions-code連接。Twitter + AS3?

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

「似乎無法使TextArea和Actions-code連接。」你有什麼嘗試? – 2013-03-26 08:58:41

+0

@JonatanHedborg 我把這段代碼扔掉了,我不記得它。但是,問題在於,我需要將Actions和TextArea連接起來嗎? – user1390252 2013-03-26 10:54:25

+0

像'yourTextAreaInstance.text =「您的消息」;'? – 2013-03-26 11:22:55

回答

-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; 
} 
+0

非常感謝!它實際上起作用了!(我用了第一個) – user1390252 2013-03-28 08:34:54

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

希望的

例子的問題,這是你在找什麼

+0

嗯,我的問題是我看不到在舞臺上的鳴叫(只在「輸出」),我想這意味着我需要像例如一個TextArea來顯示它。所以我需要連接我的動作代碼與TextArea 。 – user1390252 2013-03-26 14:00:21