package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.utils.Timer;
import flash.events.TimerEvent;
/*
twitterAPIとYahoo!Pipesの練習。
クリックすると桜が咲いたり散ったりしますがその辺は適当です。
一応文字数に応じて桜の数が変わります。
http://twitter.com/satkaku
つぶやきが読めないのは仕様です。仕様がありませんね。
*/
[SWF(width=465,height=465,frameRate=30,backgroundColor=0xffffff)]
public class TwitterSakura extends Sprite
{
public var tf:TextFormat;
public var tFld:TextField;
public var s:Sprite;
public var tBmpData:BitmapData;
public var tBmp:Bitmap;
public var tL:uint;
public var sakura:Sakura;
public var sakuras:Array;
public var canvas:BitmapData;
public var canvasB:Bitmap;
public var ti:uint;
public var t:Timer;
public var rssLoader:URLLoader;
public var rssURL:URLRequest;
public var URLString:String;
public var myXML:XML;
public var myXMLList:XMLList;
public var tweet:String;
public function TwitterSakura()
{
init();
}
private function init():void
{
sakuras = new Array();
ti = 0;
s = new Sprite();
s.graphics.beginFill(0);
s.graphics.drawRect(0,0,465,465);
s.graphics.endFill();
addChild(s);
tf = new TextFormat();
tf.size = 14;
tf.color = 0xffffff;
tf.font = "_typewriter";
tf.bold = true;
tf.italic = true;
getTweet();
}
//====文字数に応じて桜を咲かせる====
private function bloom(e:MouseEvent):void
{
tL = tFld.length;
for(var i:uint = 0; i < tL; i++)
{
sakura = new Sakura(this.stage);
sakuras.push(sakura);
}
removeEventListener(MouseEvent.MOUSE_DOWN, bloom);
addEventListener(MouseEvent.MOUSE_DOWN, breakSakura);
}
//===桜を散らす===
private function breakSakura(e:MouseEvent):void
{
t = new Timer(100);
t.addEventListener(TimerEvent.TIMER, timerBreak);
t.start();
removeEventListener(MouseEvent.MOUSE_DOWN, breakSakura);
}
private function timerBreak(e:TimerEvent):void
{
sakuras[ti].breakS(canvas);
ti++;
if(ti == sakuras.length)
{
t.removeEventListener(TimerEvent.TIMER, timerBreak);
addEventListener(MouseEvent.MOUSE_DOWN, reset);
}
}
private function reset(e:MouseEvent):void
{
init();
}
//===Yahoo!Pipesからtweetを持ってくる=====
private function getTweet():void
{
//===URLはyahooapis.com、から=====
URLString = "http://pipes.yahooapis.com/pipes/pipe.run?_id=030f26b7e1c26ee7e30f47fead31b146&_render=rss";
rssURL = new URLRequest(URLString);
rssLoader = new URLLoader();
rssLoader.addEventListener(Event.COMPLETE, onLoadXML);
rssLoader.load(rssURL);
}
private function onLoadXML(e:Event):void
{
myXML = new XML(rssLoader.data);
tweet = myXML.channel.item[0].description;
//trace(tweet);
nextInit();
}
//===tweetをゲットしたのを確認してから残りのinit処理===
public function nextInit():void
{
tFld = new TextField();
tFld.autoSize = TextFieldAutoSize.LEFT;
tFld.defaultTextFormat = tf;
tFld.text = tweet;
tFld.wordWrap = true;
tFld.width = 250;
canvas = new BitmapData(465,465,false,0);
canvasB = new Bitmap(canvas);
s.addChild(canvasB);
tBmpData = new BitmapData(tFld.width,tFld.height,true,0);
tBmp = new Bitmap(tBmpData);
tBmpData.draw(tFld);
tBmp.height = 60;
tBmp.rotation = 90;
tBmp.x = 272;
tBmp.y = 465 - tFld.width;
s.addChild(tBmp);
addEventListener(MouseEvent.MOUSE_DOWN, bloom);
}
}
}
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.MouseEvent;
import flash.events.Event;
class Sakura extends Sprite
{
public var sc:Sprite;
public var sS:Object;
public var sBmpData:BitmapData;
public var sBmp:Bitmap;
public var cl:Number;
public var sps:Array;
public var oBmpData:BitmapData;
public function Sakura(s:Object)
{
sS = s;
initS();
}
private function initS():void
{
sps = new Array();
var sca:Number = 5 + Math.random() * 35;
sc = new Sprite();
sc.graphics.lineStyle(1,0xff00cc,Math.random() );
sc.graphics.beginFill(0xff99cc,Math.random() );
sc.graphics.drawCircle(sca,sca,sca);
sc.graphics.endFill();
sBmpData = new BitmapData(sc.width, sc.height,true,0);
sBmpData.draw(sc);
sBmp = new Bitmap(sBmpData);
sS.addChild(sBmp);
var big:Number = Math.random() * 3;
sBmp.x = 100 + Math.random() * 265;
sBmp.y = 30 + Math.random() * 300;
setS();
}
public function setS():void
{
cl = 0;
for(var i:Number = sc.x; i < sc.width; i++)
{
for(var j:Number = sc.y; j < sc.height; j++)
{
cl = sBmpData.getPixel(i,j);
if(cl != 0)
{
var sp:SakuraP = new SakuraP(sBmp.x + i,sBmp.y + j,cl);
sps.push(sp);
}
}
}
}
public function breakS(ob:BitmapData):void
{
sBmp.visible = false;
oBmpData = ob;
addEventListener(Event.ENTER_FRAME, onBreak);
}
private function onBreak(e:Event):void
{
var r:uint = sps.length;
oBmpData.lock();
for(var i:uint = 0; i < r; i++)
{
sps[i].y += Math.random() * 60;
sps[i].x += -20 + Math.random() * 40;
oBmpData.setPixel(sps[i].x, sps[i].y, sps[i].startC);
if(sps[i].y > 550)
{
removeEventListener(Event.ENTER_FRAME, onBreak);
}
}
oBmpData.unlock();
}
}
import flash.display.Sprite;
class SakuraP extends Sprite
{
public var startX:Number;
public var startY:Number;
public var startC:Number;
public function SakuraP(a:Number, b:Number,c:Number)
{
startC = c;
startX = a;
startY = b;
x = startX;
y = startY;
}
}