flash on 2010-6-10
♥0 |
Line 102 |
Modified 2010-06-10 17:04:55 |
MIT License
archived:2017-03-20 14:15:45
ActionScript3 source code
/**
* Copyright aaharu ( http://wonderfl.net/user/aaharu )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/4a8l
*/
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
public class ipl_flash_2010_04_p4 extends Sprite {
private const N:uint = 200;
private var array:Array = [];
private var isMouseDown:Boolean = false;
private var mouseDownX:Number = 0;
private var mouseDownY:Number = 0;
public function ipl_flash_2010_04_p4() {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
for(var i:uint = 0; i < N; i++) {
var sp:myClass = new myClass(0, Math.random() * 10 + 1, Math.random() * 5 + 10, Math.random() * 0xFFFFFF);
sp.x = Math.random() * stage.stageWidth;
sp.y = -Math.random() * 100 - 10;
array.push(sp);
addChild(sp);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
}
private function onMouseDown(e:MouseEvent):void {
isMouseDown = true;
mouseDownX = mouseX;
mouseDownY = mouseY;
for(var i:uint = 0; i < N; i++) {
var n:Number = Math.random() * 10 + 10;
var arc:Number = Math.atan2(mouseY - array[i].y, mouseX - array[i].x);
array[i].vx = Math.cos(arc) * n;
array[i].vy = Math.sin(arc) * n;
}
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
private function onMouseUp(e:MouseEvent):void {
isMouseDown = false;
for(var i:uint = 0; i < N; i++) {
if(array[i].x == mouseDownX && array[i].y == mouseDownY) {
array[i].vx = Math.random() * 40 - 20;
array[i].vy = Math.random() * 40 - 20;
}
}
stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
private function onEnterFrame(e:Event):void {
for(var i:uint = 0; i < N; i++) {
array[i].x += array[i].vx;
array[i].y += array[i].vy;
if(isMouseDown) {
if((array[i].vx < 0 && array[i].x < mouseDownX) || (array[i].vx > 0 && array[i].x > mouseDownX)) {
array[i].x = mouseDownX;
array[i].vx = 0;
}
if((array[i].vy < 0 && array[i].y < mouseDownY) || (array[i].vy > 0 && array[i].y > mouseDownY)) {
array[i].y = mouseDownY;
array[i].vy = 0;
}
} else {
array[i].vy++;
}
if(array[i].x > stage.stageWidth - 10) {
array[i].x = stage.stageWidth - 10;
array[i].vx = -array[i].vx / 2;
} else if(array[i].x < 10) {
array[i].x = 10;
array[i].vx = -array[i].vx / 2;
}
if(array[i].y > stage.stageHeight - 10) {
array[i].y = stage.stageHeight - 10;
array[i].vy = -array[i].vy / 2;
}
}
}
}
}
import flash.display.Sprite;
class myClass extends Sprite {
private var _vx:Number;
private var _vy:Number;
public function get vx():Number {
return _vx;
}
public function set vx(v:Number):void {
_vx = v;
}
public function get vy():Number {
return _vy;
}
public function set vy(v:Number):void {
_vy = v;
}
public function myClass(vx:Number, vy:Number, r:Number, color:uint) {
_vx = vx;
_vy = vy;
graphics.beginFill(color);
graphics.drawCircle(0, 0, r);
graphics.endFill();
}
}