Flash AS3代码打造绚烂的星形变幻效果
程序员文章站
2023-11-06 14:24:10
本教程是向脚本之家的朋友介绍利用Flash AS3代码打造绚烂的星形变幻效果方法,教程制作出来的星形还是很漂亮的,推荐过来,一起来学习吧... 14-11-05...
这篇教程主要向大家讲解如何利用flash as3代码打造绚烂的星形变幻效果,教程并没有对每一个代码做出讲解,但也不是很难,分享到,喜欢的朋友一起来学习吧!
复制代码
代码如下:package
{
import flash.display.sprite;
import flash.events.event;
import flash.events.mouseevent;
[swf(backgroundcolor = "0x000000", framerate = "60")]
public class main extends sprite
{
private var patternlist:array = new array();
private var movebullet:array = new array();
public function main():void
{
if (stage) init();
else addeventlistener(event.added_to_stage, init);
}
private function init(e:event = null):void
{
removeeventlistener(event.added_to_stage, init);
// entry point
mousedown(null);
addeventlistener(event.enter_frame, enterframe );
stage.addeventlistener(mouseevent.mouse_down, mousedown);
}
private function enterframe(event:event):void
{
var i:int = 0;
for ( i = 0; i < patternlist.length; i++ )
{
var bullet:bullet = patternlist[i].run();
if ( bullet != null )
{
addchild( bullet );
}
}
for ( i = patternlist.length - 1; i >= 0; i-- )
{
if ( patternlist[i].isend() )
{
// 从待命到移动
for ( var j:int = 0; j < patternlist[i].waitbullet.length; j++ )
{
patternlist[i].waitbullet[j].startslide();
movebullet.push( patternlist[i].waitbullet[j] );
}
patternlist.splice( i, 1 );
}
}
// 移动
if ( movebullet.length > 0 )
{
for ( i = movebullet.length - 1; i >= 0; i-- )
{
//
if ( movebullet[i].slideflag )
{
movebullet[i]._xx += math.cos( movebullet[i].slideangle * math.pi / 180 ) * movebullet[i].slidespeed;
movebullet[i]._xy += math.sin( movebullet[i].slideangle * math.pi / 180 ) * movebullet[i].slidespeed;
movebullet[i].x = movebullet[i]._xx;
movebullet[i].y = movebullet[i]._xy;
if ( movebullet[i].slidespeed > 0 )
{
movebullet[i].slidespeed -= movebullet[i].slidespeedmax / 50;
if ( movebullet[i].slidespeed < 0 ) movebullet[i].slideflag = false;
}
}else
{
movebullet[i]._xx += math.cos( movebullet[i].moveangle * math.pi / 180 ) * 1;
movebullet[i]._xy += math.sin( movebullet[i].moveangle * math.pi / 180 ) * 1;
movebullet[i].x = movebullet[i]._xx;
movebullet[i].y = movebullet[i]._xy;
if ( movebullet[i].x < -50 || movebullet[i].x > stage.stagewidth + 50 || movebullet[i].y < -50 || movebullet[i].y > stage.stageheight + 50 )
{
removechild( movebullet[i] );
movebullet.splice( i, 1 );
}
}
}
}
}
private function mousedown(event:mouseevent):void
{
// 小星
patternlist.push( new pattern(stage.stagewidth / 2, stage.stageheight / 2, false, 50, 0xff5555, (270 + 72 * 0), 3 ) );
patternlist.push( new pattern(stage.stagewidth / 2, stage.stageheight / 2, false, 50, 0xff5555, (270 + 72 * 1), 3 ) );
patternlist.push( new pattern(stage.stagewidth / 2, stage.stageheight / 2, false, 50, 0xff5555, (270 + 72 * 2), 3 ) );
patternlist.push( new pattern(stage.stagewidth / 2, stage.stageheight / 2, false, 50, 0xff5555, (270 + 72 * 3), 3 ) );
patternlist.push( new pattern(stage.stagewidth / 2, stage.stageheight / 2, false, 50, 0xff5555, (270 + 72 * 4), 3 ) );
// 大星
patternlist.push( new pattern(stage.stagewidth/2, stage.stageheight/2, false, 100, 0x5555ff, (270 + 72 * 0), 5 ) );
patternlist.push( new pattern(stage.stagewidth/2, stage.stageheight/2, false, 100, 0x5555ff, (270 + 72 * 1), 5 ) );
patternlist.push( new pattern(stage.stagewidth/2, stage.stageheight/2, false, 100, 0x5555ff, (270 + 72 * 2), 5 ) );
patternlist.push( new pattern(stage.stagewidth/2, stage.stageheight/2, false, 100, 0x5555ff, (270 + 72 * 3), 5 ) );
patternlist.push( new pattern(stage.stagewidth/2, stage.stageheight/2, false, 100, 0x5555ff, (270 + 72 * 4), 5 ) );
}
}
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
import flash.display.sprite;
import flash.geom.point;
//-----------------------------
// 图案类
class pattern {
private var reverse:boolean = false;
private var starsize:number = 150;
private var bulletcolor:uint = 0x000000;
private var slideangle:number = 0;
private var slidespeed:number = 0;
private var end:boolean = false;
private var count:int = 0;
private var defx:int = 0;
private var defy:int = 0;
private var target:array = new array();
private static const path_obj_num:int = 20; // 子弹数
private static const target_num:int = 5;
private static const target_angle:array = [ 270 + 72 * 3, 270 + 72 * 1, 270 + 72 * 4, 270 + 72 * 2, 270 ]; // 通过的目标地点的角度
private static const target_angle2:array = [ 270 + 72 * 2, 270 + 72 * 4, 270 + 72 * 1, 270 + 72 * 3, 270 ]; //
public var waitbullet:array = new array();
public function pattern(
_gx:int = 0, _gy:int = 0,
_rev:boolean = false,
_starsize:number = 80,
_bulletcolor:uint = 0x000000,
_slideangle:number = 0,
_slidespeed:number = 0
) {
count = 0;
defx = _gx;
defy = _gy;
reverse = _rev;
starsize = _starsize;
bulletcolor = _bulletcolor;
slideangle = _slideangle;
slidespeed = _slidespeed;
for ( var i:int = 0; i < 5; i++ )
{
var angle:number = target_angle[i];
if ( reverse ) angle = target_angle2[i] + 180; // 反転
else angle = target_angle[i];
target[i] = new point( math.cos( angle * math.pi / 180 ) * starsize, math.sin( angle * math.pi / 180 ) * starsize );
}
}
//子弹生成
public function run() : bullet {
var bullet:bullet = new bullet(bulletcolor,slideangle,slidespeed);
var targetno:int = int(count / path_obj_num);
var targetrate:int = int(count % path_obj_num);
var p:point;
//子弹的座标
p = point.interpolate( target[targetno], target[(targetno+(target_num-1))%target_num], targetrate / path_obj_num );
bullet.x = p.x + defx;
bullet.y = p.y + defy;
bullet._xx = p.x + defx;
bullet._xy = p.y + defy;
//子弹的移动角度
//if( !reverse ) bullet.moveangle = -count / path_obj_num / target_num * 720 + 135;
//else bullet.moveangle = count / path_obj_num / target_num * 720 - 135;
bullet.moveangle = ( -int(count / path_obj_num) / target_num * 720 + 108) + ((count % path_obj_num) * 180 / path_obj_num); // target始点角度 + 180 * rate グレイソーに入る前の奴
waitbullet.push( bullet );
count++;
if ( count == target_num * path_obj_num ) end = true;
return bullet;
}
public function isend() : boolean { return end; }
}
//-----------------------------
// 子弹类
class bullet extends sprite {
public var slideflag:boolean = false;
public var slideangle:number = 0;
public var slidespeed:number = 0;
public var slidespeedmax:number = 0;
public var moveangle:number = 0;
public var _xx:number = 0;
public var _xy:number = 0;
public function bullet(color:uint,_slideangle:number = 0,_slidespeed:number = 50) {
super();
graphics.beginfill(color);
graphics.drawcircle(0, 0, 5);
graphics.endfill();
graphics.beginfill(0xffffff, 0.5);
graphics.drawcircle(0, 0, 3);
graphics.endfill();
slideangle = _slideangle;
slidespeed = _slidespeed;
slidespeedmax = _slidespeed;
slideflag = false;
}
public function startslide():void
{
slideflag = true;
}
}
{
import flash.display.sprite;
import flash.events.event;
import flash.events.mouseevent;
[swf(backgroundcolor = "0x000000", framerate = "60")]
public class main extends sprite
{
private var patternlist:array = new array();
private var movebullet:array = new array();
public function main():void
{
if (stage) init();
else addeventlistener(event.added_to_stage, init);
}
private function init(e:event = null):void
{
removeeventlistener(event.added_to_stage, init);
// entry point
mousedown(null);
addeventlistener(event.enter_frame, enterframe );
stage.addeventlistener(mouseevent.mouse_down, mousedown);
}
private function enterframe(event:event):void
{
var i:int = 0;
for ( i = 0; i < patternlist.length; i++ )
{
var bullet:bullet = patternlist[i].run();
if ( bullet != null )
{
addchild( bullet );
}
}
for ( i = patternlist.length - 1; i >= 0; i-- )
{
if ( patternlist[i].isend() )
{
// 从待命到移动
for ( var j:int = 0; j < patternlist[i].waitbullet.length; j++ )
{
patternlist[i].waitbullet[j].startslide();
movebullet.push( patternlist[i].waitbullet[j] );
}
patternlist.splice( i, 1 );
}
}
// 移动
if ( movebullet.length > 0 )
{
for ( i = movebullet.length - 1; i >= 0; i-- )
{
//
if ( movebullet[i].slideflag )
{
movebullet[i]._xx += math.cos( movebullet[i].slideangle * math.pi / 180 ) * movebullet[i].slidespeed;
movebullet[i]._xy += math.sin( movebullet[i].slideangle * math.pi / 180 ) * movebullet[i].slidespeed;
movebullet[i].x = movebullet[i]._xx;
movebullet[i].y = movebullet[i]._xy;
if ( movebullet[i].slidespeed > 0 )
{
movebullet[i].slidespeed -= movebullet[i].slidespeedmax / 50;
if ( movebullet[i].slidespeed < 0 ) movebullet[i].slideflag = false;
}
}else
{
movebullet[i]._xx += math.cos( movebullet[i].moveangle * math.pi / 180 ) * 1;
movebullet[i]._xy += math.sin( movebullet[i].moveangle * math.pi / 180 ) * 1;
movebullet[i].x = movebullet[i]._xx;
movebullet[i].y = movebullet[i]._xy;
if ( movebullet[i].x < -50 || movebullet[i].x > stage.stagewidth + 50 || movebullet[i].y < -50 || movebullet[i].y > stage.stageheight + 50 )
{
removechild( movebullet[i] );
movebullet.splice( i, 1 );
}
}
}
}
}
private function mousedown(event:mouseevent):void
{
// 小星
patternlist.push( new pattern(stage.stagewidth / 2, stage.stageheight / 2, false, 50, 0xff5555, (270 + 72 * 0), 3 ) );
patternlist.push( new pattern(stage.stagewidth / 2, stage.stageheight / 2, false, 50, 0xff5555, (270 + 72 * 1), 3 ) );
patternlist.push( new pattern(stage.stagewidth / 2, stage.stageheight / 2, false, 50, 0xff5555, (270 + 72 * 2), 3 ) );
patternlist.push( new pattern(stage.stagewidth / 2, stage.stageheight / 2, false, 50, 0xff5555, (270 + 72 * 3), 3 ) );
patternlist.push( new pattern(stage.stagewidth / 2, stage.stageheight / 2, false, 50, 0xff5555, (270 + 72 * 4), 3 ) );
// 大星
patternlist.push( new pattern(stage.stagewidth/2, stage.stageheight/2, false, 100, 0x5555ff, (270 + 72 * 0), 5 ) );
patternlist.push( new pattern(stage.stagewidth/2, stage.stageheight/2, false, 100, 0x5555ff, (270 + 72 * 1), 5 ) );
patternlist.push( new pattern(stage.stagewidth/2, stage.stageheight/2, false, 100, 0x5555ff, (270 + 72 * 2), 5 ) );
patternlist.push( new pattern(stage.stagewidth/2, stage.stageheight/2, false, 100, 0x5555ff, (270 + 72 * 3), 5 ) );
patternlist.push( new pattern(stage.stagewidth/2, stage.stageheight/2, false, 100, 0x5555ff, (270 + 72 * 4), 5 ) );
}
}
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
import flash.display.sprite;
import flash.geom.point;
//-----------------------------
// 图案类
class pattern {
private var reverse:boolean = false;
private var starsize:number = 150;
private var bulletcolor:uint = 0x000000;
private var slideangle:number = 0;
private var slidespeed:number = 0;
private var end:boolean = false;
private var count:int = 0;
private var defx:int = 0;
private var defy:int = 0;
private var target:array = new array();
private static const path_obj_num:int = 20; // 子弹数
private static const target_num:int = 5;
private static const target_angle:array = [ 270 + 72 * 3, 270 + 72 * 1, 270 + 72 * 4, 270 + 72 * 2, 270 ]; // 通过的目标地点的角度
private static const target_angle2:array = [ 270 + 72 * 2, 270 + 72 * 4, 270 + 72 * 1, 270 + 72 * 3, 270 ]; //
public var waitbullet:array = new array();
public function pattern(
_gx:int = 0, _gy:int = 0,
_rev:boolean = false,
_starsize:number = 80,
_bulletcolor:uint = 0x000000,
_slideangle:number = 0,
_slidespeed:number = 0
) {
count = 0;
defx = _gx;
defy = _gy;
reverse = _rev;
starsize = _starsize;
bulletcolor = _bulletcolor;
slideangle = _slideangle;
slidespeed = _slidespeed;
for ( var i:int = 0; i < 5; i++ )
{
var angle:number = target_angle[i];
if ( reverse ) angle = target_angle2[i] + 180; // 反転
else angle = target_angle[i];
target[i] = new point( math.cos( angle * math.pi / 180 ) * starsize, math.sin( angle * math.pi / 180 ) * starsize );
}
}
//子弹生成
public function run() : bullet {
var bullet:bullet = new bullet(bulletcolor,slideangle,slidespeed);
var targetno:int = int(count / path_obj_num);
var targetrate:int = int(count % path_obj_num);
var p:point;
//子弹的座标
p = point.interpolate( target[targetno], target[(targetno+(target_num-1))%target_num], targetrate / path_obj_num );
bullet.x = p.x + defx;
bullet.y = p.y + defy;
bullet._xx = p.x + defx;
bullet._xy = p.y + defy;
//子弹的移动角度
//if( !reverse ) bullet.moveangle = -count / path_obj_num / target_num * 720 + 135;
//else bullet.moveangle = count / path_obj_num / target_num * 720 - 135;
bullet.moveangle = ( -int(count / path_obj_num) / target_num * 720 + 108) + ((count % path_obj_num) * 180 / path_obj_num); // target始点角度 + 180 * rate グレイソーに入る前の奴
waitbullet.push( bullet );
count++;
if ( count == target_num * path_obj_num ) end = true;
return bullet;
}
public function isend() : boolean { return end; }
}
//-----------------------------
// 子弹类
class bullet extends sprite {
public var slideflag:boolean = false;
public var slideangle:number = 0;
public var slidespeed:number = 0;
public var slidespeedmax:number = 0;
public var moveangle:number = 0;
public var _xx:number = 0;
public var _xy:number = 0;
public function bullet(color:uint,_slideangle:number = 0,_slidespeed:number = 50) {
super();
graphics.beginfill(color);
graphics.drawcircle(0, 0, 5);
graphics.endfill();
graphics.beginfill(0xffffff, 0.5);
graphics.drawcircle(0, 0, 3);
graphics.endfill();
slideangle = _slideangle;
slidespeed = _slidespeed;
slidespeedmax = _slidespeed;
slideflag = false;
}
public function startslide():void
{
slideflag = true;
}
}
以上就是flash as3代码打造绚烂的星形变幻效果的方法介绍,希望能帮到大家,谢谢阅读!
上一篇: 二货青年,笑到肚子疼