声明:勿做商业用途! 昨晚看到闪吧翻译flashcompoment.net的创建组件的方法。 觉得还少了一点儿什么,想想,原来是这样。
1.大家做好了组件之后,只能在自己机子上用,可这样做,只有自已可以用到, 不能方便别人。 最好能打包成mxp的文件, 然后提供下载, 大家一起用嘛。
2.那个例子没有监听对象, 只能是三角形, 不太灵活。
首先说一下,这个组件是一个方向控制组件, 响应键盘的上,下,左,右方向键。可以使物体朝上,下, 左,右,左上, 右上, 左下,右下不同的八个方向运动. 看看这个吧。 这是注册了监听给甲虫对象的。 用ctrl+f8新建一个MC,并在此MC第一帧加上这些AS:
#initclip // 构造MovementClass MovementClass = function () { }; // 注册MovementClass Object.registerClass("Movement", MovementClass); // 加一个监听方法 MovementClass.prototype.addListener = function(ref) { //把所要加的对象传给listener this.listener = ref; if (this.listener<>undefined) { // 检查是不是MC if (this.listener instanceof MovieClip) { // 嗯,可以了,使它的方法生效 this.enabled = true; // 图标或者商标什么的全隐掉 this._visible = false; } else { // 告诉用户你的目标不是有效的,这个目标方法无效 trace("你引入的"+this.listener+"不是一个有效的物件"); this.enabled = false; } } else { //告诉用户没有定义目标 trace("你的目标呢?"); this.enabled = false; } }; // 去掉监听 MovementClass.prototype.removeListener = function() { delete this.listener; }; // 发送动作 MovementClass.prototype.sendMovementEvent = function() { this.listener.onMovement(this.listener._rotation, this.listener._x, this.listener._y); }; MovementClass.prototype.onEnterFrame = function() { if (this.enabled) { //正方向运动 if (Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT)) { this.listener._x -= this.speed; this.listener._rotation = 270; } if (Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT)) { this.listener._x += this.speed; this.listener._rotation = 90; } if (Key.isDown(Key.UP) && !Key.isDown(Key.DOWN)) { this.listener._y -= this.speed; this.listener._rotation = 0; } if (Key.isDown(Key.DOWN) && !Key.isDown(Key.UP)) { this.listener._y += this.speed; this.listener._rotation = 180; } // // 斜方向的运动 if (Key.isDown(Key.LEFT) && Key.isDown(Key.UP) && !Key.isDown(Key.RIGHT) && !Key.isDown(Key.DOWN)) { this.listener._rotation = 315; } if (Key.isDown(Key.RIGHT) && Key.isDown(Key.UP) && !Key.isDown(Key.LEFT) && !Key.isDown(Key.DOWN)) { this.listener._rotation = 45; } if (Key.isDown(Key.LEFT) && Key.isDown(Key.DOWN) && !Key.isDown(Key.RIGHT) && !Key.isDown(Key.UP)) { this.listener._rotation = 225; } if (Key.isDown(Key.RIGHT) && Key.isDown(Key.DOWN) && !Key.isDown(Key.LEFT) && !Key.isDown(Key.UP)) { this.listener._rotation = 135; } this.sendMovementEvent(); } }; #endinitclip
然后用ctrl+L调出库面板,用右键点击这个MC,选择linkage. 然后勾选如下。

|