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.
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.