Here I’ve got some boxes that individually expand as the mouse moves over them, and shrink back to their orignal size when it moves off. It works almost exactly how I would like, except that sometimes a shrinking box will unexpectedly slip behind an adjacent box that is above or below it, which is only supposed to happen when the adjacent box is the very next item being rolled over. I’m not 100% sure, but it seems like it might be doing it only when the mouse rolls outside of a box and not over another box, but it doesn’t happen every time.
If that is the case, how can I change bringToFront and nextInLine to allow for this condition? Or what would be a good solution?
The file I’ve posted has been altered a bit so that you can better see the problem. The boxes grow much larger than they normally would and the animation is slower.
package
{
import flash.display.*
import TweenBox;
import flash.events.*
public class Base extends MovieClip {
public var myTweenBox:TweenBox;
public function Base() {
createTheBoxes(7);
}
public function createTheBoxes(num:Number):void {
for(var i:uint = 0; i<num;i++) {
var b:TweenBox = new TweenBox(150, 50 + i * 35);
b.name = "myBox" + i;
addChild(b);
b.addEventListener(Event.CHANGE, bringToFront);
b.addEventListener(Event.ACTIVATE, backInLine);
}
}
public function bringToFront(e:Event):void{
var num:Number = this.numChildren-1;
var i:Number = getChildIndex(e.currentTarget);
trace(getChildIndex(e.currentTarget));
swapChildrenAt(i,num);
}
public function backInLine(e:Event):void{
var num:Number = this.numChildren-1;
var i:Number = getChildIndex(e.currentTarget);
swapChildrenAt(i,num-1);
}
}
}
package
{
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.util.*;
public class TweenBox extends MovieClip {
public var box:Shape;
private var over:Boolean;
public function TweenBox(xPos:Number, yPos:Number) {
this.x = xPos;
this.y = yPos;
over = undefined;
this.addEventListener(MouseEvent.MOUSE_OVER,overHandler);
this.addEventListener(MouseEvent.MOUSE_OUT, outHandler);
}
public function overHandler(e:MouseEvent):void {
trace("over");
over = true;
this.addEventListener(Event.ENTER_FRAME, playForwards);
if(over){
this.dispatchEvent(new Event(Event.CHANGE));
}
}
public function outHandler(e:MouseEvent):void {
trace("out");
over = false;
this.addEventListener(Event.ENTER_FRAME, playBackwards);
if(over==false){
this.dispatchEvent(new Event(Event.ACTIVATE));
}
}
public function playForwards(e:Event):void {
if (over == true){
this.nextFrame();
}
}
public function playBackwards(e:Event):void {
if(over==false){
this.prevFrame();
}
}
}
}
Fingers