import flash.events.MouseEvent; import flash.events.KeyboardEvent; import flash.events.Event; var speed:Number = -4; var moving:Boolean = false; var swapStatus:Boolean = true; stage.addEventListener(KeyboardEvent.KEY_DOWN, trapKey); stage.addEventListener(KeyboardEvent.KEY_UP, releaseKey); stage.addEventListener(Event.ENTER_FRAME, updStage); stage.addEventListener(MouseEvent.CLICK, updSwapStatus); dog.gotoAndStop("stoppedLeft"); function updSwapStatus(evt:MouseEvent) { swapStatus = !swapStatus; if(swapStatus == true) { dog.graphics.clear(); } else { dog.graphics.beginFill(0xFF0000, 1); dog.graphics.drawCircle(0, 0, 10); } } function updStage(evt:Event):void { if(moving) { dog.x += speed; if(swapStatus == true) { for(var ch = 0; ch <= numChildren - 1; ch++) { var tempChild = getChildAt(ch); if(tempChild.name != "dog") { if(tempChild.hitTestObject(dog)) { if(getChildIndex(tempChild) != numChildren - 1) { //using setChildIndex and getChildIndex, set the index of the dog movie clip to the index of the tempChild + 1 // set index to be one higher than what object collided with dog setChildIndex(dog, getChildIndex(tempChild) + 1); } else { //using the swapChildren method swap indexes with the dog and the tempChild movie clips swapChildren(dog, tempChild); } updateIndexes(); } } } } } } function releaseKey(evt:KeyboardEvent) { if(evt.keyCode == 37 || evt.keyCode == 39) { moving = false; if(speed < 0) { dog.gotoAndStop("stoppedLeft"); } else { dog.gotoAndStop("stoppedRight"); } } } function trapKey(evt:KeyboardEvent):void { if(evt.keyCode == 39) { if(!moving) { speed = 4; dog.gotoAndPlay("runningRight"); } moving = true; } if(evt.keyCode == 37) { if(!moving) { speed = -4; dog.gotoAndPlay("runningLeft"); } moving = true; } } function updateIndexes() { for(var c = 0; c <= numChildren -1; c++) { var tempChild = getChildAt(c); tempChild.idNum.text = String(getChildIndex(tempChild)); } } updateIndexes();