Jump to content
Search Community

Decreasing lag in simple horizontal slide

LanLing test
Moderator Tag

Recommended Posts

Hi I was wondering if the amount of lag in TweenLite for a simple horizontal tween would be less than just using default actionscript code?

I'm trying to make a horizontal banner slide slowly across the screen, then when it gets to the last bit reset the position to the starting point and continue sliding:

 

private function slideShow():void {
  menuLeftSide.mainMenuSlideShow.addEventListener(Event.ENTER_FRAME,onEnterFrameSlide);

  function onEnterFrameSlide(e:Event):void {

   if (menuLeftSide.mainMenuSlideShow.x <= -1406) {  //when it slides to last bit of image
 menuLeftSide.mainMenuSlideShow.x = 1000; //bring it all the way to the right and slide again
   }

   menuLeftSide.mainMenuSlideShow.x -= 1; //slide direction is left
  }
 }

 

This works, however it is slightly laggy. Not extremely noticeable but I'd expect no lag for a super-simple animation like this.

Does TweenLite have something like this and is it more system-efficient and less laggy?

Link to comment
Share on other sites

I don't see anything horribly wrong with your code (although nested functions are generally frowned upon in ActionScript) and it should perform fine, BUT the down side is that your movement is tied to the frame rate. So if the fps drops, your animation will slow down too (take longer to complete). TweenLite and TweenMax are time-based, so they are much more consistent. If the frame rate drops, they just make up the difference on the next render. I doubt TweenLite or TweenMax would be any easier on the CPU than your existing code, but they would be more reliable in terms of the overall speed of the object and completion time.

 

I hope that helps.

 

Just out of curiosity, have you tried it with TweenLite? Did it perform better for you?

Link to comment
Share on other sites

I'm not sure how to add the x position checker with TweenLite, so that its x position resets to 1000 when x is <-1406.

 

private function slideShow():void {
  TweenLite.to(menuLeftSide.mainMenuSlideShow,10,{x:-1400});
 }

Link to comment
Share on other sites

Yeah I get the logic but I'm not exactly sure about the code... like what is the syntax for the onComplete?

This is what I have but I don't think is right:

TweenLite.to(menuLeftSide.mainMenuSlideShow,5,{x:-1400,
onComplete:menuLeftSide.mainMenuSlideshow.x,
onCompleteParams:[1000]});

Link to comment
Share on other sites

Nope, all you need to do is restart the tween (assuming you start at 1000 and that's where you want it to loop back to)...

 

var tween:TweenLite = TweenLite.to(menuLeftSide.mainMenuSlideShow, 5, {x:-1400, onComplete:loop});
function loop():void {
tween.restart();
}

Link to comment
Share on other sites

Yes, the default ease is a Quad.easeOut because that's a more natural motion (for most animations) but you can get a linear movement by adding ease:Linear.easeNone to your vars parameter:

 


var tween:TweenLite = TweenLite.to(menuLeftSide.mainMenuSlideShow, 5, {x:-1400, ease:Linear.easeNone, onComplete:loop});
function loop():void {
tween.restart();
}

 

And of course don't forget to import the com.greensock.easing.Linear class.

Link to comment
Share on other sites

Yep, you're correct but one thing you could try (and I don't know if this would improve performance or not, but I think it's worth a shot) is to wrap your scrolling object in a Sprite. Inside that Sprite, you apply a BlitMask that's only as big as the screen (or the area you need visible). Then apply your PNG mask to that container Sprite. That way, Flash doesn't need to keep recalculating and rendering the pixels that fall outside the mask. You're essentially clipping the area that you need masked.

 

Beware that scrolling large objects, ESPECIALLY with a mask is very processor-intensive. That has nothing to do with the tweening engine of course - it's just a graphics rendering issue in Flash.

Link to comment
Share on other sites

Thanks for suggestion, I'll just use the TweenLite method, I think I got too much stuff going on nothing's gonna save it from lag.

Hmm how do I stop this animation when I go to another page? If I use the old AS method, I can use removeeventlistener when I go to another page:

 

menuLeftSide.mainMenuSlideShow.removeEventListener(Event.ENTER_FRAME,onEnterFrameSlide); //stop sliding picture
delegate.beginStory();

Link to comment
Share on other sites

Or you can kill all the tweens of a particular object like this:

 

TweenLite.killTweensOf(myObject);

 

Which can be slightly more user-friendly because you don't need to keep a reference of the tween on-hand. Carl's pause() recommendation, however, would allow you to resume() that same tween anytime. That can be handy, but if you're just looking to kill the tween completely and aren't worried about using it later, use kill() or killTweensOf() like above.

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.
×
×
  • Create New...