Jump to content
GreenSock

speedfontain

ThrowProps in function

Recommended Posts

Hi Team,

 

Using the ThrowProps example, I need to place this into a function and call it when the MC has finsished loading its content.

 

However when i call the ThrowProps function it does not work, gets stuck and will not scroll.

 

How can i place this in a function to call when needed ?

 

Cheers

Link to comment
Share on other sites

Not quite sure how you've structured things exactly - can you post a very simple FLA with the only the essential code so that we can publish and see for ourselves?

Link to comment
Share on other sites

I have just wrapped the sample like this, and targerted the MC called licenceHolder

 

function makeScroll(){

 

var mc1:Sprite = licenceHolder;

 

var bounds:Rectangle = new Rectangle(0,230,510,1482);

 

var blitMask:BlitMask = new BlitMask(mc1,bounds.x,bounds.y,bounds.width,bounds.height,false);

 

var t1:uint,t2:uint,y1:Number,y2:Number,yOverlap:Number,yOffset:Number;

 

blitMask.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);

 

function mouseDownHandler(event:MouseEvent):void

{

TweenLite.killTweensOf(mc1);

y1 = y2 = mc1.y;

yOffset = this.mouseY - mc1.y;

yOverlap = Math.max(0,mc1.height - bounds.height);

t1 = t2 = getTimer();

mc1.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);

mc1.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

}

 

function mouseMoveHandler(event:MouseEvent):void

{

var y:Number = this.mouseY - yOffset;

//if mc1's position exceeds the bounds, make it drag only half as far with each mouse movement (like iPhone/iPad behavior)

if (y > bounds.top)

{

mc1.y = (y + bounds.top) * 0.5;

}

else if (y < bounds.top - yOverlap)

{

mc1.y = (y + bounds.top - yOverlap) * 0.5;

}

else

{

mc1.y = y;

}

blitMask.update();

var t:uint = getTimer();

//if the frame rate is too high, we won't be able to track the velocity as well, so only update the values 20 times per second

if (t - t2 > 50)

{

y2 = y1;

t2 = t1;

y1 = mc1.y;

t1 = t;

}

event.updateAfterEvent();

}

 

function mouseUpHandler(event:MouseEvent):void

{

mc1.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

mc1.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);

var time:Number = (getTimer() - t2) / 1000;

var yVelocity:Number = (mc1.y - y2) / time;

ThrowPropsPlugin.to(mc1, {throwProps:{y:{velocity:yVelocity, max:bounds.top, min:bounds.top - yOverlap, resistance:300}}, onUpdate:blitMask.update, ease:Strong.easeOut}, 10, 0.3, 1);

}

 

}

Link to comment
Share on other sites

It's tough to verify for sure without seeing your actual file and being able to publish, but it looks like the problem is that you're nesting functions and creating local variables that don't persist. Nested functions are generally frowned upon in ActionScript because technically they're supposed to get deleted as soon as the parent function finishes running. Same thing with the local variables that you're trying to reference inside those nested functions. I think this approach would be much cleaner:

 

var t1:uint, t2:uint, y1:Number, y2:Number, yOverlap:Number, yOffset:Number, mc1:Sprite, bounds:Rectangle, blitMask:BlitMask;

function makeScroll():void {
   mc1 = licenceHolder;
   bounds = new Rectangle(0,230,510,1482);
   blitMask = new BlitMask(mc1,bounds.x,bounds.y,bounds.width,bounds.height,false);
   blitMask.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
}

function mouseDownHandler(event:MouseEvent):void {
   TweenLite.killTweensOf(mc1);
   y1 = y2 = mc1.y;
   yOffset = this.mouseY - mc1.y;
   yOverlap = Math.max(0,mc1.height - bounds.height);
   t1 = t2 = getTimer();
   mc1.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
   mc1.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
}

function mouseMoveHandler(event:MouseEvent):void {
   var y:Number = this.mouseY - yOffset;
   //if mc1's position exceeds the bounds, make it drag only half as far with each mouse movement (like iPhone/iPad behavior)
   if (y > bounds.top)    {
       mc1.y = (y + bounds.top) * 0.5;
   } else if (y < bounds.top - yOverlap) {
       mc1.y = (y + bounds.top - yOverlap) * 0.5;
   } else {
       mc1.y = y;
   }
   blitMask.update();
   var t:uint = getTimer();
   //if the frame rate is too high, we won't be able to track the velocity as well, so only update the values 20 times per second
   if (t - t2 > 50) {
       y2 = y1;
       t2 = t1;
       y1 = mc1.y;
       t1 = t;
   }
   event.updateAfterEvent();
}

function mouseUpHandler(event:MouseEvent):void {
   mc1.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
   mc1.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
   var time:Number = (getTimer() - t2) / 1000;
   var yVelocity:Number = (mc1.y - y2) / time;
   ThrowPropsPlugin.to(mc1, {throwProps:{y:{velocity:yVelocity, max:bounds.top, min:bounds.top - yOverlap, resistance:300}}, onUpdate:blitMask.update, ease:Strong.easeOut}, 10, 0.3, 1);
}

Link to comment
Share on other sites

That worked a treat, many thanks.

 

Next question :-)

 

Within a timer, how can i move the content back to its top position after a period of time ?

 

Cheers

Link to comment
Share on other sites

If I understand your question correctly, that should be as simple as doing a tween with a delay set on it. Like:

 

TweenLite.to(mc1, 1, {y:0, delay:5});

 

No need for a timer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×