読もう!!!とぎれなくて曲がるコミックビーム
package{
import flash.display.*;
import flash.events.*;
public class Beam06 extends Sprite{
private var spring:Spring;
private var head:BeamHead;
private function onEnterFrame(e:Event):void {
spring.draw();
head.x = mouseX;
head.y = mouseY;
}
public function Beam06() {
spring = new Spring;
head = new BeamHead;
addChild(spring);
addChild(head);
addEventListener(Event.ENTER_FRAME,onEnterFrame );
}
}
}
import flash.display.Sprite;
import flash.filters.GlowFilter;
import flash.geom.Point;
import flash.events.Event;
class Spring extends Sprite{
private var dist:Number = 10;//25;
private var k:Number = .80;//.55;
private var damp:Number = .5//.99;
private var cx:Number = 270;
private var cy:Number = 200;
private var points:Array = [];
private var numPoints:int = 15;
private var oneSlice:Number = Math.PI * 2 / numPoints;
private var radius:Number = 100;
public function Spring(){
for (var i:int = 0; i < numPoints; i++)
{
var angle:Number = oneSlice * i;
var x:Number = Math.cos(angle) * radius + cx;
var y:Number = Math.sin(angle) * radius + cy;
points[i] = {x:x, y:y, a:angle - Math.PI / 2,vx:0,vy:0};
}
filters = [new GlowFilter()];
}
private const unit:Number = 4.0;
public function draw():void{
graphics.clear();
points[numPoints-1].x = mouseX;
points[numPoints-1].y = mouseY;
graphics.moveTo(points[0].x, points[0].y);
var pts:Array=[];
graphics.lineStyle(3, 0xffff00);
for (var i:int = 1; i < numPoints; i++)
{
var distX:Number = points[i].x - points[i-1].x;
var distY:Number = points[i].y - points[i-1].y;
var dist:Number = Math.sqrt(distX*distX+distY*distY);
var thick:Number = 40;
var rad:Number = Math.atan2(distY,distX)+Math.PI/2;
for(var j:int = dist/unit-1;j>=0;j--){
var p:Point =new Point(points[i].x+Math.random()*thick-thick/2,
points[i].y+j*unit);
var dx:Number = p.x - points[i].x;
var dy:Number = p.y - points[i].y;
p.x = points[i].x + Math.cos(rad) * (dx) - Math.sin(rad) * (dy);
p.y = points[i].y + Math.sin(rad) * (dx) + Math.cos(rad) * (dy);
pts.push(p);
}
graphics.lineTo(points[i].x, points[i].y);
}
graphics.lineStyle(2, 0xff);
graphics.moveTo(pts[0].x,pts[0].y);
for(i=1;i<pts.length;i++){
graphics.lineTo(pts[i].x,pts[i].y);
}
var totalx:int = 0;
var totaly:int = 0;
for (i = 0; i < numPoints - 1; i++)
{
var point:Object = points[i];
spring(point, points[i + 1]);
}
for (i = 1; i < numPoints; i++)
{
point = points[i];
point.vx *= damp;
point.vy *= damp;
point.x += point.vx;
point.y += point.vy;
if (point.y > 465)
{
point.y = 465;
point.vy = 0;
}
if(point.x<20)
{
point.x=20;
point.vx=0;
}else if(point.x>465)
{
point.x=520;
point.vx=0;
}
}
}
public function spring(p0:Object, p1:Object):void
{
var dx:Number = p0.x - p1.x;
var dy:Number = p0.y - p1.y;
var angle:Number = p0.a;
var tx:Number = p1.x + dist * Math.cos(angle);
var ty:Number = p1.y + dist * Math.sin(angle);
var ax:Number = (tx - p0.x) * k;
var ay:Number = (ty - p0.y) * k;
p0.vx += ax * .5;
p0.vy += ay * .5;
p1.vx -= ax * .5;
p1.vy -= ay * .5;
}
public function mouseSpring(p:Object):void
{
var dx:Number=p.x-mouseX;
var dy:Number=p.y-mouseY;
var dist:Number=Math.sqrt(dx*dx+dy*dy);
if(dist<40)
{
var angle:Number=Math.atan2(dy, dx);
var tx:Number=mouseX+Math.cos(angle)*40;
var ty:Number=mouseY+Math.sin(angle)*40;
p.vx+=(tx-p.x)*k;
p.vy+=(ty-p.y)*k;
}
}
}
class BeamHead extends Sprite{
private var pts1:Array = new Array;
private var pts2:Array = new Array;
private const inside:int = 40;
private const outside:int = 100;
public function BeamHead(){
addEventListener(Event.ENTER_FRAME,update);
var i:int ;
var len:Number;
var r:Number;
for(i=0;i<inside;i++){
len = Math.random()*10+5;
r = Math.PI*2/inside;
pts1.push(new Point(Math.cos(r*i)*len,Math.sin(r*i)*len));
}
for(i=0;i<outside;i++){
len = Math.random()*20+10;
r = Math.PI*2/outside;
pts2.push(new Point(Math.cos(r*i)*len,Math.sin(r*i)*len));
}
filters = [new GlowFilter()];
}
public function update(e:Event):void{
graphics.clear();
var r:Number = Math.PI*2/inside;
var i:int;
var len:Number;
graphics.lineStyle(1,0xff0000);
graphics.moveTo(pts1[0].x,pts1[0].y);
for(i=1;i<inside;i++){
graphics.lineTo(pts1[i].x,pts1[i].y);
len = Math.random()*10+5;
pts1[i].x = Math.cos(r*i)*len;
pts1[i].y =Math.sin(r*i)*len;
}
graphics.lineTo(pts1[0].x,pts1[0].y);
pts1[0].x = Math.cos(r*i)*len;
pts1[0].y =Math.sin(r*i)*len;
r = Math.PI*2/outside;
graphics.lineStyle(2,0xffff00);
graphics.moveTo(pts2[0].x,pts2[0].y);
for(i=1;i<outside;i++){
graphics.lineTo(pts2[i].x,pts2[i].y);
len = Math.random()*40+10;
pts2[i].x = Math.cos(r*i)*len;
pts2[i].y =Math.sin(r*i)*len;
}
graphics.lineTo(pts2[0].x,pts2[0].y);
pts2[0].x = Math.cos(r*i)*len;
pts2[0].y =Math.sin(r*i)*len;
}
}