PDA

View Full Version : Problem with a class


SammyFM
05-24-2008, 01:59 PM
Hy, i wrote a class and get the following error message in the panel (but seems to work!):

TypeError: Error #1006: pickUp is not a function.
at DragDropClass/::init()
at DragDropClass$iinit()

my class:

package
{
import flash.display.MovieClip;
import flash.geom.ColorTransform;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.MouseEvent;

public class DragDropClass extends MovieClip
{
private var menu_array:Array;
private var startX:Number;
private var startY:Number;
private var endX:Number;
private var endY:Number;

public function DragDropClass()
{
init();
}

private function init():void
{
menu_array=new Array(anastasia)

addMenuEvents();
menu_array.pickUp();
menu_array.dropIt();
}

public function addMenuEvents():void
{
for(var i:int=0;i < menu_array.length;i++)
{
menu_array[i].mouseChildren=false;
menu_array[i].buttonMode=true;

menu_array[i].addEventListener(MouseEvent.MOUSE_DOWN,pickUp);
menu_array[i].addEventListener(MouseEvent.MOUSE_UP,dropIt);
}
}

private function pickUp(event:MouseEvent):void
{
event.currentTarget.startDrag(true);
event.currentTarget.parent.addChild(event.currentT arget);
startX = event.currentTarget.x;
startY = event.currentTarget.y;
}

private function dropIt(event:MouseEvent):void
{
event.currentTarget.stopDrag();

if (event.currentTarget.dropTarget != null && event.currentTarget.dropTarget.parent == model ||
event.currentTarget.dropTarget != null && event.currentTarget.dropTarget.parent == model2)
{
event.currentTarget.x = event.currentTarget.startX;
event.currentTarget.y = event.currentTarget.startY;
}
else
{
event.currentTarget.x = event.currentTarget.endX;
event.currentTarget.y = event.currentTarget.endY;
}
}

}
}

Can somebody tell me the problem over here?

Thx Sammy

wvxvw
05-24-2008, 10:20 PM
menu_array.pickUp(); // Array has no pickUp() method
This is the line that throws the error, however there're few more errors... like in previous (anastasia is undefined) and the following line (Array has no dropIt() method too) and this:
if (event.currentTarget.dropTarget != null && event.currentTarget.dropTarget.parent == model ||
event.currentTarget.dropTarget != null && event.currentTarget.dropTarget.parent == model2) // model, model1 and model2 are not visible from this scope (or do not exist).

SammyFM
05-25-2008, 11:14 AM
But i defined "pickUp" as afunction, and anastasia is set as a array type.
Do i have to tell flash that anastasia is mc? i donīthink so , itīs a mc on the stage!

wvxvw
05-25-2008, 04:19 PM
>> But i defined "pickUp"
<< you defined pickUp() for the class DragDropClass not for the Array class.

>> and anastasia is set as a array type
<< to cast types you have to either use a constructor function on the object you want to cast to another type, or use as (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/operators.html#as) operator *clickable*. What you've done in your code was an attemt to create an array containing 1 item, and it's undefined.
>> Do i have to tell flash that anastasia is mc?
<< Yes, and the way you tried to access your clip it won't work. You have to either use getChildByName (http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/DisplayObjectContainer.html#getChildByName()) *clickable* or dynamicaly create it and pass the class the link to it (2nd way is prefferable).

SammyFM
05-26-2008, 07:56 AM
Iīm sorry, im new at as3. could you write down how it should be?

Iīm still learning :rolleyes:

SammyFM
05-26-2008, 01:58 PM
Ok, in my Main Class i can do it. i just type e.g. this.getChildByName("model").x=22; and my model mc moves there.

But how can i define that in my subClass?

wvxvw
05-26-2008, 08:24 PM
go to the library, select the clip "model", export it for ActionScript, define a class for it (leave the base class unchanged). In the class you create for the clip 1. extend MovieClip. 2. add your desired functionality.
Now you can use in your document class something like this:
var instanceOfModel:Model = new Model();
addChild(instanceOfModel);
Or, if you want to leave it on the stage, than:
var instanceOfModel:Model = getChildByName("model") as Model;
The second way is less practical and makes you store 1 useless string in your class ("model").

SammyFM
05-27-2008, 08:33 AM
Thanx!!!