开发者

1180: Call to a possibly undefined method addEventListener

开发者 https://www.devze.com 2023-02-03 03:15 出处:网络
I\'m going through some AS3 training, but I\'m getting a weird error... I\'m trying to add an event listener to the end of a motion tween in AS.

I'm going through some AS3 training, but I'm getting a weird error...

I'm trying to add an event listener to the end of a motion tween in AS.

I've created a tween, highlighted the frames, right clicked and copied the tween as AS and pasted it into the movie clip (I think there's a better way to do this, but I'm not sure what it is...)

When I try to add the listener to the end of that code, I get the error. Here's my code.

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) {
    __motio开发者_Python百科n_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);
}

I've tried a few places for it, both with the animFactory_Enemy_3 variable and the motion_Enemy_3 variable - getting the same error both times.


I have never used this library, but as I see the Motion Object only stores the animation description. You need an Animator instance which will animate a DisplayObject on the Stage for you. The Animator class broadcasts the MotionEvents.

I would do it somewhat like this:

    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);
     }
    }

Some helpful stuff: http://www.kirupa.com/forum/showthread.php?t=258967 http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/fl/motion/Animator.html

I think you should take a look at the greensock Tweening engine, I am pretty sure it is much easier to use and more effective than this one.

I hope I could help, Rob

0

精彩评论

暂无评论...
验证码 换一张
取 消