;

PDA

Click to See Complete Forum and Search --> : Help me translate AS2 to AS3


rogueblade
04-18-2009, 01:45 AM
Ok so I was looking for a nice tutorial on scrubbing through a video by mousing down over it and dragging through the frames but it couldn't actually find any (not a scrub bar where the flv loads).....

But I've managed to get a hold of this AS2 code for what I'm trying to do. Now I know nothing about AS2 but I can sort of work out for myself whats going on here. But I cant translate it to AS3. Can someone please help me write this out in AS3 syntax/format


onMouseDown = function() { //when the mouse is clicked
mouseLocV = _xmouse; // mouseLoc = x co-ordinates of the mouse at time of clicking

imgMC.onEnterFrame = function() { //initiate a constant checking of the mouse x location
differenceV = _xmouse - mouseLocV; //finds the difference between the current mouse location and the previous location

differenceV = differenceV / 10; //***makes the movieclip advance 1 frame for every 10 pixels the mouse moved

gotoFrameV = (imgMC._currentframe + differenceV) % imgMC.totalFrames; //finds the remainder the use for which frame to go to
imgMC.gotoAndStop(gotoFrameV); //stops the movieclip at the frame specified
mouseLocV = _xmouse; // mouseLoc = the new x co-ordinates of the mouse
}
}

onMouseUp = function() { //when the mouse button is released
delete imgMC.onEnterFrame; //stop checking mouse position
}


I tried this just off the top of my head but its way off...probably will make things worse trying to work off it


stage.addEventListener(MouseEvent.MOUSE_DOWN, scrub);
stage.addEventListener(MouseEvent.MOUSE_UP, stopScrub);
video.addEventListener(Event.ENTER_FRAME, diff);

function scrub (event:MouseEvent):void {
var mouseLocation = mouseX;

function diff (e:Event):void {
var difference = mouseX - mouseLocation
difference = difference /10;
var goToFrame = (video.currentFrame + difference) % video.totalFrames;
video.gotoAndStop(goToFrame);
mouseLocation = mouseX
}
}

function stopScrub (event:MouseEvent):void {
delete video.ENTER_FRAME;
}

onMouseUp = function() {
delete imgMC.onEnterFrame;
}

neznein9
04-18-2009, 08:58 AM
I didn't compile this so there might be a couple errors, but this is roughly what the code should look like. I set it to use the entire video as a scrub surface but it would be a pretty easy change to use any other object (say a progress bar).

video.addEventListener(MouseEvent.MOUSE_DOWN, startScrubbing);
stage.addEventListener(MouseEvent.MOUSE_UP, stopScrubbing);

var scrubbing:Boolean = false;


function startScrubbing(e:MouseEvent):void{
if(scrubbing){ return };

scrubbing = true;
stage.addEventListener(MouseEvent.MOUSE_MOVE, liveScrub);
liveScrub(null);
}

function stopScrubbing(e:MouseEvent):void{
if(!scrubbing){ return };

scrubbing = false;
stage.removeEventListener(MouseEvent.MOUSE_MOVE, liveScrub);
}

function liveScrub(e:MouseEvent):void{
var mouseLocation:Number = stage.mouseX - video.x;
if(mouseLocation < 0) mouseLocation = 0;
if(mouseLocation > video.width) mouseLocation = video.width;

var mousePercentage:Number = mouseLocation / video.width;

video.playheadPercentage = mousePercentage;
}