欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Flint Particle粒子入门教程(四):接口和类的实现结构

程序员文章站 2022-05-02 20:27:52
...
下面我们看看Flint Particle的接口和类的关系。
在Flint Particle中Action是一个接口。
Action:

package org.flintparticles.common.actions
{
import org.flintparticles.common.emitters.Emitter;
import org.flintparticles.common.particles.Particle;

public interface Action
{
function getDefaultPriority():Number;
function addedToEmitter( emitter:Emitter ):void;
function removedFromEmitter( emitter:Emitter ):void;
function update( emitter:Emitter, particle:Particle, time:Number ):void;
}
}

所有要实现Action接口的类都要两输入的元素:Emitter和Particle(发射器和粒子)
然后他们要实现4个基本的方法:
getDefaultPriority():Number;
addedToEmitter( emitter:Emitter ):void;
removedFromEmitter( emitter:Emitter ):void;
update( emitter:Emitter, particle:Particle, time:Number ):void;

《获得默认的优先级;
《加到发射器;
《从发射器移除;
《刷新(发射器、粒子和事件作为参数);

ActionBase implements Action
ActionBase类实现了Action的接口。

package org.flintparticles.common.actions
{
import org.flintparticles.common.emitters.Emitter;
import org.flintparticles.common.particles.Particle;
public class ActionBase implements Action
{
public function ActionBase()
{
}
public function getDefaultPriority():Number
{
return 0;
}
public function addedToEmitter( emitter:Emitter ):void
{
}
public function removedFromEmitter( emitter:Emitter ):void
{
}
public function update( emitter:Emitter, particle:Particle, time:Number ):void
{
}
}
}

ActionBase类实现了Action的接口,把getDefaultPriority()细化了,注意,ActionBase只是一个类,它没有继承任何其他的类。
关键的一点是它实现了接口,这样的类我们可以把很多共性的东西写到里面来,然后再让其他的类继承自它,可以减少代码的书写量,结构更明晰。

ScaleImage extends ActionBase
ScaleImage 继承自 ActionBase

package org.flintparticles.common.actions
{
import org.flintparticles.common.emitters.Emitter;
import org.flintparticles.common.particles.Particle;
public class ScaleImage extends ActionBase
{
private var _diffScale:Number;
private var _endScale:Number;
public function ScaleImage( startScale:Number = 1, endScale:Number = 1 )
{
_diffScale = startScale - endScale;
_endScale = endScale;
}
public function get startScale():Number
{
return _endScale + _diffScale;
}
public function set startScale( value:Number ):void
{
_diffScale = value - _endScale;
}
public function get endScale():Number
{
return _endScale;
}
public function set endScale( value:Number ):void
{
_diffScale = _endScale + _diffScale - value;
_endScale = value;
}
override public function update( emitter:Emitter, particle:Particle, time:Number ):void
{
particle.scale = _endScale + _diffScale * particle.energy;
}
}
}


下面我们生产一个Candle粒子,其他的知识大家可以参考博客的其他文章或者是Flint Particle的其他参数文章,这里我们就不重复了,主要是讨论一下addAction(new ScaleImage(1,1));的功能,这里我们的设置是1、1,也就是说粒子不缩放了。
我们留意ScaleImage的两个参数。

package
{
import org.flintparticles.common.actions.*;
import org.flintparticles.common.counters.*;
import org.flintparticles.common.displayObjects.Dot;
import org.flintparticles.common.initializers.*;
import org.flintparticles.threeD.actions.*;
import org.flintparticles.threeD.activities.MoveEmitter;
import org.flintparticles.threeD.emitters.Emitter3D;
import org.flintparticles.threeD.geom.Vector3D;
import org.flintparticles.threeD.initializers.*;
import org.flintparticles.threeD.zones.*;

public class Candle extends Emitter3D
{
public function Candle( position:Vector3D )
{
//counter = new Steady( 5 );
counter=new Blast(8);

addInitializer( new SharedImage( new Dot( 1 ) ) );
addInitializer( new ColorInit( 0xFFFFFF00, 0xFFFF6600 ) );
addInitializer( new Position( new PointZone( position ) ) );
addInitializer( new Velocity( new DiscZone( new Vector3D( 0, 120, 0 ), new Vector3D( 0, 1, 0 ), 30 ) ) );
addInitializer( new Lifetime( 5 ) );

addAction( new Move() );
addAction( new Accelerate( new Vector3D( 0, -50, 0 ) ) );
addAction( new Age() );
addAction(new ScaleImage(1,1));

}
}
}

进入到ScaleImage的构成
public function ScaleImage( startScale:Number = 1, endScale:Number = 1 )
第一个为startScal(开始缩放大小)e,第二个为endScale(结束缩放大小)。
相关标签: velocity