数十枚、数百枚の画像を読み込む代わりに、flvから切り分ければ使える、というのはやったことがなかったのでやってみた。
ロード直後の一回は、flvを再生しながら、bitmap_arrayにbitmapを溜め込んでいる。
その後は角度に応じて、bitmap_arrayからbitmapを引き出してdrawしている。
1 2 3 |
var bm1:BitmapData = new BitmapData(160,120); bm1.draw(video_obj); bitmap_array.push(new Bitmap(bm1)); |
テストでは180、360枚でも問題なかったが、今回は、30fps,3秒、90枚の画像にした。目玉の動きはそこまで細かく分ける必要が無い、というのと、撮影する時にゆっくりスムーズに目玉を動かすのが難しかった、というのが主な理由。
medama2 – wonderfl build flash online
▼ActionScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
/* * * */ package { import flash.display.Sprite; import net.hires.debug.Stats; import flash.events.Event; import flash.events.NetStatusEvent; import flash.net.NetConnection; import flash.net.NetStream; import flash.media.Video; import flash.display.BitmapData; import flash.display.Bitmap; import flash.system.Security; import flash.geom.Matrix; import flash.events.MouseEvent; [SWF(backgroundColor="0x000000")] /** * ... * @author umhr */ public class Main extends Sprite { private var baseURL:String = ""; private var count:int; private var bmp_data : BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight , false , 0); //private var bmp : Bitmap = new Bitmap(bmp_data); private var stageWidth:int = stage.stageWidth; private var stageHeight:int = stage.stageHeight; private var sp:Sprite = Create.newSprite(null,[ ["buttonMode", true]]); function Main() { stage.scaleMode = "noScale"; stage.align = "TL"; //addChild(bmp); addChild(sp); addChild(new Stats()); addEventListener(MouseEvent.CLICK,onClick); stage.addEventListener(Event.RESIZE, resizeHandler); var bitmap_array:Array = new Array(); var imgLoadStatus:int = -1; if(isMztmjp()){ }else{ baseURL = "http://mztm.jp/wonderfl/"; Security.loadPolicyFile("http://mztm.jp/crossdomain.xml"); } var connection:NetConnection = new NetConnection(); connection.connect(null); var netStream:NetStream = new NetStream(connection); netStream.addEventListener (NetStatusEvent.NET_STATUS ,NET_STATUS); function NET_STATUS (event : NetStatusEvent):void { if(event.info.code == "NetStream.Play.Stop"){ imgLoadStatus = 1; video_obj.clear(); }else if(event.info.code == "NetStream.Play.Start"){ imgLoadStatus = 0; } } netStream.client = new Object(); netStream.play(baseURL+"medama2.flv"); var video_obj : Video = new Video(160,120); video_obj.attachNetStream(netStream); video_obj.y = stageHeight/2-60; addChild(video_obj) //書き込み処理を行い続ける。 stage.addEventListener(Event.ENTER_FRAME,ENTER_FRAME); var poz_array:Array = [0,0,0,0,0,0,0,0,0,0]; function ENTER_FRAME(e:Event):void { if(imgLoadStatus > 0){ }else if(imgLoadStatus == 0){ var bm1:BitmapData = new BitmapData(160,120); bm1.draw(video_obj); bitmap_array.push(new Bitmap(bm1)); video_obj.x += stageWidth/90; return; }else if(imgLoadStatus == -1){ return; } count++; //Macのマウス座標を補正するために。 var mouseX:Number = Math.min(Math.max(stage.mouseX,0),stageWidth); var mouseY:Number = Math.min(Math.max(stage.mouseY,0),stageHeight); var _x:Number; var r:int; var imgW:Number=0; var imgH:Number=0; var pozY:Number=0; var num:int; bmp_data.lock(); var s:Number = stageHeight*(8.4546/465) for (var j:int = 0; j < 10; j++) { pozY += imgH; imgH = (j+1)*s; imgW = 4*(imgH/3); num = Math.ceil(stageWidth/imgW)+2; //_x = (((mouseX - stageWidth / 2) / 200) * ((mouseY - (pozY + imgH / 2)) / 200) * count) % imgW; poz_array[j] += (((mouseX - stageWidth / 2) / 80) * ((mouseY - (pozY + imgH / 2)) / 80) ) _x = poz_array[j] % imgW; for (var i:int = 0; i < num; i++) { r = ((2-Math.atan2(mouseY-imgH/2-pozY+Math.random()*3,mouseX-imgW*i-_x+imgW/2+Math.random()*3)/Math.PI)*45)%90+Math.random()/4; //Matrixで0.005を足しているのは、計算上のサイズだと隣の画像との境目が目立つ場合があるから。 bmp_data.draw(bitmap_array[r],new Matrix(imgH/120+0.005,0,0,imgH/120+0.005,imgW*i+_x-imgW,pozY)); } } bmp_data.unlock(); Create.newSprite([,,,,sp],null,[["clear",null],["beginBitmapFill", [bmp_data]], ["drawRect", [0, 0, stageWidth,stageHeight]]]); } } private function isMztmjp():Boolean{ var _str:String = stage.loaderInfo.url; return (_str.substr(0,5) == "file:" || _str.indexOf("mztm.jp") > -1); } private function onClick(e:MouseEvent = null):void{ if(stage.displayState == "normal"){ stage.displayState = "fullScreen"; }else{ stage.displayState = "normal"; } } public function resizeHandler(e:Event = null):void { bmp_data.dispose(); bmp_data = new BitmapData(stage.stageWidth, stage.stageHeight , false , 0); //bmp = new Bitmap(bmp_data); stageWidth = stage.stageWidth; stageHeight = stage.stageHeight; } } } import flash.display.DisplayObject; import flash.display.Graphics; import flash.text.TextField; import flash.text.TextFieldType; import flash.text.TextFormat; import flash.display.Sprite; import flash.display.Shape; import flash.display.SimpleButton; import flash.events.MouseEvent; import flash.events.Event; import flash.events.KeyboardEvent; class Create{ public static var defaultTextFormat:TextFormat = new TextFormat(); public static function newSimpleButton(x_y_w_h_txt:Array = null,property:Array=null,graphics:Array=null):SimpleButton{ var upState:Sprite = newSprite([x_y_w_h_txt[0],x_y_w_h_txt[1]],null,[["beginFill",[0xCCCCCC,1]],["drawRoundRect",[0,0,x_y_w_h_txt[2],x_y_w_h_txt[3],8]]]); upState.addChild(newShape([2,2],null,[["beginFill",[0xE5E5E5,1]],["drawRoundRect",[0,0,x_y_w_h_txt[2]-4,x_y_w_h_txt[3]-4,6]]])) var overState:Sprite = newSprite([x_y_w_h_txt[0],x_y_w_h_txt[1]],null,[["beginFill",[0xBBBBBB,1]],["drawRoundRect",[0,0,x_y_w_h_txt[2],x_y_w_h_txt[3],8]]]); overState.addChild(newShape([2,2],null,[["beginFill",[0xEEEEEE,1]],["drawRoundRect",[0,0,x_y_w_h_txt[2]-4,x_y_w_h_txt[3]-4,6]]])) var downState:Sprite = newSprite([x_y_w_h_txt[0],x_y_w_h_txt[1]],null,[["beginFill",[0xAAAAAA,1]],["drawRoundRect",[0,0,x_y_w_h_txt[2],x_y_w_h_txt[3],8]]]); downState.addChild(newShape([2,2],null,[["beginFill",[0xDDDDDD,1]],["drawRoundRect",[0,0,x_y_w_h_txt[2]-4,x_y_w_h_txt[3]-4,6]]])) var hitTestState:Shape = newShape([x_y_w_h_txt[0],x_y_w_h_txt[1]],null,[["beginFill",[0,1]],["drawRoundRect",[0,0,x_y_w_h_txt[2],x_y_w_h_txt[3],8]]]); if(x_y_w_h_txt[4]){ upState.addChild(newTextField([0,2,x_y_w_h_txt[2],x_y_w_h_txt[3]-2],[["defaultTextFormat",new TextFormat("_sans", null, null, null, null, null, null, null, "center")],["text",x_y_w_h_txt[4]]])); overState.addChild(newTextField([0,2,x_y_w_h_txt[2],x_y_w_h_txt[3]-2],[["defaultTextFormat",new TextFormat("_sans", null, null, null, null, null, null, null, "center")],["text",x_y_w_h_txt[4]]])); downState.addChild(newTextField([0,3,x_y_w_h_txt[2],x_y_w_h_txt[3]-3],[["defaultTextFormat",new TextFormat("_sans", null, null, null, null, null, null, null, "center")],["text",x_y_w_h_txt[4]]])); } var sb:SimpleButton = new SimpleButton(upState,overState,downState,hitTestState); return sb; } public static function newShape(x_y_w_h_sh:Array = null,property:Array=null,graphics:Array=null):Shape{ var i:int; var sh:Shape; if(x_y_w_h_sh && x_y_w_h_sh[4]){ sh = x_y_w_h_sh[4]; }else{ sh = new Shape(); } if(x_y_w_h_sh){ if (x_y_w_h_sh[0]) { sh.x = x_y_w_h_sh[0] }; if (x_y_w_h_sh[1]) { sh.y = x_y_w_h_sh[1] }; } if(property){ for (i = 0; i < property.length; i++) { if(property[i] && property[i].length > 1){ sh[property[i][0]] = property[i][1]; } } } if(graphics){ for (i = 0; i < graphics.length; i++) { if(graphics[i] && graphics[i].length > 1){ sh.graphics[graphics[i][0]].apply(null, graphics[i][1]); } } } if(x_y_w_h_sh){ if (x_y_w_h_sh[2]) { sh.width = x_y_w_h_sh[2] }; if (x_y_w_h_sh[3]) { sh.height = x_y_w_h_sh[3] }; } return sh; } public static function newSprite(x_y_w_h_sp:Array = null,property:Array=null,graphics:Array=null,addChild:DisplayObject = null):Sprite{ var i:int; var sp:Sprite; if(x_y_w_h_sp && x_y_w_h_sp[4]){ sp = x_y_w_h_sp[4]; }else{ sp = new Sprite(); } if(x_y_w_h_sp){ if (x_y_w_h_sp[0]) { sp.x = x_y_w_h_sp[0] }; if (x_y_w_h_sp[1]) { sp.y = x_y_w_h_sp[1] }; } if(property){ for (i = 0; i < property.length; i++) { if(property[i] && property[i].length > 1){ sp[property[i][0]] = property[i][1]; } } } if(graphics){ for (i = 0; i < graphics.length; i++) { if(graphics[i] && graphics[i].length > 1){ sp.graphics[graphics[i][0]].apply(null, graphics[i][1]); } } } if(addChild){ sp.addChild(addChild); } if(x_y_w_h_sp){ if (x_y_w_h_sp[2]) { sp.width = x_y_w_h_sp[2] }; if (x_y_w_h_sp[3]) { sp.height = x_y_w_h_sp[3] }; } return sp; } public static function newTextField(x_y_w_h_txt_color_alpha:Array = null,property:Array=null,method:Array=null):TextField{ var i:int; var ta:TextField = new TextField(); ta.defaultTextFormat = defaultTextFormat; if(x_y_w_h_txt_color_alpha){ if (x_y_w_h_txt_color_alpha[0]) { ta.x = x_y_w_h_txt_color_alpha[0] }; if (x_y_w_h_txt_color_alpha[1]) { ta.y = x_y_w_h_txt_color_alpha[1] }; if (x_y_w_h_txt_color_alpha[2]) { ta.width = x_y_w_h_txt_color_alpha[2] }; if (x_y_w_h_txt_color_alpha[3]) { ta.height = x_y_w_h_txt_color_alpha[3] }; if (x_y_w_h_txt_color_alpha[4]) { ta.text = x_y_w_h_txt_color_alpha[4] }; if (x_y_w_h_txt_color_alpha[5]) { ta.textColor = x_y_w_h_txt_color_alpha[5] }; if (x_y_w_h_txt_color_alpha[6]) { ta.alpha = x_y_w_h_txt_color_alpha[6] }; } if(property){ for (i = 0; i < property.length; i++) { if(property[i] && property[i].length > 1){ ta[property[i][0]] = property[i][1]; } } } if(method){ for (i = 0; i < method.length; i++) { if(method[i] && method[i].length > 1){ ta[method[i][0]].apply(null, method[i][1]); } } } return ta; } } |