A Flash Developer Resource Site

Page 2 of 4 FirstFirst 1234 LastLast
Results 21 to 40 of 61

Thread: TypeError: Error #1009: Cannot access a property or method of a null object reference

  1. #21
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It'd be a lot easier if we knew which line. That could be due to dropMovie running before the targetpiece is set. Try this:
    PHP Code:
            private function dropMovie(event:MouseEvent):void
            
    {
                
    this.stopDrag();
                
    this.filters = [];
                
    stopDrag();
                if ((
    dropTarget != null) && (_targetPiece != null) && (_targetPiece.contains(dropTarget))){
                           
    removeEventListener(MouseEvent.MOUSE_DOWNdragMovie);
                           
    removeEventListener(MouseEvent.MOUSE_UPdropMovie);
                           
    buttonMode false;
                           
    _targetPiece.x;
                           
    _targetPiece.y;
                } else {
                       
    _origX;
                       
    _origY;
                }
            } 

  2. #22
    Member
    Join Date
    Nov 2009
    Posts
    46
    I ran it with that one line of code update and now it has gone back to not dropping at all, just snapping back on mouse up. Any other ideas? Also, how could I get you the line numbers you're asking for?

  3. #23
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I think you should get the line numbers if you compile for debug rather than production. I don't know, I don't use the IDE.

    I also thought that I might have gotten that contains line backwards. Try swapping which one is checking for containing the other. Also, trace out _targetPiece and dropTarget in that function.

  4. #24
    Member
    Join Date
    Nov 2009
    Posts
    46

    stripped down fla

    Hi !
    By the way, thank you so much for your help. I am supposed to get this in today !!

    I have uploaded the fla.
    Attached Files Attached Files

  5. #25
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I don't have access to flash here, and if it's CS4, I won't have access to it at home either. I hope that you can solve your issues.

  6. #26
    Member
    Join Date
    Nov 2009
    Posts
    46
    I changed the code below and got the following error. Did I switch them right? I added the trace statements.

    1061: Call to a possibly undefined method contains through a reference with static type flash.displayisplayObject.

    I have CS3.

    PHP Code:

    if ((dropTarget != null) && (_targetPiece != null) && (dropTarget.contains(_targetPiece))){
                           
    trace (dropTarget);
                        
    trace (_targetPiece);
                        
    removeEventListener(MouseEvent.MOUSE_DOWNdragMovie);
                           
    removeEventListener(MouseEvent.MOUSE_UPdropMovie);
                           
    buttonMode false;
                           
    _targetPiece.x;
                           
    _targetPiece.y;
                 } else {
                           
    _origX;
                       
    _origY;
                 } 

  7. #27
    Member
    Join Date
    Nov 2009
    Posts
    46
    It was line 41 - which is the first line of the if/then statement with the drop target contains. etc.

  8. #28
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You did change it the way I intended, but I forgot that contains is a method of DisplayObjectContainer, not DisplayObject. You could cast, but dropTarget is not guaranteed to actually be a DisplayObjectContainer. Of course, if it's not a container, it doesn't contain anything. What did the traces say before you swapped the contains?

  9. #29
    Member
    Join Date
    Nov 2009
    Posts
    46
    This was the code before. But the traces did not return anything and the pieces did not drop at all.

    PHP Code:
    if ((dropTarget != null) && (_targetPiece != null) && (_targetPiece.contains(dropTarget))){
                        
    trace(_targetPiece);
                        
    trace(dropTarget); 
    Also, what should I change the * to? You mentioned before DisplayObject....still do that...or DisplayObjectContainer??
    PHP Code:
    public var _targetPiece:*; 

  10. #30
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Since you know the targets will be MovieClips, you could even do that. But DisplayObjectContainer or Sprite seems like the best choice. Doing that, realize that you cannot set the target to be a TextField or Shape or something. I'm really not sure of the relationship between the dropTarget and the _targetPiece you've set, which is why i wanted the traces. Move them up outside of the if so that we see them regardless of whether the condition is fulfilled.

  11. #31
    Member
    Join Date
    Nov 2009
    Posts
    46
    Ok, so some success here! Thanks! So there are actually 2 instances of, for example, the airplane. The lighter one (lower alpha) that is being generated automatically by the addChild to the stage. When I click on that one and bring it to the target, I get the Whoooooot!!! Is it possible to make the instance of the airplane that is 100% alpha that I place on the stage manually in an earlier frame also react to the drop target? If so, how?

  12. #32
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Basically, you need to get the original instances when you are setting the targets and checking stuff up, instead of the more transparent instances. If they have instancenames, you should be able to access them within the setUpTargets method.

  13. #33
    Member
    Join Date
    Nov 2009
    Posts
    46

    Adding Sound Upon Dropping to Target

    Hi again,
    I am trying to have a short sound play if the mode of transportation is dragged to the correct year. I have tried to add the code...but does not work. Could you please review it and let me know how the code should be?
    Thanks!

    PHP Code:
    package
    {
        
    import flash.display.MovieClip;
        
    import flash.events.MouseEvent;
        
    import flash.display.DisplayObject;
        
    import flash.media.Sound;
        
    import flash.media.SoundChannel;
        
    import flash.net.URLRequest;
        
        
    import DragDrop;
        
    import TN;
        
    import AP;
        
    import CW;


        
        public class 
    DragGame extends MovieClip
        
    {
            private var 
    tn:TN;
            private var 
    ap:AP;
            private var 
    cw:CW;


            
            public function 
    DragGame()
            {
                
    createPieces();
                
            }
            
            private function 
    createPieces():void
            
    {
                
    tn = new TN();
                
    addChild(tn);
                
    tn.169.9;
                
    tn.90.8;
                
    tn.width 370.9;
                
    tn.height 68;
                
    tn.alpha 0.3;
                
                
                
    ap = new AP();
                
    addChild(ap);
                
    ap.496.6;
                
    ap.169.9;
                
    ap.width 162.8;
                
    ap.height 110.2;
                
    ap.alpha 0.3;
                
                
                
    cw = new CW();
                
    addChild(cw);
                
    cw.44.8;
                
    cw.193.1;
                
    cw.width 197.0;
                
    cw.height 94;
                
    cw.alpha 0.3;
                
            }
            
            public function 
    setUpTargets():void
            
    {
               var 
    correctsoundReq:URLRequest = new URLRequest("correct1.wav");
               
    //var wrongsoundReq:URLRequest = new URLRequest ("audio/wrong1.wav");
               
    var correctsound:Sound = new Sound();
               
    //var wrongsound:Sound = new Sound();
               
    correctsound.load(correctsoundReq);
               
    //wrongsound.load(wrongsoundReq);
               
               
               
    tn._targetPiece ttn_mc;
               
    tn.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    correctsound.addEventListener(Event.COMPLETEcheckTarget);
               
    trace(ttn_mc);

               
    ap._targetPiece tap_mc;
               
    ap.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    trace(tap_mc);

               
    cw._targetPiece tcw_mc;
               
    cw.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    trace(tcw_mc);
               
               
    apm._targetPiece tap_mc;
               
    apm.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    trace(tap_mc);
               
               
    cwm._targetPiece tcw_mc;
               
    cwm.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    trace(tcw_mc);
               
               
    tnm._targetPiece ttn_mc;
               
    tnm.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    trace(ttn_mc);
               
            }
            
            
            
    /*private function onComplete(event:Event):void
            {
                correctsound.play();
            }*/
            
            
    private function checkTarget(event:MouseEvent):void
           
    {
               var 
    ec:MovieClip MovieClip(event.currentTarget);
               var 
    target:DisplayObject DisplayObject(ec._targetPiece);
               
               
               if(
    ec.hitTestObject(target))
               {
                   
    trace("whooooot!");
                   
    correctsound.play();
                   
    ec.target.x;
                   
    ec.target.y;
                   
    ec.removeEventListener(MouseEvent.MOUSE_UPcheckTarget);
                   
    ec.disable();
               }
               else
               {
                   
    //wrongsound.play();
                   
    ec.ec._origX;
                   
    ec.ec._origY;
               }
           }
       }
        


  14. #34
    Member
    Join Date
    Nov 2009
    Posts
    46
    I am getting undefined property errors for "Event" and for "correctsound".

  15. #35
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You need to import Event like you did with MouseEvent.

    You declared correctsound as a local variable in setUpTargets. That means that that variable has no meaning outside of that function. You should move the declaration up to the class level in order to use it in checkTarget.

  16. #36
    Member
    Join Date
    Nov 2009
    Posts
    46

    moving declaration to higher level?

    Hi again,
    So I added the "import flash.events.Event;" to the package. And I tried to move the var declarations in the package area and the public function DragGame area and the private function createPieces...and I couldn't get them to work...where exactly should they go? With the code as it is below, I am getting an error on line 119, the line in the checkTarget function that says, "correctsound.play();"

    PHP Code:
    package
    {
        
    import flash.display.MovieClip;
        
    import flash.events.MouseEvent;
        
    import flash.display.DisplayObject;
        
    import flash.media.Sound;
        
    import flash.media.SoundChannel;
        
    import flash.net.URLRequest;
        
    import flash.events.Event;
        
        
    import DragDrop;
        
    import TN;
        
    import AP;
        
    import CW;


        
        public class 
    DragGame extends MovieClip
        
    {
            private var 
    tn:TN;
            private var 
    ap:AP;
            private var 
    cw:CW;


            
            public function 
    DragGame()
            {
                
    createPieces();
                
                
            }
            
            private function 
    createPieces():void
            
    {
                
                        
               var 
    correctsoundReq:URLRequest = new URLRequest("correct1.wav");
               
    //var wrongsoundReq:URLRequest = new URLRequest ("audio/wrong1.wav");
               
    var correctsound:Sound = new Sound();
               
    //var wrongsound:Sound = new Sound();
               
    correctsound.load(correctsoundReq);
               
    //wrongsound.load(wrongsoundReq);
               
    correctsound.addEventListener(Event.COMPLETEcheckTarget);
                
                
    tn = new TN();
                
    addChild(tn);
                
    tn.169.9;
                
    tn.90.8;
                
    tn.width 370.9;
                
    tn.height 68;
                
    tn.alpha 0.3;
                
                
                
    ap = new AP();
                
    addChild(ap);
                
    ap.496.6;
                
    ap.169.9;
                
    ap.width 162.8;
                
    ap.height 110.2;
                
    ap.alpha 0.3;
                
                
                
    cw = new CW();
                
    addChild(cw);
                
    cw.44.8;
                
    cw.193.1;
                
    cw.width 197.0;
                
    cw.height 94;
                
    cw.alpha 0.3;
                
            }
            
            public function 
    setUpTargets():void
            
    {

               
               
    tn._targetPiece ttn_mc;
               
    tn.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
               
    trace(ttn_mc);

               
    ap._targetPiece tap_mc;
               
    ap.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    trace(tap_mc);

               
    cw._targetPiece tcw_mc;
               
    cw.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    trace(tcw_mc);
               
               
    apm._targetPiece tap_mc;
               
    apm.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    trace(tap_mc);
               
               
    cwm._targetPiece tcw_mc;
               
    cwm.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    trace(tcw_mc);
               
               
    tnm._targetPiece ttn_mc;
               
    tnm.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    trace(ttn_mc);
               
            }
            
            
            
    /*private function onComplete(event:Event):void
            {
                correctsound.play();
            }*/
            
            
    private function checkTarget(event:MouseEvent):void
           
    {
               var 
    ec:MovieClip MovieClip(event.currentTarget);
               var 
    target:DisplayObject DisplayObject(ec._targetPiece);
               
               
               if(
    ec.hitTestObject(target))
               {
                   
    trace("whooooot!");
                   
    correctsound.play();
                   
    ec.target.x;
                   
    ec.target.y;
                   
    ec.removeEventListener(MouseEvent.MOUSE_UPcheckTarget);
                   
    ec.disable();
               }
               else
               {
                   
    //wrongsound.play();
                   
    ec.ec._origX;
                   
    ec.ec._origY;
               }
           }
       }
        


  17. #37
    Member
    Join Date
    Nov 2009
    Posts
    46
    Also, I'd like to specify particular x and y locations of where the final placement of the dropped object should go once it has hit the target. Can you help with code for that?

  18. #38
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It should go with the other class level variables, tn, ap, and cw.

    PHP Code:
    package
    {
        
    import flash.display.MovieClip;
        
    import flash.events.MouseEvent;
        
    import flash.display.DisplayObject;
        
    import flash.media.Sound;
        
    import flash.media.SoundChannel;
        
    import flash.net.URLRequest;
        
    import flash.events.Event;
        
        
    import DragDrop;
        
    import TN;
        
    import AP;
        
    import CW;


        
        public class 
    DragGame extends MovieClip
        
    {
            private var 
    tn:TN;
            private var 
    ap:AP;
            private var 
    cw:CW;
            private var 
    correctsound:Sound;

            public function 
    DragGame()
            {
                
    createPieces();
                
                
            }
            
            private function 
    createPieces():void
            
    {
                
                        
               var 
    correctsoundReq:URLRequest = new URLRequest("correct1.wav");
               
    //var wrongsoundReq:URLRequest = new URLRequest ("audio/wrong1.wav");
               
    correctsound = new Sound();
               
    //var wrongsound:Sound = new Sound();
               
    correctsound.load(correctsoundReq);
               
    //wrongsound.load(wrongsoundReq);
               
    correctsound.addEventListener(Event.COMPLETEcheckTarget);
                
                
    tn = new TN();
                
    addChild(tn);
                
    tn.169.9;
                
    tn.90.8;
                
    tn.width 370.9;
                
    tn.height 68;
                
    tn.alpha 0.3;
                
                
                
    ap = new AP();
                
    addChild(ap);
                
    ap.496.6;
                
    ap.169.9;
                
    ap.width 162.8;
                
    ap.height 110.2;
                
    ap.alpha 0.3;
                
                
                
    cw = new CW();
                
    addChild(cw);
                
    cw.44.8;
                
    cw.193.1;
                
    cw.width 197.0;
                
    cw.height 94;
                
    cw.alpha 0.3;
                
            }
            
            public function 
    setUpTargets():void
            
    {

               
               
    tn._targetPiece ttn_mc;
               
    tn.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
               
    trace(ttn_mc);

               
    ap._targetPiece tap_mc;
               
    ap.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    trace(tap_mc);

               
    cw._targetPiece tcw_mc;
               
    cw.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    trace(tcw_mc);
               
               
    apm._targetPiece tap_mc;
               
    apm.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    trace(tap_mc);
               
               
    cwm._targetPiece tcw_mc;
               
    cwm.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    trace(tcw_mc);
               
               
    tnm._targetPiece ttn_mc;
               
    tnm.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    trace(ttn_mc);
               
            }
            
            
            
    /*private function onComplete(event:Event):void
            {
                correctsound.play();
            }*/
            
            
    private function checkTarget(event:MouseEvent):void
           
    {
               var 
    ec:MovieClip MovieClip(event.currentTarget);
               var 
    target:DisplayObject DisplayObject(ec._targetPiece);
               
               
               if(
    ec.hitTestObject(target))
               {
                   
    trace("whooooot!");
                   
    correctsound.play();
                   
    ec.target.x;
                   
    ec.target.y;
                   
    ec.removeEventListener(MouseEvent.MOUSE_UPcheckTarget);
                   
    ec.disable();
               }
               else
               {
                   
    //wrongsound.play();
                   
    ec.ec._origX;
                   
    ec.ec._origY;
               }
           }
       }
        

    For moving the object after it has successfully been dropped, you'd want to change the x and y in the section where you have 'whoooot'. You can set a particular target point as a property on the objects and use that.

  19. #39
    Member
    Join Date
    Nov 2009
    Posts
    46
    So you have to have both lines of code below?

    var correctsoundReq:URLRequest = new URLRequest("correct1.wav");
    and
    private var correctsound:Sound;

    Now I am getting this error:
    TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::Event@22d6eeb1 to flash.events.MouseEvent.

    Where should this part of the code go:
    PHP Code:
               var correctsoundReq:URLRequest = new URLRequest("correct1.wav");
               
    //var wrongsoundReq:URLRequest = new URLRequest ("audio/wrong1.wav");
               
    var correctsound:Sound = new Sound();
               
    //var wrongsound:Sound = new Sound();
               
    correctsound.load(correctsoundReq);
               
    //wrongsound.load(wrongsoundReq);
               
    correctsound.addEventListener(Event.COMPLETEcheckTarget); 

  20. #40
    Member
    Join Date
    Nov 2009
    Posts
    46

    Added x and y and now getting new error

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at DragGame/checkTarget()

    I added the x and y to the instances on the stage....what is wrong here?
    PHP Code:
    package
    {
        
    import flash.display.MovieClip;
        
    import flash.events.MouseEvent;
        
    import flash.display.DisplayObject;
        
    import flash.media.Sound;
        
    import flash.media.SoundChannel;
        
    import flash.net.URLRequest;
        
    import flash.events.Event;
        
        
    import DragDrop;
        
    import TN;
        
    import AP;
        
    import CW;


        
        public class 
    DragGame extends MovieClip
        
    {
            private var 
    tn:TN;
            private var 
    ap:AP;
            private var 
    cw:CW;
            private var 
    correctsound:Sound;


            
            public function 
    DragGame()
            {
                
    createPieces();
                
                
            }
            
            private function 
    createPieces():void
            
    {
                
                        
               var 
    correctsoundReq:URLRequest = new URLRequest("correct1.wav");
               
    //var wrongsoundReq:URLRequest = new URLRequest ("audio/wrong1.wav");
               
    var correctsound:Sound = new Sound();
               
    //var wrongsound:Sound = new Sound();
               
    correctsound.load(correctsoundReq);
               
    //wrongsound.load(wrongsoundReq);
               
    correctsound.addEventListener(Event.COMPLETEcheckTarget);
                
                
    tn = new TN();
                
    addChild(tn);
                
    tn.169.9;
                
    tn.90.8;
                
    tn.width 370.9;
                
    tn.height 68;
                
    tn.alpha 0.3;
                
                
                
    ap = new AP();
                
    addChild(ap);
                
    ap.496.6;
                
    ap.169.9;
                
    ap.width 162.8;
                
    ap.height 110.2;
                
    ap.alpha 0.3;
                
                
                
    cw = new CW();
                
    addChild(cw);
                
    cw.44.8;
                
    cw.193.1;
                
    cw.width 197.0;
                
    cw.height 94;
                
    cw.alpha 0.3;
                
            }
            
            public function 
    setUpTargets():void
            
    {

               
               
    tn._targetPiece ttn_mc;
               
    tn.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
               
    trace(ttn_mc);

               
    ap._targetPiece tap_mc;
               
    ap.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    trace(tap_mc);

               
    cw._targetPiece tcw_mc;
               
    cw.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    trace(tcw_mc);
               
               
    apm._targetPiece tap_mc;
               
    apm.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    trace(tap_mc);
               
               
    cwm._targetPiece tcw_mc;
               
    cwm.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    trace(tcw_mc);
               
               
    tnm._targetPiece ttn_mc;
               
    tnm.addEventListener(MouseEvent.MOUSE_UPcheckTarget);
               
    trace(ttn_mc);
               
            }
            
            
            
    /*private function onComplete(event:Event):void
            {
                correctsound.play();
            }*/
            
            
    private function checkTarget(event:MouseEvent):void
           
    {
               var 
    ec:MovieClip MovieClip(event.currentTarget);
               var 
    target:DisplayObject DisplayObject(ec._targetPiece);
               
               
               if(
    ec.hitTestObject(target))
               {
                   
    trace("whooooot!");
                   
    correctsound.play();
                   
    ec.target.x;
                   
    ec.target.y;
                   
    apm.497.6;
                   
    apm.409.9;
                   
    cwm.50.3;
                   
    cwm.426.1;
                   
    tnm.181.9;
                   
    tnm.295.8;
                   
    ec.removeEventListener(MouseEvent.MOUSE_UPcheckTarget);
                   
    ec.disable();
               }
               else
               {
                   
    //wrongsound.play();
                   
    ec.ec._origX;
                   
    ec.ec._origY;
               }
           }
       }
        


Tags for this Thread

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