I thought I would try something new and make a class that uses composition to control a movieclip rather than inheritance. I’ve run into a bit of a snag though, so maybe someone can help me out. I’ll make a very basic example, just to show what I’m asking.
class Robot {
private var xspeed:Number;
private var yspeed:Number;
private var robot_name:String;
private var robot_mc:MovieClip;
private var container_mc:MovieClip;
public function Robot(container_mc:MovieClip, robot_mc:String, robot_name:String, xspeed:Number, yspeed:Number){
this.container_mc = container_mc;
this.robot_name = robot_name;
this.robot_mc = this.container_mc.attachMovie(robot_mc, robot_name, container_mc.getNextHighestDepth());
this.xspeed = xspeed;
this.yspeed = yspeed;
this.robot_mc.onEnterFrame = function(){
this._x += xspeed;
this._y += yspeed;
}
}
public function doSomething(){
trace("I did something!!!!");
}
}
and on the first frame of the movie using this class…
// 'this' being _root, where the movieclip with the ID "robot" will be attached from the library. Sparky is the robots name, 5 = xspeed, 5 = yspeed.
aRobot:Robot = new Robot(this, "robot", "Sparky", 5, 5);
Ok, the problem I’m having is how would i get the xspeed and yspeed properties to work in the robot_mc’s onEnterFrame event. Also, would there be a way to call the doSomething() method from within the robot_mc’s onEnterFrame event?
Thanks for anyone who can help me, even if all you have to say is “This is not a job for composition, use inheritance!”
when you’re in the onEnterFrame event, or any event handler or function defined that way for that matter, you’re in the scope of the movieclip, not the class. So when you’re looking for the xspeed and yspeed properties in there, you get nothing (and since you arent inheriting, the movieclip has no luck searching its inheritance chain either). What to do? Two basic solutions.
assign the movieclip a reference to the class. This way, while in the scope of the movieclip, you can use this reference to access the class instance controlling it (as well as its properties). This would act much in the way the _parent reference does, but for the class instance and not the containing movieclip
provide local variables within the scope of which the event function is defined. Functions inherently have access to local variables within the scope in which the function was written - this meaning the scope of the script, independant of where the function variable is defined. For example, no matter what object you chose to define function A in, as long as its defined in the script on the main timeline (_root) then it will have access to all local variables in _root. Local variables (and this does not include variables passed in by argument) for the event function to access can be provided scope in which the function was defined - in your case, the Robot constructor.
Excellent, thank you. The parentClass works great for the properties, but it’s messing up my methods… probably because they weren’t made with this in mind. I’ll probably have to re-write some of them to get it to work… but maybe I should have just used inheritance .
btw, good luck in the BBForum Battle!! (I know you wont’ need it though :hugegrin: )