// Copyright 2005 by Scott T. Leutenegger class ReflectSwapMC { // A class to hold information about moving MovieClips. It can be modified // to add more stuff at your liking. Advantages inlcude: // - it keeps all code about the moving MovieClip in one place // - It provides an API that "protects" the user from the MC calls // - It guarantees that you do not overwrite a mc at the same depth! // Look in the constructor, does this by the way it creates the internal // name and the depth of the object. // - It keeps track of the internal name of the MC // NOTE: must call destroy() before delete to make sure any // attached MCs are removed private var x:Number ; private var y:Number ; // current x,y location private var xv:Number ; // x velocity private var yv:Number ; // y velocities private var xscale:Number ; private var yscale:Number ; private var width:Number ; private var height:Number ; private var internalName:String ; private var imageType:String ; private var depth:Number ; private var mc:MovieClip ; // constructor function ReflectSwapMC(x:Number, y:Number, xs:Number, ys:Number, xv:Number, yv:Number, linkName:String) { // this gets the next depth level not used, ensure unique var nextDepth:Number = _root.getNextHighestDepth() ; depth = nextDepth ; // using this depth level create a unique internal name internalName = "mc" + nextDepth ; // create the MovieClip object and attach to // the mc member of this object this.mc = _root.attachMovie(linkName,internalName,nextDepth) ; // setup all the internal member data values setX(x) ; setY(y) ; setXscale(xs) ; setYscale(ys) ; setXV(xv) ; setYV(yv) ; setWidth(mc._width) ; setHeight(mc._height) ; imageType = linkName ; // record the type of image // trace("ReflectSwapMC constructor: " + x + " " + y + " " // + xv + " " + yv + " " + this.width + " " + this.height // + " " + internalName + " " + nextDepth ) ; } // set methods public function setX(newX:Number) : Void { this.mc._x = newX ; this.x = newX ;} public function setY(y:Number) : Void {this.mc._y = y ; this.y = y ;} public function setYscale(ys:Number) : Void {this.mc._yscale = ys ; this.yscale = ys ;} public function setXscale(xs:Number) : Void {this.mc._xscale = xs ; this.xscale = xs ;} public function setXV(xv:Number) : Void {this.xv = xv ;} public function setYV(yv:Number) : Void {this.yv = yv ;} public function setWidth(width:Number) : Void { this.width = width ; this.mc._width = width ; } public function setHeight(height:Number) : Void { this.height = height ; this.mc._height = height ; } public function setMC(newMC:MovieClip) : Void { this.mc = newMC ; newMC._x = this.x ; newMC._y = this.y ; newMC._width = this.width ; newMC._height = this.height ; } // get methods public function getX() : Number {return x ;} public function getY() : Number {return y ;} public function getXV() : Number {return xv ;} public function getYV() : Number {return yv ;} public function getWidth() : Number {return width ;} public function getHeight() : Number {return height ;} public function getMC() : MovieClip {return mc ;} public function getInternalName() : String {return internalName ;} public function getInternalDepth() : Number {return depth ;} // other methods public function swapImages() : Void { var newClip:String ; // get rid of the current movieClip removeMovieClip(this.mc) ; if (this.imageType == "penguin") { newClip = "gorilla" ; trace("inside swapImages, newClip == " + newClip) ; this.imageType = "gorilla" ; } else { newClip = "penguin" ; trace("inside swapImages, newClip == " + newClip) ; this.imageType = "penguin" ; } this.mc = _root.attachMovie(newClip,this.internalName,this.depth) ; // scale to reasonable size setXscale(25) ; setYscale(25) ; // set the width/height to be the new MC setWidth(this.mc._width) ; setHeight(this.mc._height) ; // set it on the right place on screen this.mc._x = this.x ; this.mc._y = this.y ; // make sure not out of bounds if (this.x >= Stage.width - this.width/2 ) this.x = this.mc._x = Stage.width - this.width/2 - 1; if (this.x <= this.width/2 ) this.x = this.mc._x = this.width/2 + 1 ; if (this.y >= Stage.height - this.height/2 ) this.y = this.mc._y = Stage.height - this.height/2 - 1; if (this.y <= this.height/2 ) this.y = this.mc._y = this.height/2 + 1 ; } // increment the X (Y) location by xv (yv) respectively public function updateX() : Void { // if the coming update will push off stage, negate velocity if ( (this.x + this.getXV() >= (Stage.width - this.width/2) ) || (this.x + this.getXV()<= this.width/2) ) setXV(this.getXV() * -1) ; // update the x location this.x += this.xv ; this.mc._x = this.x ; } public function updateY() : Void { // if the coming update will push off stage, negate velocity if ( (this.y + this.getYV() >= (Stage.height - this.height/2) ) || (this.y + this.getYV()<= this.height/2) ) setYV(this.getYV() * -1) ; this.y += this.yv ; this.mc._y = this.y ; } public function updateXY() : Void {updateX() ; updateY() ;} public function destroy() : Void { removeMovieClip(mc) ; } public function hit(mc2:ReflectSwapMC) : Boolean { if (this.mc.hitTest(mc2)) return(true) ; else return(false) ; } // print method: prints out the object contents public function print() : Void { trace("Inside ReflectSwapMC.print(): " + this.x + " " + this.y + " " + this.xv + " " + this.yv + " " + this.width + " " + this.height) ; } }