Search code examples
flashactionscript-3actionscript

vertical scroll based on mouseY in ActionScript 3


I have to write a simple method, which allows me to move a movieclip inside another one, like in http://www.cballestimenti.it/ (I have to move it vertically, instead of horizontally).

I've written this simple code, but it seems to not work properly... can anyone help me? thanks a lot in advance!

public function factor():void
{
    thumbs.addEventListener(MouseEvent.MOUSE_MOVE, moveSlide);
    factor = ((content.height)/container.height);
}

private function moveSlide(e:MouseEvent):void
{
    if (container.mouseX>=0 && container.mouseX<=container.width && container.mouseY>=0 && container.mouseY<=container.height)
    {
        TweenMax.to(content, 0.3, { y:(-(container.mouseY)*factor) });
    }
}

Solution

  • Would be better if you tell us WHY or HOW it's "not work properly". And give us any errors that your are getting.

    I do see a few things wrong straight away. First, you are setting a public method called factor, then inside that method, you set factor to a number! So what is factor? Is it a function or a number?

    Secondly, I think you are having issues with scope. Since you do not explain the layout of your assets, I am assuming that you have three MovieClips, called container, content, and thumbs. But I have no idea if content is inside the container. And I can't see why you would have MovieClips called thumbs and content.

    I am going ignore the thumbs MC and just use content. I will assume the content MC is inside the container MC. I came up with this:

    var maxHeight:Number = 400;
    stage.addEventListener(MouseEvent.MOUSE_MOVE, moveSlide);
    
    function moveSlide(e:MouseEvent):void
    {
        var mouseFactor:Number = container.mouseY / maxHeight;
        var targetY:Number = (container.content.height - maxHeight) * -mouseFactor;
        container.content.y = targetY;
    }
    

    Try to get it working without all the tween stuff first. Then once you get your maths working, it's easy to add effects in.