A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: #2007: Parameter listener must be non-null. YOU WHAT?

  1. #1
    Senior Member b18269's Avatar
    Join Date
    Jan 2006
    Location
    London, UK
    Posts
    167

    Red face #2007: Parameter listener must be non-null. YOU WHAT?



    TypeError: Error #2007: Parameter listener must be non-null.
    at flash.events::EventDispatcher/addEventListener()
    at as3_testsite_fla::MainTimeline/setSelectedBtn()
    at as3_testsite_fla::MainTimeline/doClick()

    Hey guys, wondering if someone has a very simple answer to this. I bet its that i have just been stupid and forgotten to add something but can think. i had a search around but cannot find exactly what i am looking for.

    Below is a copy of the button array script. I have commented where the error is occuring (MOTION_FINISH)

    Actionscript Code:
    var buttonsArray:Array = [themenu.aboutus_btn,themenu.portfolio_btn,themenu.contact_btn,homebtn];
    function setButtons():void
    {
        for (var i:int=0; i<buttonsArray.length; i++)
        {
            buttonsArray[i].id = i;
            buttonsArray[i].buttonMode = true;
            buttonsArray[i].mouseChildren = false;
            buttonsArray[i].mouseEnabled = true;
            buttonsArray[i].addEventListener(MouseEvent.ROLL_OVER,playOver);
            buttonsArray[i].addEventListener(MouseEvent.ROLL_OUT,playOut);
            buttonsArray[i].addEventListener(MouseEvent.CLICK,doClick);
        }
    }
    function playOver(event:MouseEvent):void
    {
        event.currentTarget.gotoAndPlay("over");
    }
    function playOut(event:MouseEvent):void
    {
        event.currentTarget.gotoAndPlay("out");
    }

    function doClick(event:MouseEvent):void
    {
       
        var currentBtn:int = event.currentTarget.id;
       
        setSelectedBtn(currentBtn);
    }

    function setSelectedBtn(id:int):void
    {
        for (var i:int=0; i< buttonsArray.length; i++)
        {
            if (id == i)
            {
                buttonsArray[i].gotoAndStop("down");
                buttonsArray[i].buttonMode = false;
                buttonsArray[i].mouseEnabled = false;
                buttonsArray[i].removeEventListener(MouseEvent.ROLL_OVER,playOver);
                buttonsArray[i].removeEventListener(MouseEvent.ROLL_OUT,playOut);
                buttonsArray[i].removeEventListener(MouseEvent.CLICK,doClick);

                switch (id)
                {
                    case 0 :
                        trace ("about")

                        break;
                    case 1 :

    //////////THIS LITTLE RASCAL IS CAUSING ME PROBLEMS/////////////// WHAT SHOULD I ADD?
                        var portTween:Tween = new Tween(homebtn,"x",Strong.easeOut,homebtn.x,-750,1,true);

    portTween.addEventListener(TweenEvent.MOTION_FINISH, WhenTweenEnds);
    function WhenTweenEnds(e:TweenEvent):void
    {
       
    trace("portfolio");
    new Tween(topmenu,"y",Strong.easeOut,topmenu.y,0,1,true);
    }
    //////////////////////////////////////////////////////////////////////////////

                        break;
                    case 2 :
                        trace ("contact")

                        break;

                }
            }
            else
            {
                if (buttonsArray[i].currentLabel == "down")
                {
                    buttonsArray[i].gotoAndPlay("out");
                }
                buttonsArray[i].buttonMode = true;
                buttonsArray[i].mouseEnabled = true;
                buttonsArray[i].addEventListener(MouseEvent.ROLL_OVER,playOver);
                buttonsArray[i].addEventListener(MouseEvent.ROLL_OUT,playOut);
                buttonsArray[i].addEventListener(MouseEvent.CLICK,doClick);
            }
        }
    }
    setButtons();

    Thanks for taking the time time to look. If I figure it out I will post the answer up here for others to learn from my mistakes. flashkit rocks

  2. #2
    Senior Member
    Join Date
    May 2010
    Location
    Russia: Western Siberia
    Posts
    268
    You cannot just create a new tween like so:
    Actionscript Code:
    new Tween(topmenu,"y",Strong.easeOut,topmenu.y,0,1,true);
    You have to assign it to a variable:
    Actionscript Code:
    var topMenuTween:Tween = new Tween(topmenu,"y",Strong.easeOut,topmenu.y,0,1,true);

    And before you ask another question about why does your tween gets stuck from time to time, I'll answer this:
    Declare you're tween outside the function:
    Actionscript Code:
    var topMenuTween:Tween;
    function WhenTweenEnds(e:TweenEvent):void
    {
       
    trace("portfolio");
    topMenuTween = new Tween(topmenu,"y",Strong.easeOut,topmenu.y,0,1,true);
    }

    And by the way, I do reccomend you to use caurina tweener, ot TweenLite instead these crappy built-in tweens

  3. #3
    Senior Member b18269's Avatar
    Join Date
    Jan 2006
    Location
    London, UK
    Posts
    167
    Cheers Ryan. I will have a play tonight and let you know how I go on. I use tweenMax/tweenLite occasionally but get scared sometimes and when im in a rush I dont have the time to search online for answers when I'm in a pickle.


    With tweening like so:
    Actionscript Code:
    new Tween(topmenu,"y",Strong.easeOut,topmenu.y,0,1,true);
    I am lazy. The tween worked without
    Actionscript Code:
    var topMenuTween:Tween =
    in front of it, so did not understand why it should be used.

    I did'nt notice the tween getting stuck anywhere :s Im gonn go click crazy and replicate it. But I will use your hint of declareing the tween outside the function.

    Thanks buddy

  4. #4
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    You should assign your tween to a variable so you can manipulate it later if you have to. If you just want a tween that will never ever need to be accessed again or cancelled, then you can do it without a variable.

  5. #5
    Senior Member b18269's Avatar
    Join Date
    Jan 2006
    Location
    London, UK
    Posts
    167
    Quote Originally Posted by 5Five555 View Post
    You should assign your tween to a variable so you can manipulate it later if you have to. If you just want a tween that will never ever need to be accessed again or cancelled, then you can do it without a variable.

    Thats what I thought. I'm glad you confined this for me, otherwise I would have been doing something seriously wrong for the last 5 years ha.

    I still havent had a chance to work on this problem yet, so busy with client work at the mo

  6. #6
    Senior Member
    Join Date
    May 2010
    Location
    Russia: Western Siberia
    Posts
    268
    As to the problem, it seems like "topmenu" is null.
    Where did you define it? Where are you trying to access it from?

    This problem may occur if you try to access some property that was defined on, e.g. frame 2, from frame one etc.

  7. #7
    Senior Member b18269's Avatar
    Join Date
    Jan 2006
    Location
    London, UK
    Posts
    167
    Its strange. One this. Must be obvious to someone who's a code genius.

    @caseyryan the objects are all in 1 keyframe in the timeline

    I have attached an cs4 fla file. In this the buttons trace into the output window for testing.

    Maybe someone could have a quick looksy for me? I've been struggling on this for days
    Attached Files Attached Files

  8. #8
    Senior Member
    Join Date
    May 2010
    Location
    Russia: Western Siberia
    Posts
    268
    You wrote a very bad code actually. It's almost unreadable without some refactoring, but since it's pretty small, I found the problem right away;
    Look, here you try to add an event listener to an object that's not created yet:
    Actionscript Code:
    portfoliotween.addEventListener(TweenEvent.MOTION_FINISH, onFinish); // program: what is portfoliotween? I don't know.
    var portfoliotween:Tween = new Tween(themenu,"x",Strong.easeOut,themenu.x,-750,1,true);

    And then you create an internal function that won't be accessible after the swich code has been completed:
    Actionscript Code:
    function onFinish(e:TweenEvent):void {
                            trace("portfolio");
                        }

    I changed your code a little bit, and it worked fine:

    Actionscript Code:
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    import fl.transitions.TweenEvent;

    var buttonsArray:Array = [themenu.aboutus_btn, themenu.portfolio_btn, themenu.contact_btn];

    function setButtons():void
    {
        for (var i:String in buttonsArray)
        {
            trace(buttonsArray[i]);
            buttonsArray[i].id = i;
            buttonsArray[i].buttonMode = true;
            buttonsArray[i].mouseChildren = false;
            buttonsArray[i].mouseEnabled = true;
            buttonsArray[i].addEventListener(MouseEvent.ROLL_OVER, playOver);
            buttonsArray[i].addEventListener(MouseEvent.ROLL_OUT, playOut);
            buttonsArray[i].addEventListener(MouseEvent.CLICK, doClick);
        }
    }

    function playOver(event:MouseEvent):void
    {
        event.currentTarget.gotoAndPlay("over");
    }

    function playOut(event:MouseEvent):void
    {
        event.currentTarget.gotoAndPlay("out");
    }

    function doClick(event:MouseEvent):void
    {
       
        var currentBtn:int = event.currentTarget.id;

        setSelectedBtn(currentBtn);
    }

    function setSelectedBtn(id:int):void
    {
        for (var i:int=0; i< buttonsArray.length; i++)
        {
            if (id == i)
            {
                buttonsArray[i].gotoAndStop("down");
                buttonsArray[i].buttonMode = false;
                buttonsArray[i].mouseEnabled = false;
                buttonsArray[i].removeEventListener(MouseEvent.ROLL_OVER,playOver);
                buttonsArray[i].removeEventListener(MouseEvent.ROLL_OUT,playOut);
                buttonsArray[i].removeEventListener(MouseEvent.CLICK,doClick);

                switch (id)
                {
                    case 0 :
                        trace ("about")
                       
                        break;
                    case 1 :
                   
                        trace ("portfolio")
                        var portfoliotween:Tween = new Tween(themenu,"x",Strong.easeOut,themenu.x,-750,1,true);
                        portfoliotween.addEventListener(TweenEvent.MOTION_FINISH, onFinish);
                       
                       
                         
                        break;
                    case 2 :
                        trace ("contact")
                       
                        break;

                }
            }
            else
            {
                if (buttonsArray[i].currentLabel == "down")
                {
                    buttonsArray[i].gotoAndPlay("out");
                }
                buttonsArray[i].buttonMode = true;
                buttonsArray[i].mouseEnabled = true;
                buttonsArray[i].addEventListener(MouseEvent.ROLL_OVER,playOver);
                buttonsArray[i].addEventListener(MouseEvent.ROLL_OUT,playOut);
                buttonsArray[i].addEventListener(MouseEvent.CLICK,doClick);
            }
        }
    }
    function onFinish(e:TweenEvent):void {
                            trace("portfolio");
                        }
    setButtons();


    And, by the way, Flash IDE sucks
    Try to get used to FlashDevelop

  9. #9
    Senior Member b18269's Avatar
    Join Date
    Jan 2006
    Location
    London, UK
    Posts
    167
    caseyryan thank you I understand better know what you mean and why it was not working

    I copied a pasted the code from a 'flash author', I guess they sucked.

    I will try out FlashDevelop, you have convinced me Im more of a design than a developer but all I can do is learn learn and learn.

  10. #10
    Senior Member
    Join Date
    May 2010
    Location
    Russia: Western Siberia
    Posts
    268
    You're welcome

    Im more of a design than a developer but all I can do is learn learn and learn.
    I also thought it was better to use Flash IDE for a design. Now I'm using FlashDevelop + Flex SDK 4 for all of my projects; sites, games, AIR apps. It's much better to write everything in classes than on a timeline. And also, it comes out cleaner and more human-readable.

    As to graphics, you can easily embed them into your project by using constructs like this:
    Actionscript Code:
    [Embed(source="someimage.png")] private var SomeImage:Class;
    private var image:Bitmap = new SomeImage as Bitmap;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center