Results with trying to convert AS2 to AS3
Previously I wrote on trying to convert some AS2 to AS3. It's a sliding panel. A big panel called bigSlide onstage contains to children MC's: slide1 and slide2. Onstage there are also buttons b1, b2 and closeb.
Here's the AS2:
stop();
var currentPosition:Number = bigSlide.slide1._x;
var startFlag:Boolean = false;
menuSlide = function (input:MovieClip) {
if (startFlag == false) {
startFlag = true;
var finalDestination:Number = input._x;
var distanceMoved:Number = 0;
var distanceToMove:Number = Math.abs(finalDestination-currentPosition);
var finalSpeed:Number = .3;
var currentSpeed:Number = 0;
var dir:Number = 1;
if (currentPosition<=finalDestination) {
dir = -1;
} else if (currentPosition>finalDestination) {
dir = 1;
}
this.onEnterFrame = function() {
currentSpeed = Math.round((distanceToMove-distanceMoved+1)*finalSpeed);
distanceMoved += currentSpeed;
bigSlide._x += dir*currentSpeed;
if (Math.abs(distanceMoved-distanceToMove)<=1) {
//bigSlide._x = maskMovie._x-currentPosition+dir*distanceToMove;
currentPosition = input._x;
startFlag = false;
delete this.onEnterFrame;
}
};
}
};
b1开发者_运维技巧.onRelease = function() {
menuSlide(bigSlide.slide1);
};
bigSlide.slide1.more.onRelease = function() {
menuSlide(bigSlide.slide2);
};
b2.onRelease = function() {
menuSlide(bigSlide.slide2);
};
And here is my attempt at turning it into AS3. I really don't know what I'm doing very well. Needless to say, the script acts erratically. If anyone has any suggestions of libraries that will allow me to do the same thing without all this fuss, lemme know.
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
stop();
var currentPosition:Number = bigSlide.slide1.x;
var startFlag:Boolean = false;
var finalDestination:Number;
var distanceMoved:Number;
var distanceToMove:Number;
var finalSpeed:Number;
var currentSpeed:Number;
var dir:Number;
function menuSlide (target:MovieClip):void {
if (startFlag == false) {
startFlag = true;
finalDestination = target.x;
distanceMoved = 0;
distanceToMove = Math.abs(finalDestination-currentPosition);
finalSpeed = .3;
currentSpeed = 0;
dir = 1;
if (currentPosition<=finalDestination) {
dir = -1;
} else if (currentPosition>finalDestination) {
dir = 1;
}
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
function enterFrameHandler(event:Event):void {
var target:MovieClip = MovieClip(event.target);
currentSpeed = Math.round((distanceToMove-distanceMoved+1)*finalSpeed);
distanceMoved += currentSpeed;
bigSlide.x += dir*currentSpeed;
if (Math.abs(distanceMoved-distanceToMove)<=1) {
// bigSlide.x = maskMovie.x-currentPosition+dir*distanceToMove;
currentPosition = target.x;
startFlag = false;
this.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
}
}
}
b1.addEventListener(MouseEvent.CLICK, onClick1);
function onClick1(event:MouseEvent) {
menuSlide(bigSlide.slide1);
};
bigSlide.addEventListener(MouseEvent.CLICK, onClickBig);
function onClickBig(event:MouseEvent) {
menuSlide(bigSlide.slide2);
};
b2.addEventListener(MouseEvent.CLICK, onClick2);
function onClick2(event:MouseEvent) {
menuSlide(bigSlide.slide2);
};
closeb.addEventListener(MouseEvent.CLICK, onClickClose);
function onClickClose(event:MouseEvent) {
// root.myLoader.contentPath = null;
};
Thanks so much!
When you say "acts erratically", what does it do?
Here is how I would rewrite the AS2 into AS3
stop();
var currentPosition:Number = bigSlide.slide1._x;
var startFlag:Boolean = false;
var finalDestination:Number
var distanceMoved:Number = 0;
var distanceToMove:Number;
var finalSpeed:Number = .3;
var currentSpeed:Number = 0;
var dir:Number;
function menuSlide($target:MovieClip):void {
if (startFlag == false) {
startFlag = true;
finalDestination = $target.x;
distanceMoved = 0;
distanceToMove = Math.abs(finalDestination - currentPosition);
currentSpeed = 0;
if (currentPosition <= finalDestination) {
dir = -1;
} else if (currentPosition > finalDestination) {
dir = 1;
}
addEventListener(Event.ENTER_FRAME, enterFrameHandler, false, 0, true);
}
}
function enterFrameHandler($evt:Event):void {
currentSpeed = Math.round((distanceToMove - distanceMoved + 1) * finalSpeed);
distanceMoved += currentSpeed;
bigSlide.x += dir * currentSpeed;
if (Math.abs(distanceMoved - distanceToMove) <= 1) {
currentPosition = finalPosition;
startFlag = false;
removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
}
b1.addEventListener(MouseEvent.CLICK, onClick1, false, 0, treu);
bigSlide.slide1.more.addEventListener(MouseEvent.CLICK, onClickBig, false, 0, treu);
b2.addEventListener(MouseEvent.CLICK, onClickBig, false, 0, treu);
function onClick1($evt:MouseEvent):void {
menuSlide(bigSlide.slide1);
}
function onClickBig($evt:MouseEvent):void {
menuSlide(bigSlide.slide2);
}
精彩评论