Starling Particle Test
Scoutテスト用にちょっとStarlingで作りました。
マウスダウンでぶりぶり生まれます。
マウスコリコリで重力の方向が変わります。
♥2 |
Line 275 |
Modified 2013-03-01 13:15:53 |
MIT License
archived:2017-03-10 15:22:18
ActionScript3 source code
/**
* Copyright kawamura ( http://wonderfl.net/user/kawamura )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/eNqz
*/
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.setInterval;
import starling.core.Starling;
/**
* ...
* @author jaiko
*/
public class Main extends flash.display.Sprite
{
private var maruList:Array;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:flash.events.Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var viewManager:ViewManager = ViewManager.getInstance();
addChild(viewManager);
}
}
}
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import starling.core.Starling;
/**
* ...
* @author jaiko
*/
class ViewManager extends flash.display.Sprite
{
private static var _gravityTheta:Number;
//
private static var _instance:ViewManager;
private static var g:Graphics;;
private static var cx:Number;
private static var cy:Number;
private static var tf:TextField;
private static var backGround:flash.display.Sprite;
public var starling:Starling;
public function ViewManager(block:SingletonBlock)
{
if (stage) init();
addEventListener(flash.events.Event.ADDED_TO_STAGE, init);
}
public static function getInstance():ViewManager
{
if (_instance == null)
{
_instance = new ViewManager(new SingletonBlock());
}
return _instance;
}
private function init(e:flash.events.Event = null):void
{
removeEventListener(flash.events.Event.ADDED_TO_STAGE, init);
//
layout();
}
private function layout():void
{
_gravityTheta = 2 * Math.PI * Math.random();;
g = this.graphics;
cx = stage.stageWidth * 0.5;
cy = stage.stageHeight * 0.5;
/**/
starling = new Starling(StarlingManager, stage, null, null);
starling.enableErrorChecking = true;
starling.start();
backGround = new flash.display.Sprite();
addChild(backGround)
var g_back:Graphics;
g_back = backGround.graphics;
g_back.beginFill(0xFFFFFF,0.8);
g_back.drawRect(0, 0, 200, 20)
tf = new TextField();
addChild(tf);
tf.height = 20;
//
stage.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelListener);
}
private function mouseWheelListener(e:MouseEvent):void
{
//trace(e.delta);
_gravityTheta += e.delta * 0.1;
}
public static function enterFrameListener(event:flash.events.Event = null):void
{
tf.text = String(StarlingManager(_instance.starling.root).squareNumber);
backGround.width = tf.textWidth+5;
_gravityTheta += 0.01;
if (_gravityTheta > 2 * Math.PI)
{
_gravityTheta -= 2 * Math.PI;
}
else if ( _gravityTheta < 0)
{
_gravityTheta += 2 * Math.PI;
}
var _x:Number;
var _y:Number;
_x = cx + 20 * Math.cos(_gravityTheta);
_y = cy + 20 * Math.sin(_gravityTheta);
g.clear();
g.lineStyle(2, 0xFF0000);
g.drawCircle(cx, cy, 20);
g.moveTo(cx, cy);
g.lineTo(_x, _y);
}
static public function get gravityTheta():Number
{
return _gravityTheta;
}
}
class SingletonBlock {
}
import flash.geom.Point;
import flash.utils.clearInterval;
import flash.utils.setInterval;
import starling.display.Quad;
import starling.core.Starling;
import starling.display.Sprite;
import starling.events.Event;
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.events.TouchPhase;
/**
* ...
* @author jaiko
*/
class StarlingManager extends starling.display.Sprite
{
private var squareList:Array;
private var id:uint;
private var _mouseX:Number;
private var _mouseY:Number;
private var _squareNumber:Number = 100;
public function StarlingManager()
{
this.addEventListener(starling.events.Event.ADDED_TO_STAGE, init);
}
private function init(event:starling.events.Event = null):void
{
this.removeEventListener(starling.events.Event.ADDED_TO_STAGE, init);
//
layout();
}
public function layout():void
{
trace( "StarlingManager.layout" );
/**/
var i:uint;
var n:uint;
var square:CustomSquare;
squareList = [];
n = _squareNumber;
for (i = 0; i < n; i++)
{
square = addSquare();
square.x = stage.stageWidth * Math.random();
square.y = stage.stageHeight * Math.random();
}
addEventListener(starling.events.Event.ENTER_FRAME, enterFrameListener);
stage.addEventListener(TouchEvent.TOUCH, touchListener);
}
private function touchListener(e:TouchEvent):void
{
var began:Touch = e.getTouch(stage, TouchPhase.BEGAN);
var ended:Touch = e.getTouch(stage, TouchPhase.ENDED);
var moved:Touch = e.getTouch(stage, TouchPhase.MOVED);
var point:Point;
if (began) {
if (began.phase == "began")
{
id = setInterval(addClosure, 1);
point = began.getLocation(this);
_mouseX = point.x;
_mouseY = point.y;
}
}
if (ended)
{
if (ended.phase == "ended")
{
clearInterval(id);
}
}
if (moved)
{
if (moved.phase == "moved")
{
point = moved.getLocation(this);
_mouseX = point.x;
_mouseY = point.y;
}
}
}
private function addClosure():void
{
var i:uint;
var n:uint;
var square:CustomSquare;
n = 4 + Math.floor(7 * Math.random());
for (i = 0; i < n; i++)
{
square = addSquare();
square.x = _mouseX;
square.y = _mouseY;
}
}
public function enterFrameListener(event:starling.events.Event):void
{
var i:uint;
var n:uint;
var square:CustomSquare;
n = squareList.length;
for (i = 0; i < n; i++)
{
square = squareList[i];
square.onEnterFrame();
}
//
ViewManager.enterFrameListener();
}
private function addSquare():CustomSquare
{
var square:CustomSquare;
square = new CustomSquare();
addChild(square);
squareList.push(square);
return square;
}
public function get squareNumber():Number
{
_squareNumber = this.numChildren;
return _squareNumber;
}
}
import starling.display.Quad;
import starling.display.Sprite;
//import starling.events.Event;
/**
* ...
* @author jaiko
*/
class CustomSquare extends starling.display.Sprite
{
private const LENGTH:Number = 5;
private const SPEED:Number = 10;
private var vx:Number = 0;
private var vy:Number = 0;
public function CustomSquare()
{
var square:Quad = new Quad(LENGTH, LENGTH, 0);
square.x = -0.5 * LENGTH;
square.y = -0.5 * LENGTH;
addChild(square);
var theta:Number = 2 * Math.PI * Math.random();
vx = SPEED * Math.cos(theta);
vy = SPEED * Math.sin(theta);
}
/**/
public function onEnterFrame():void
{
var ax:Number = 1 * Math.cos(ViewManager.gravityTheta);
var ay:Number = 1 * Math.sin(ViewManager.gravityTheta);
vx += ax- 0.01*vx;
vy += ay- 0.01*vy;
this.x += vx;
this.y += vy;
if (this.x < 0) {
vx *= -1;
vy = vy - 0.1 + 0.2 * Math.random();
this.x =0
}
else if (this.x > stage.stageWidth)
{
vx *= -1;
vy = vy - 0.1 + 0.2 * Math.random();
this.x = stage.stageWidth;
}
if (this.y < 0) {
vx = vx - 0.1 + 0.2 * Math.random();
vy *= -1;
this.y = 0;
}
else if (this.y > stage.stageHeight)
{
vx = vx - 0.1 + 0.2 * Math.random();
vy *= -1;
this.y = stage.stageHeight;
}
}
}