Actionscript 2 粒子
Posted
技术标签:
【中文标题】Actionscript 2 粒子【英文标题】:Actionscript 2 particles 【发布时间】:2018-05-11 22:07:45 【问题描述】:我知道这是旧技术,但我只想更新我发送的闪存圣诞卡。基本上我使用粒子脚本(见下文)跟随我的鼠标并喷出一些金粒子。我想对此提出一些反对意见,以便它跟随在屏幕上的补间对象后面。我在想这可能是将事件侦听器更改为某些东西但无法弄清楚的情况。
编辑 - 我的对象是一个名为 sleigh 的影片剪辑
任何帮助表示赞赏。
脚本 (AS2)
//import bitmap class
import flash.display.BitmapData;
//Settings
var particleMaxSpeed:Number = 3;
var particleFadeSpeed:Number = 5;
var particleTotal:Number = 5;
var particleRange:Number = 25;
/**
* createExplosion(target X position, target Y position)
*/
function createExplosion(targetX:Number, targetY:Number):Void
//run for loop based on particleTotal
for (var i:Number = 0; i<particleTotal; i++)
//attach bitmap from the library with the linked name "adobe_flash"
var myBmp:BitmapData = BitmapData.loadBitmap("fire.jpg");
//create the "main_holder" movieclip that will hold our bitmap
var particle_mc = _root.createEmptyMovieClip("main_holder", _root.getNextHighestDepth());
//create an "internal_holder" movieclip inside "main_holder" that we'll use to center the bitmap data
var internal_holder:MovieClip = particle_mc.createEmptyMovieClip("internal_holder", particle_mc.getNextHighestDepth());
//set "internal_holder" x and y position based on bitmap size
internal_holder._x = -myBmp.width/2;
internal_holder._y = -myBmp.height/2;
//finally, attach the bitmapData "myBmp" to the movieclip "internal_holder"
internal_holder.attachBitmap(myBmp, internal_holder.getNextHighestDepth(), "never", true);
//set position & rotation, alpha
particle_mc._x = targetX
particle_mc._y = targetY
particle_mc._rotation = random(360);
particle_mc._alpha = random(50)+50;
//set particle boundry
particle_mc.boundyLeft = targetX - particleRange;
particle_mc.boundyTop = targetY - particleRange;
particle_mc.boundyRight = targetX + particleRange;
particle_mc.boundyBottom = targetY + particleRange;
//set speed/direction of fragment
particle_mc.speedX = Math.random(particleMaxSpeed)-Math.random(particleMaxSpeed);
particle_mc.speedY = Math.random(particleMaxSpeed)-Math.random(particleMaxSpeed);
particle_mc.speedX *= particleMaxSpeed
particle_mc.speedY *= particleMaxSpeed
//set fade out speed
particle_mc.fadeSpeed = Math.random(particleFadeSpeed)*particleFadeSpeed;
//just a visual particle counter
numberOfParticles++;
//make fragment move using onEnterFrame
particle_mc.onEnterFrame = function():Void
//update alpha, x, y
this._alpha -= this.fadeSpeed;
this._x += this.speedX;
this._y += this.speedY;
//if fragment is invisible or out of bounds, remove it
if (this._alpha <= 0 || this._x < this.boundyLeft || this._x > this.boundyRight || this._y < this.boundyTop || this._y > this.boundyBottom)
this.removeMovieClip();
//
numberOfParticles--;
/**
* Mouse Controls
*/
//create an object that we'll listen to
mouseListener = new Object();
//on Click, createExplosion
mouseListener.onMouseDown = function()
//createExplosion(_xmouse, _ymouse);
mouseListener.onMouseMove = function()
createExplosion(_xmouse, _ymouse);
//add listener
Mouse.addListener(mouseListener);
【问题讨论】:
【参考方案1】:您需要每帧调用createExplosion
函数并将sleigh
x 和y 坐标而不是鼠标坐标传递给它。替换
mouseListener.onMouseMove = function()
createExplosion(_xmouse, _ymouse);
与
sleigh.onEnterFrame = function()
createExplosion(sleigh._x, sleigh._y);
并删除其他鼠标监听器
【讨论】:
以上是关于Actionscript 2 粒子的主要内容,如果未能解决你的问题,请参考以下文章
ActionScript 3.0 + 计算两个日期之间的时间跨度?
ActionScript 3:字符命中测试对象,所有对象都在数组中
粒子群算法MATLAB代码,怎么运行不行,高手们给我修改一下,本人是菜鸟!谢谢!