views:

329

answers:

3

My animation is 100 frames total. I want to set a range of frames. Is is there a command to play only frames 50 to 75? I want begin-and-end frame to be in an object I can call on later. There must be a "best" way of doing this!

100 frame total <==play 50 to 75==>

SAMPLE

    These commands work, but I would end up adding labels and stop();

//start playing at given frame
Play at 9950.00
gotoAndPlay(4990,"Scene 1");

//advance and stop at given frame
my_mc.gotoAndStop(my_mc.currentFrame + 10);

//control frame rate on stage
Frame rate command
stage.frameRate = 90;

USE

The begin-and-end of my movie should be an object in it's self. My movie will need to control time and duration for a given number of frames. Comment if you have any ideas.

TAGS

AS 3.0, timer class, display, time-scaling

Patricks example modified "I play with the values, but no effect or error"

  import flash.display.MovieClip; 
  import fl.transitions.Tween; 
  import fl.transitions.easing.*; 

    //... 

    function playFromTo(from:int, to:int, duration:Number):Tween { 

      return new Tween(this, "tweenFrame", None.easeNone, from, to, duration, true); 
    } 

    //instance of tweenFrame on stage
    var _tweenFrame:Number; 
    function set tweenFrame(value:Number):void{ 

    //what about value?
      _tweenFrame = value; 
      gotoAndStop(int(value)); 
    } 
    function get tweenFrame():Number{ 
      return _tweenFrame; 
    } 
A: 

general idea, not out of the box solution :]

var _isPlaying:Boolean=false;
    _timelineMovement:Boolean=false;
    _stopMovementOn:Number;

addEventListener(Event.ENTER_FRAME, playTimeline);

function playTimeline(e:Event):void{
  if (currentFrame == _stopMovementOn) { _isPlaying = false; }
  if (!_isPlaying) { return false; }
  gotoAndStop (currentFrame + _timelineMovement);
}

function playPart(from,to:Number):void{
  if (_isPlaying) { doSomething(); }
  if (to > from) { _timelineMovement= 1; } else { _timelineMovement = -1; }
  _stopMovementOn = to;
  _isPlaying = true;
}

OnEnterFrame is called either on enterframe or, if movie is stopped on every tick of framerate (this makes this scalable). playPart sets imaginary Play() and on every tick we control if there is movement we should do or not. Pretty straightforward :)

Adam Kiss
Thanks. I get a syntax error on line 2 Boolean=false;. Is there something I'm suppose differently. -VideoDnd
VideoDnd
Has anyone tested this?
VideoDnd
Nope, said it was general idea... but as I look on the code, it seems that in as3 you can't define thre variable via one var, so you have to put var in front of every variable definition (first three lines)
Adam Kiss
A: 

Use the undocumented addFrameScript() method, to add code to a frame. And example function could be something like this:

private function gotoAndPlayRange(target:MovieClip, startFrame:uint, endFrame:uint):void 
{
    target.addFrameScript(endFrame - 1, function():void 
    {
        target.stop();
        target.addFrameScript(endFrame - 1, null, false, false);
    });
    target.gotoAndPlay(startFrame);
}

The first parameter of addFrameScript is the frame number, but it is zero based. That is the reason for the -1.

TandemAdam
Thanks, I'll check it out.
VideoDnd
Has anyone got this to work
VideoDnd
yip, works perfectly fine! Just make a test case for it. It's what the cool kids do, and it's the best way to make sure something is working. Make a new FLA (you don't even need to save it). put the function on the root timeline (without the "private"). Create a movieclip with and animation on it. Also put it on the root timeline. Give it an instance name (e.g. "foo_mc"). Then below the function, just call it. gotoAndPlayRange(foo_mc, 10, 40);
TandemAdam
+1  A: 

If you want to control time, acceleration, etc... You can use a tween to advance the play head, add a getter and setter that will change the play head.

For this example i use the flash tweening but you can use whatever library you want.

package  {
  import flash.display.MovieClip;
  import fl.transitions.Tween;
  import fl.transitions.easing.*;

  public class MyMC extends MovieClip { 
    //...

    public function playFromTo(from:int, to:int, duration:Number):Tween {
      // add a tween with no acceleration and a duration in second
      return new Tween(this, "tweenFrame", None.easeNone, from, to, duration, true);
    }

    private var _tweenFrame:Number;
    public function set tweenFrame(value:Number):void{
      _tweenFrame = value;
      gotoAndStop(int(value));
    }
    public function get tweenFrame():Number{
      return _tweenFrame;
    }
  }
}

or using composition if you cant modify your MovieClip:

package  {
  import flash.display.MovieClip;
  import fl.transitions.Tween;
  import fl.transitions.easing.*;

  public class TweenableMC {
    private var _target:MovieClip;

    public function TweenableMC(target:MovieClip):void {
      _target=target;
    }

    public function playFromTo(from:int, to:int, duration:Number):Tween {
      // add a tween with no acceleration and a duration in second
      return new Tween(_target, "tweenFrame", None.easeNone, from, to, duration, true);
    }

    private var _tweenFrame:Number;
    public function set tweenFrame(value:Number):void{
      _tweenFrame = value;
      gotoAndStop(int(value));
    }
    public function get tweenFrame():Number{
      return _tweenFrame;
    }
  }
}

//...
var tm:TweenableMC=new TweenableMC(myMc);
tm.playFromTo(50, 75, 1); // play from 50 to 75 in 1 second
Patrick
See if what I'm doing makes sense
VideoDnd