1180:调用可能未定义的方法addEventListener

时间:2011-01-06 22:05:58

标签: actionscript-3 flash-cs5

我正在接受一些AS3培训,但我得到一个奇怪的错误......

我正在尝试将事件侦听器添加到AS中补间动画的末尾。

我创建了一个补间,突出显示了框架,右键单击并将补间复制为AS并将其粘贴到影片剪辑中(我认为有更好的方法可以做到这一点,但我不确定它是什么。 ..)

当我尝试将侦听器添加到该代码的末尾时,我收到错误。这是我的代码。

import fl.motion.AnimatorFactory;
import fl.motion.MotionBase;
import fl.motion.Motion;
import flash.filters.*;
import flash.geom.Point;
import fl.motion.MotionEvent;
import fl.events.*;

var __motion_Enemy_3:MotionBase;
if(__motion_Enemy_3 == null) {
    __motion_Enemy_3 = new Motion();
    __motion_Enemy_3.duration = 30;

    // Call overrideTargetTransform to prevent the scale, skew,
    // or rotation values from being made relative to the target
    // object's original transform.
    // __motion_Enemy_3.overrideTargetTransform();

    // The following calls to addPropertyArray assign data values
    // for each tweened property. There is one value in the Array
    // for every frame in the tween, or fewer if the last value
    // remains the same for the rest of the frames.
    __motion_Enemy_3.addPropertyArray("x", [0]);
    __motion_Enemy_3.addPropertyArray("y", [0]);
    __motion_Enemy_3.addPropertyArray("scaleX", [1.000000,1.048712,1.097424,1.146136,1.194847,1.243559,1.292271,1.340983,1.389695,1.438407,1.487118,1.535830,1.584542,1.633254,1.681966,1.730678,1.779389,1.828101,1.876813,1.925525,1.974237,2.022949,2.071661,2.120372,2.169084,2.217796,2.266508,2.315220,2.363932,2.412643]);
    __motion_Enemy_3.addPropertyArray("scaleY", [1.000000,1.048712,1.097424,1.146136,1.194847,1.243559,1.292271,1.340983,1.389695,1.438407,1.487118,1.535830,1.584542,1.633254,1.681966,1.730678,1.779389,1.828101,1.876813,1.925525,1.974237,2.022949,2.071661,2.120372,2.169084,2.217796,2.266508,2.315220,2.363932,2.412643]);
    __motion_Enemy_3.addPropertyArray("skewX", [0]);
    __motion_Enemy_3.addPropertyArray("skewY", [0]);
    __motion_Enemy_3.addPropertyArray("rotationConcat", [0]);
    __motion_Enemy_3.addPropertyArray("blendMode", ["normal"]);
    __motion_Enemy_3.addPropertyArray("cacheAsBitmap", [false]);
 __motion_Enemy_3.addEventListener(MotionEvent.MOTION_END, hurtPlayer);

    // Create an AnimatorFactory instance, which will manage
    // targets for its corresponding Motion.
    var __animFactory_Enemy_3:AnimatorFactory = new AnimatorFactory(__motion_Enemy_3);
    __animFactory_Enemy_3.transformationPoint = new Point(0.499558, 0.500000);

    // Call the addTarget function on the AnimatorFactory
    // instance to target a DisplayObject with this Motion.
    // The second parameter is the number of times the animation
    // will play - the default value of 0 means it will loop.
    // __animFactory_Enemy_3.addTarget(<instance name goes here>, 0);
}

function hurtPlayer(event:MotionEvent):void {
 this.parent.removeChild(this);
}

我已经尝试了几个地方,包括animFactory_Enemy_3变量和motion_Enemy_3变量 - 两次都得到相同的错误。

1 个答案:

答案 0 :(得分:0)

我从未使用过这个库,但正如我所见,Motion Object只存储动画描述。您需要一个Animator实例,它将为舞台上的DisplayObject设置动画。 Animator类广播MotionEvents。

