PDA

View Full Version : Static function can't find non-static functions



Skye McCloud
January 6th, 2010, 08:30 PM
Ok, so I have a bit of a mess here (Which I'm probably not doing right anyway, which could be my problem...). I have a document class, I'll call it as Body.as, that creates an object from another class known as Headgear.as. There are also other objects created from other classes, or will be in the future.

Now, amoung all this, Headgear.as and all of the other classes Body.as will use to make objects require the use of dragging functions (starting and stopping dragging). On stopping drags, position checks are then made to compare a particular hidden movieclip with the dragged MC.

To try and save myself some hassle (from making the same functions over and over to ensuring that when the objects are created I don't need to send a complete crapload of variables just to ensure the position checking function would work), I had the functions for the event listeners (Which are set-up on the draggable MCs in Headgear.as) just direct to functions in Body.as

This worked fine... providing I made those functions in Body.as as static functions. Problem is, having the drag start and drag stop functions as static is now making my code want me to make the checkPosition function as static, which is preventing me from being able to use keywords such as "this", which I need in order to avoid the hassles I listed above.

Here are the two AS files, condensed to only include the stuff required for what I'm trying to do. Anything else is intentionally cut out (Such as imports)

Body.as

package {

public class Body extends MovieClip {

private var headgear_att:MovieClip = new HeadgearAttacher;
private var bows:Array = [bow1, bow2, bow3, bow4];
private var headItems:Array = [bows];
private var doll:MovieClip = new MovieClip;
private var headgear:Headgear = new Headgear(doll, headItems);

public static function moveThis(mc:MovieClip):void {
mc.startDrag(true);
}

public static function dropThis(mc:MovieClip):void {
mc.stopDrag();
var thisGear:MovieClip;
thisGear = MovieClip(mc);
checkPosition(thisGear); // it errors out here if checkPosition is NOT static.
}

public function checkPosition(mc:MovieClip):void {
var attaching:Boolean = false;
var splitString:Array = mc.name.split("_");
trace(this[splitString[0] + "_att"]); // it errors out here if checkPosition is static.
// missing if statement for what to do from here, so don't mind that.
if (attaching == false) {
mc.x = mc.xPos;
mc.y = mc.yPos;
}
}
}
}

Headgear.as

package {


public class Headgear extends MovieClip {

public var headClasses:Array;
public var currentHeadgear:MovieClip;
public var headContainer:DisplayObjectContainer;

public var BowsArray:Array;

public function showItems():void {
var showGear:MovieClip;
for(var x:int = 0; x < BowsArray.length; x++) {
showGear = BowsArray[x];
showGear.x = 20 * x;
showGear.y = 60;
showGear.name = "headgear_bow" + (x+1);
showGear.xPos = showGear.x;
showGear.yPos = showGear.y;
showGear.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);
showGear.addEventListener(MouseEvent.MOUSE_UP, ceaseDrag);
headContainer.addChild(showGear);
}
}

public function beginDrag(e:Event):void {
Dressup.moveThis(MovieClip(e.target));
}

public function ceaseDrag(e:Event):void {
Dressup.dropThis(MovieClip(e.target));
}
}
}

I'm fairly certain what I'm even doing is just plain retarded anyway so if there's a better way I'd love to know it.

_kp
January 7th, 2010, 04:45 AM
I don't see any reasons to use static function at all in your case ;)

you should move the code from the static function into headgear, where it belongs to.

public function beginDrag(e:Event):void {
this.startDrag(true);
}
Now there will be a problem if you need to call checkPosition from inside, but again I think it should be moved to headgear because there is no reason for it to be in the body class (only the trace won't work there).

Skye McCloud
January 7th, 2010, 06:21 AM
The only reason I put it into Body.as was because of the fact that I have to recreate these functions about 13 more times. I considered the more practical idea of a possible Utilities.as file, but I'm even less sure about implementing that than I am this.

Anyway, I'll try it that way, though now I need to remember how to access variables from Body.as in Headgear.as unless I feel like passing a dozen variables when I create the Headgear object.

_kp
January 7th, 2010, 06:31 AM
If you have more classes that will use those function you should put them in a class and extend headgear and those other classes from it.

For those variables you could simply pass a reference of body when creating the objects and get it's variables from that body object when needed.

Skye McCloud
January 7th, 2010, 04:19 PM
For those variables you could simply pass a reference of body when creating the objects and get it's variables from that body object when needed.

I was able to follow up to this. Guess I've not had enough practice at AS3 programming. If body is a document class, how is that even possible? Like, do I have it that when I make the headgear object, one of its inputs is just my entire body class? Argh on my lack of knowledge!

_kp
January 7th, 2010, 05:17 PM
Actually, I don't know if the 'document class' behaves like normal classes. If it does, it should work if you use new Headgear(this); inside of it. In Headgear(body:Body) you will have access to all public methods or properties of body.

This is more useful if you need to use some methods of body later, not just some variables in the constructor. If you only need some variables to create the object (like it's position or so) just pass them in the constructor, it shouldn't be that much more work and gives more control over the object when creating it.

Skye McCloud
January 7th, 2010, 05:40 PM
To have to pass variables was fully what I wanted to avoid, but I suppose it isn't.

However, that said, I gave a test of trying to pass in a reference of the document class. It gave an error: "Implicit coercion of a value of type Class to an unrelated type." So it appears it won't work that way. Guess I'm passing variables... argh.