topical media & game development
#display-collide.mx
#display-collide.mx
[swf]
flex
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:ae="*"
paddingLeft="0" paddingTop="0" paddingBottom="0" paddingRight="0"
backgroundColor="0x000000"
layout="absolute"
initialize = "init();"
>
<ae:component_screen id="display"/>
script
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.CuePointEvent;
import mx.controls.videoClasses.CuePointManager;
[Bindable] public var first:String = "../assets/clips/tube/balloon.flv";
[Bindable] public var movie:String = first;
[Bindable] private var arrColl:ArrayCollection = new ArrayCollection();
[Bindable] private var state:uint = 0;
private var off:int = 0;
[Bindable] private var radius:uint = 100;
[Bindable] private var ball:Sprite = new Sprite();
[Bindable] private var square:Sprite = new Sprite();
[Bindable] private var slist:Sprite = new Sprite();
[Bindable] private var vx:Number;
[Bindable] private var vy:Number;
[Bindable] private var bounce:Number = -1.0; //-0.9;
[Bindable] private var gravity:Number = 0; //-0.001;
private var oldX:Number;
private var oldY:Number;
private var sprites:Array;
private var smax:uint = 0;
private var avx:Array;
private var avy:Array;
private var arz:Array;
private function init() : void {
avx = new Array();
avy = new Array();
arz = new Array();
input.text = first;
myVid.source = movie;
off = 1;
var h:Number = systemManager.stage.stageHeight;
var w:Number = systemManager.stage.stageWidth;
ball.graphics.beginFill(0xff0000);
ball.graphics.drawCircle(0, 0, radius);
ball.x = 200;
ball.y = 200;
//avx.push(Math.random() * 30 - 5);
//avy.push(-20);
//slist.addChild(ball);
sprites = new Array();
//sprites.push(ball);
myVid.addChild(slist);
//myVid.mask = slist;
addEventListener(Event.ENTER_FRAME, frame);
addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
}
camera
import flash.media.Camera;
private var camera:Camera; // = Camera.getCamera();
private function attach(v:Object) : void {
camera = Camera.getCamera();
myVid.attachCamera(camera);
}
frame(s)
private function frame(event:Event):void {
var h:Number = systemManager.stage.height;
var w:Number = systemManager.stage.width;
for(var i:uint = 0; i < smax; i++) {
vx = avx[i]; vy = avy[i];
var dx:Number = 0; //Math.random() * 2 - 1;
var dy:Number = 0; //Math.random() * 2 - 1 ;
vx += dx; vy += dy + gravity;
var s:Number = w/800;
var left:Number = 0;
var right:Number = myVid.width;
var top:Number = 0;
var bottom:Number = myVid.height;
// for(var i:uint = 0; i < 2; i++) {
var ball:Sprite = sprites[i];
var bounds:Rectangle = ball.getBounds(myVid);
ball.x += vx;
ball.y += vy;
var cx:Number = bounds.x + bounds.width/2;
var cy:Number = bounds.y + bounds.height/2;
for(var j:uint = i; j < smax; j++) {
var other:Rectangle = sprites[j].getBounds(myVid);
var ocx:Number = other.x + other.width/2;
var ocy:Number = other.y + other.height/2;
if (((bounds.right > other.left && cx < other.left) && ((bounds.bottom > other.top && cy < other.top) || (bounds.top < other.bottom && cy > other.bottom)) ) ||
((bounds.left < other.right && cx > other.right) && ((bounds.bottom > other.top && cy < other.top) || (bounds.top < other.bottom && cy > other.bottom))) )
{
var ddx:Number = cx - ocx;
var ddy:Number = cy - ocy;
if (ddx*ddx < ddy*ddy) {
vx *= bounce; avx[j] *= bounce;
ball.x += 3 * vx;
} else { vy *= bounce; avy[j] *= bounce;
ball.y += 3 * vy;
}
}
}
if((bounds.right > right) || (bounds.left < left))
{
vx *= bounce;
ball.x += 5 * vx;
}
if((bounds.bottom > bottom) || (bounds.top < top))
{
vy *= bounce;
ball.y += 5 * vy;
}
avx[i] = vx; avy[i] = vy;
}
}
mouse
private var drawing:uint = 0;
private function onMouseDown(event:MouseEvent):void
{
var h:Number = systemManager.stage.height;
var w:Number = systemManager.stage.width;
if (mouseY < h-50 && mouseY > 50 &&
mouseX < w-50 && mouseX > 50) {
drawing = 1;
// myVid.mask = null;
// story.text = "m:" + mouseX + " " + mouseY;
oldX = mouseX; oldY = mouseY;
ball = new Sprite();
myVid.addChild(ball);
//panel.addChild(ball);
square = new Sprite();
slist.addChild(square);
ball.graphics.moveTo(mouseX, mouseY);
ball.graphics.lineStyle(4,0xff0000);
//ball.graphics.beginFill(0xff0000);
square.graphics.moveTo(mouseX, mouseY);
square.graphics.beginFill(0xff0000);
addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
story.text = "m:" + mouseX + " " + mouseY;
}
}
private function onMouseUp(event:MouseEvent):void
{
smax += 1;
story.text = "";
square.graphics.endFill();
slist.addChild(square);
myVid.removeChild(ball);
avx.push(Math.random() * 20 - 5);
avy.push(-5);
arz.push(Math.random() * 0.2 - 0.1);
sprites.push(square);
myVid.mask = slist;
removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
var bounds:Rectangle = square.getBounds(myVid);
// story.text = "s: " + square.x + "/" + square.y + "(" + bounds.left + "," + bounds.top + "," + bounds.width + "," + bounds.height + ")";
}
private function onMouseMove(event:MouseEvent):void
{
if (drawing == 1) {
square.graphics.lineTo(mouseX, mouseY);
ball.graphics.lineTo(mouseX, mouseY);
// square.graphics.drawCircle(mouseX, mouseY, 10);
// story.text = "m:" + mouseX + " " + mouseY;
} else { story.text = "???"; }
}
hide
private var peep:uint = 0;
private function hide(video:Object):void {
if (peep == 0) {
myVid.removeChild(slist);
myVid.mask = null;
peep = 1;
} else {
peep = 0;
myVid.addChild(slist);
myVid.mask = slist;
addEventListener(Event.ENTER_FRAME, frame);
}
}
cuepoint(s)
[Bindable] private var cuepoints:Array = [
{name:'cue 0', time:1.00},
{name:'cue 1', time:2.00},
];
[Bindable] private var info:String = "cue -";
[Bindable] private var show:uint = 0;
clear
private function clear(video:Object):void {
if (show == 0) {
show = 1; tileList.visible = false;
arrColl.removeAll();
tileList.invalidateList();
}
else { show = 0; tileList.visible = true; }
}
select
private function select(event:Event):void {
myVid.close();
tileList.visible = false;
arrColl.removeAll();
tileList.invalidateList();
tileList.visible = true;
show = 0;
movie = input.text;
myVid.source = movie;
myVid.cuePoints = cuepoints;
story.text = movie;
myVid.play();
off = 0;
state = 0;
}
cue(s)
private function cue(evt:Object):void {
if (show == 1) { show = 0; tileList.visible = true; }
var pad:String = " ";
var bm:Bitmap = copy(myVid as DisplayObject);
var time:String = format(myVid.playheadTime);
if (state < 2) info = cuepoints[state].name;
if (state == 0) { story.text = "click ! to annotate ..."; }
else if (state == 1) { story.text = "... "; }
else { off = 1; story.text = ""; info="cue " + state; }
arrColl.addItem({pad:pad, bitmap:bm, info:info, time:time});
state += 1;
if (off == 1) story.text = "";
}
copy / format
private function copy(source:DisplayObject):Bitmap {
var bmd:BitmapData = new BitmapData(source.width, source.height);
bmd.draw(source);
return new Bitmap(bmd);
}
private function format(value:Number):String{
var sec:int = (value * 60) / 60;
var mil:int = (value - sec) * 100;
var result:String = sec.toString() + "." + mil.toString();
return result;
}
]]>
</mx:Script>
display
<mx:VideoDisplay x="0" y="0" id="myVid" source="" height="100%" width="100%" autoPlay="false"
cuePointManagerClass="mx.controls.videoClasses.CuePointManager"
cuePoint="cue(event)"
/>
<mx:HBox top="5" right="3">
<mx:Label text="{movie} / {state}" color="silver" top="10" right="20"/>
<mx:Label color="gray" text="@" click="attach(myVid);"/>
</mx:HBox>
<mx:HBox top="5" left="20">
<mx:Label color="gray" text="*" click="hide(myVid);"/>
<mx:Label text="{format(myVid.playheadTime)} / {info}" color="white" top="10" left="20"/>
</mx:HBox>
<mx:Label id="story" text="" color="white" top="75" left="100" fontSize="20"/>
controls
<mx:HBox left="20" bottom="10">
<mx:Button color="white" borderColor="0" fillAlphas="[0,0]" label="!" click="cue(myVid);"/>
<mx:TextInput borderColor="gray" color="silver" backgroundAlpha="0" width="275" id="input" text="" enter="select(event);"/>
<mx:Button color="white" borderColor="0" fillAlphas="[0,0]" label="[!]" click="select(event);"/>
<mx:Button color="silver" borderColor="0" fillAlphas="[0,0]" label="clear" click="clear(myVid);"/>
</mx:HBox>
<mx:HBox right="5" bottom="10">
<mx:Button color="gray" borderColor="0" fillAlphas="[0, 0]" label=">" click="myVid.play();"/>
<mx:Button color="gray" borderColor="0" fillAlphas="[0, 0]" label="||" click="myVid.pause();"/>
<mx:Label color="gray" text="#" click="display.toggle();"/>
</mx:HBox>
<mx:Canvas id="panel" x="0" y="0" height="100%" width="100%"
backgroundAlpha="0.3" borderColor="0" borderThickness="0"/>
tile(s)
<mx:TileList
top="50" right="10"
backgroundAlpha="0"
borderColor="0"
borderThickness="0"
id="tileList"
columnCount="1" dataProvider="{arrColl}"
width="170"
height="{myVid.height - 100 }"
verticalScrollPolicy="off">
<mx:itemRenderer>
<mx:Component>
<mx:HBox right="0" paddingBottom="0" paddingTop="0">
<mx:Text textAlign="right">
<mx:htmlText>
<br;\ >
name: {data.info}
time: {data.time}
</mx:htmlText>
</mx:Text>
<mx:HBox right="0">
<mx:Image source="{data.bitmap}" toolTip="{data.time}"
maintainAspectRatio="false"
scaleX="0.07" scaleY="0.07" />
</mx:HBox>
</mx:HBox>
</mx:Component>
</mx:itemRenderer>
</mx:TileList>
</mx:Application>
(C) Æliens
27/08/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.