我会这样做:

    import fl.motion.AnimatorFactory;
    import fl.motion.MotionBase;
    import fl.motion.Motion;
    import flash.display.DisplayObjectContainer;
    import flash.display.Sprite;
    import flash.motion.Animator;
    import flash.filters.*;
    import flash.geom.Point;
    import fl.motion.MotionEvent;
    import fl.events.*;

    var __motion_Enemy_3:MotionBase;
    var animator:Animator;

    var spriteToMove:DisplayObjectContainer;

    if(!spriteToMove)
    {
        spriteToMove = new Sprite();
        spriteToMove.graphics.beginFill(0xff0000, 1);
        spriteToMove.graphics.drawRect(0, 0, 50, 50);
        spriteToMove.graphics.endFill();
        addChild(spriteToMove)
    }

    if(__motion_Enemy_3 == null) {
        __motion_Enemy_3 = new Motion();
        __motion_Enemy_3.duration = 30;

        // Call overrideTargetTransform to prevent the scale, skew,
        // or rotation values from being made relative to the target
        // object's original transform.
        // __motion_Enemy_3.overrideTargetTransform();

        // The following calls to addPropertyArray assign data values
        // for each tweened property. There is one value in the Array
        // for every frame in the tween, or fewer if the last value
        // remains the same for the rest of the frames.
        __motion_Enemy_3.addPropertyArray("x", [0]);
        __motion_Enemy_3.addPropertyArray("y", [0]);
        __motion_Enemy_3.addPropertyArray("scaleX", [1.000000,1.048712,1.097424,1.146136,1.194847,1.243559,1.292271,1.340983,1.389695,1.438407,1.487118,1.535830,1.584542,1.633254,1.681966,1.730678,1.779389,1.828101,1.876813,1.925525,1.974237,2.022949,2.071661,2.120372,2.169084,2.217796,2.266508,2.315220,2.363932,2.412643]);
        __motion_Enemy_3.addPropertyArray("scaleY", [1.000000,1.048712,1.097424,1.146136,1.194847,1.243559,1.292271,1.340983,1.389695,1.438407,1.487118,1.535830,1.584542,1.633254,1.681966,1.730678,1.779389,1.828101,1.876813,1.925525,1.974237,2.022949,2.071661,2.120372,2.169084,2.217796,2.266508,2.315220,2.363932,2.412643]);
        __motion_Enemy_3.addPropertyArray("skewX", [0]);
        __motion_Enemy_3.addPropertyArray("skewY", [0]);
        __motion_Enemy_3.addPropertyArray("rotationConcat", [0]);
        __motion_Enemy_3.addPropertyArray("blendMode", ["normal"]);
        __motion_Enemy_3.addPropertyArray("cacheAsBitmap", [false]);

        // Create an AnimatorFactory instance, which will manage
        // targets for its corresponding Motion.
        var __animFactory_Enemy_3:AnimatorFactory = new AnimatorFactory(__motion_Enemy_3);
        __animFactory_Enemy_3.transformationPoint = new Point(0.499558, 0.500000);

        // Call the addTarget function on the AnimatorFactory
        // instance to target a DisplayObject with this Motion.
        // The second parameter is the number of times the animation
        // will play - the default value of 0 means it will loop.
        // __animFactory_Enemy_3.addTarget(<instance name goes here>, 0);
    }

    /*The Animator instance*/
    animator = new Animator(__motion_Enemy_3, spriteToMove);
    animator.addEventListener(MotionEvent.MOTION_END, onMotionEnd);

    function onMotionEnd(event:MotionEvent):void {
     if (spriteToMove)
     {
        removeChild(spriteToMove);
     }
    }

一些有用的东西: http://www.kirupa.com/forum/showthread.php?t=258967 http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/fl/motion/Animator.html

我认为你应该看看greensock Tweening engine,我很确定它比这个更容易使用和更有效。

我希望我能提供帮助, 罗布