A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Switch case loop for tweens

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    69

    Switch case loop for tweens

    I've got the following code (what it should do: on keypress, loops through movieclips, scaling each one toward center stage). However, where it says "this" is making the program think I'm referring to the stage, so it's scaling that instead.

    1. How do I specify "whatever the current case/movieclip it's working on now"?

    2. How do I add sound to switch case transitions/tween? (meaning when each mc is tweened or transitioned, it also makes a sound)

    Peace

    // Create an array with specified elements.
    var scaleXto:Number = 4;
    var scaleYto:Number = 4;

    var arrSlides:Array = new Array(null, first_mc, second_mc, third_mc, fourth_mc);
    var tMgr:TransitionManager;
    var index:int = 0;
    var t:uint = 1;

    stage.addEventListener(KeyboardEvent.KEY_UP, myFunction);

    function myFunction(event:KeyboardEvent):void {
    index++;
    if (index == arrSlides.length) {
    index = 1;
    }
    tMgr = new TransitionManager(arrSlides[index]);

    switch (index) {
    case 1: {
    var tweenSX:Tween = new Tween(this,"scaleX",Strong.easeOut,this.scaleX,sca leXto,t,true);
    var tweenXY:Tween = new Tween(this,"scaleY",Strong.easeOut,this.scaleY,sca leYto,t,true);
    var tweenX:Tween = new Tween(this,"x",Strong.easeOut,this.x,((stage.stage Width - this.width)/2),t,true);
    var tweenY:Tween = new Tween(this,"y",Strong.easeOut,this.y,((stage.stage Height - this.height)/2),t,true);
    break;
    }

    case 2: {
    var tweenSX:Tween = new Tween(this[index],"scaleX",Strong.easeOut,this.scaleX,scaleXto,t,tr ue);
    var tweenXY:Tween = new Tween(this,"scaleY",Strong.easeOut,this.scaleY,sca leYto,t,true);
    var tweenX:Tween = new Tween(this,"x",Strong.easeOut,this.x,((stage.stage Width - this.width)/2),t,true);
    var tweenY:Tween = new Tween(this,"y",Strong.easeOut,this.y,((stage.stage Height - this.height)/2),t,true);
    break;
    }
    ...etc

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The answer to your first question is use event.target rather than this.

    The answer to your second is that you play a sound in that case. It's really unrelated to the tween stuff you're doing.

  3. #3
    Member
    Join Date
    Jul 2009
    Posts
    69
    I'm getting this error with event.target: Error #2071: The Stage class does not implement this property or method. I think it's because I'm using stage event / listener, rather than specific mc or btn - but not sure if that's the case.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Post the line throwing the error. You did something wrong, but I can't guess what exactly.

  5. #5
    Member
    Join Date
    Jul 2009
    Posts
    69
    The error was from these changes:

    var tweenSX:Tween = new Tween(event.target,"scaleX",Strong.easeOut,event.target.scaleX,scaleXto,t,true);
    var tweenXY:Tween = new Tween(event.target,"scaleY",Strong.easeOut,event.target.scaleY,scaleYto,t,true);
    var tweenX:Tween = new Tween(event.target,"x",Strong.easeOut,event.target.x,525,t,true);
    var tweenY:Tween = new Tween(event.target,"y",Strong.easeOut,event.target.y,400,t,true);
    Last edited by Leftyplayer; 01-13-2010 at 04:05 PM.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I misunderstood you. I thought you were activating each by some keyboard interaction where the one you wanted had focus. But your event.target is still the stage. What you wanted was to get the instance from arrSlides by index. That's just
    Code:
    arrSlides[index]
    So those tweens would be:
    Code:
    new Tween(arrSlides[index],"scaleX",Strong.easeOut,arrSlides[index].scaleX,scaleXto,t,true);
    new Tween(arrSlides[index],"scaleY",Strong.easeOut,arrSlides[index].scaleY,scaleYto,t,true);
    new Tween(arrSlides[index],"x",Strong.easeOut,arrSlides[index].x,525,t,true);
    new Tween(arrSlides[index],"y",Strong.easeOut,arrSlides[index].y,400,t,true);
    Note that you cannot redeclare the same variable within the function. You cannot have var tweenSx in each case. If you need to keep a variable around, you need to declare them once at the top of the function instead of in each case. But unless you are actually using those vars in the function, they will fall out of scope and be useless.

    If you were using a more advanced tween library, you could accomplish all 4 property tweens with a single statement per case.

  7. #7
    Member
    Join Date
    Jul 2009
    Posts
    69
    Ah, thank you, that helps!

    If you were using a more advanced tween library, you could accomplish all 4 property tweens with a single statement per case.
    Any recommendations?

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Why do you need the switch in the first place? If you do the same thing in each case, but to a different object, then just set a variable for the object and do the tweens with that variable. Or use arrSlides[index] as that variable.

    I always recommend TweenLite, because it is the one I happen to have the most experience with. But TweenMax, Go, or many many others would also work.

  9. #9
    Member
    Join Date
    Jul 2009
    Posts
    69
    I always recommend TweenLite, because it is the one I happen to have the most experience with. But TweenMax, Go, or many many others would also work.
    thanks, will check them out.

    and, yes, I may just do away with the switch altogether. i started out heading in a certain direction, but have ended up in a different place where now it doesn't make much sense to use it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center