Scoping Problems, I Think

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

}

What happens if you replace

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

with

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

?

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

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

with

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

?[/quote]

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.

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

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.

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]

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;

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