# Scoping Problems, I Think

**URL:** <https://forum.kirupa.com/t/scoping-problems-i-think/264124>\
**Category:** flash\
**Created:** [June 23, 2008, 1:45pm UTC](https://forum.kirupa.com/t/scoping-problems-i-think/264124 "2008-06-23T13:45:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bencam](https://avatars.discourse-cdn.com/v4/letter/b/d6d6ee/32.png) [@bencam](https://forum.kirupa.com/u/bencam)\
**Post date:** [June 23, 2008, 1:45pm UTC](https://forum.kirupa.com/t/scoping-problems-i-think/264124/1 "2008-06-23T13:45:27Z")

</div>

I have code that works when placed in a keyframe on the main timeline, but doesn’t behave the same when it’s in a package in an .as file. The linkage is correct for the document and movieclips, but I am missing some step. I’d appreciate any help. I’ve denoted the specific lines that are generating an error.

package myPackage {  
import flash.display.MovieClip;  
import flash.events.MouseEvent;  
import flash.display.DisplayObject;  
import flash.display.Scene;  
import flash.external.ExternalInterface;

```
public class Marble extends MovieClip{
    public function Marble(){
        addEventListener(MouseEvent.MOUSE_DOWN, marbleDrag);
        addEventListener(MouseEvent.MOUSE_UP, marbleDrop);
    }
    public function marbleDrag(event:MouseEvent):void {
        event.target.startDrag(true);
        startX = event.target.x;
        startY = event.target.y;
        trace(event.target.name);
    }
    public function marbleDrop(event:MouseEvent):void {
        event.target.stopDrag();

```

[COLOR=Red]//I THINK THESE CREATE ERRORS IN MY SUBSEQUENT CODE  
var myTargetName:String = “target” + event.target.name;  
var myTarget:DisplayObject = getChildByName(this.root.myTargetName);[/COLOR]

```
        trace(event.target.dropTarget.name,event.target.dropTarget.parent.name);
        if (event.target.dropTarget.parent == myTarget){
            trace("target acquired");
            event.target.x = myTarget.x;
            event.target.y = myTarget.y;
        }
        else {
            trace("target lost");
            event.target.x = startX;
            event.target.y = startY;
        }
    }
}

```

}

---

<div class="post-metadata">

**Author:** ![DiamondDog](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DiamondDog](https://forum.kirupa.com/u/DiamondDog)\
**Post date:** [June 23, 2008, 2:57pm UTC](https://forum.kirupa.com/t/scoping-problems-i-think/264124/2 "2008-06-23T14:57:15Z")

</div>

What happens if you replace

```auto
[COLOR=Red]var myTargetDisplayObject = getChildByName(this.root.myTargetName);[/COLOR]

```

with

```auto
[COLOR=Red]var myTargetDisplayObject = this.root.getChildByName(myTargetName);[/COLOR]

```

?

---

<div class="post-metadata">

**Author:** ![bencam](https://avatars.discourse-cdn.com/v4/letter/b/d6d6ee/32.png) [@bencam](https://forum.kirupa.com/u/bencam)\
**Post date:** [June 23, 2008, 3:08pm UTC](https://forum.kirupa.com/t/scoping-problems-i-think/264124/3 "2008-06-23T15:08:25Z")

</div>

I get an error message: Error #1009: Cannot access a property or method of a null object reference.

The problem is always that it is a null object. If I don’t have the code in a package (ie directly on the keyframe of the main timeline), a trace reveals the name of the TargetDisplayObject. When I place the function in a package, the Display Object becomes a null object.

[quote=DiamondDog;2347646]What happens if you replace

```auto
[COLOR=Red]var myTargetDisplayObject = getChildByName(this.root.myTargetName);[/COLOR]

```

with

```auto
[COLOR=Red]var myTargetDisplayObject = this.root.getChildByName(myTargetName);[/COLOR]

```

?[/quote]

---

<div class="post-metadata">

**Author:** ![energy](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@energy](https://forum.kirupa.com/u/energy)\
**Post date:** [June 23, 2008, 6:26pm UTC](https://forum.kirupa.com/t/scoping-problems-i-think/264124/4 "2008-06-23T18:26:45Z")

</div>

Ok i am not sure if i understood what are you trying and wht error you are getting but the below does not give any error to me.

```auto
package myPackage {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.DisplayObject;
import flash.display.Scene;
import flash.external.ExternalInterface;

public class Marble extends MovieClip{
public function Marble(){
addEventListener(MouseEvent.MOUSE_DOWN, marbleDrag);
addEventListener(MouseEvent.MOUSE_UP, marbleDrop);
}
public function marbleDrag(event:MouseEvent):void {
event.target.startDrag(true);
var startX = event.target.x;
var startY = event.target.y;
trace(event.target.name);
}
public function marbleDrop(event:MouseEvent):void {
event.target.stopDrag();

var myTargetName:String = "target" + event.target.name;
var myTargetisplayObject = getChildByName(myTargetName);

//trace(event.target.dropTarget.name,event.target.dropTarget.parent.name);
}
}
}

```

---

<div class="post-metadata">

**Author:** ![bencam](https://avatars.discourse-cdn.com/v4/letter/b/d6d6ee/32.png) [@bencam](https://forum.kirupa.com/u/bencam)\
**Post date:** [June 23, 2008, 8:23pm UTC](https://forum.kirupa.com/t/scoping-problems-i-think/264124/5 "2008-06-23T20:23:36Z")

</div>

Well, I have a movieclip with the instance name “marble” on the stage and a movieclip with an instance name of “targetmarble” on the stage. I receive the error message when I drag “marble” over “targetmarble”.

[quote=energy;2347795]Ok i am not sure if i understood what are you trying and wht error you are getting but the below does not give any error to me.

```auto
package myPackage {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.DisplayObject;
import flash.display.Scene;
import flash.external.ExternalInterface;

public class Marble extends MovieClip{
public function Marble(){
addEventListener(MouseEvent.MOUSE_DOWN, marbleDrag);
addEventListener(MouseEvent.MOUSE_UP, marbleDrop);
}
public function marbleDrag(event:MouseEvent):void {
event.target.startDrag(true);
var startX = event.target.x;
var startY = event.target.y;
trace(event.target.name);
}
public function marbleDrop(event:MouseEvent):void {
event.target.stopDrag();

var myTargetName:String = "target" + event.target.name;
var myTargetisplayObject = getChildByName(myTargetName);

//trace(event.target.dropTarget.name,event.target.dropTarget.parent.name);
}
}
}

```

[/quote]

---

<div class="post-metadata">

**Author:** ![bencam](https://avatars.discourse-cdn.com/v4/letter/b/d6d6ee/32.png) [@bencam](https://forum.kirupa.com/u/bencam)\
**Post date:** [June 24, 2008, 6:34pm UTC](https://forum.kirupa.com/t/scoping-problems-i-think/264124/6 "2008-06-24T18:34:52Z")

</div>

Thanks for the help. That did solve one problem. The other problem I’ve realized is that event.target.dropTarget has a null value, so that when I do the comparison, it throws an error. I understand why this.root.getChildByName works, but it doesn’t translate in the same way with an event.

public function marbleDrop(event:MouseEvent):void {  
event.target.stopDrag();  
for(var i=1;i\<=numberOfCallouts;i++) {  
var DragTargetName:String = “callout\_” + i;  
var DragTarget:DisplayObject = [COLOR=Magenta]this.root.getChildByName/COLOR;  
if ([COLOR=Red]event.target.dropTarget[/COLOR].parent == DragTarget) {  
event.target.x = DragTarget.x;  
event.target.y = DragTarget.y;  
break;  
}  
else {  
event.target.x = initialX;  
event.target.y = initialY;

---

<div class="post-metadata">

**Author:** ![bencam](https://avatars.discourse-cdn.com/v4/letter/b/d6d6ee/32.png) [@bencam](https://forum.kirupa.com/u/bencam)\
**Post date:** [June 24, 2008, 7:33pm UTC](https://forum.kirupa.com/t/scoping-problems-i-think/264124/7 "2008-06-24T19:33:34Z")

</div>

Okay, not sure what I did, but it seems to work now. Thanks for the help.

public function marbleDrop(event:MouseEvent):void {  
event.target.stopDrag();  
for(var i=1;i\<=numberOfCallouts;i++) {  
var DragTargetName:String = “callout\_” + i;  
var DragTarget:DisplayObject = this.root.getChildByName(DragTargetName);  
if (event.target.dropTarget.parent == DragTarget) {  
event.target.x = DragTarget.x;  
event.target.y = DragTarget.y;  
break;  
}  
else {  
event.target.x = initialX;  
event.target.y = initialY;  
}  
}  
}
