1

我只需要正确的语法......我不断收到错误。

当前代码如下所示:

for (var i = movieClip.currentFrame; i<15; i++){

   movieClip.currentFrame+1

}
4

3 回答 3

1

What are you trying to achieve? If you want to play from a specified frame to another frame, this is not the way to go. That said, I don't undstand the current answers that are given. MovieClip.currentFrame is a read-only property. So none of the answers would actually work, none tested their code. I downvoted the answers for that. This is why you get errors too.

myMc.currentFrame = 5; 
// gives an error, since this property is read-only, you cannot set it.

To jump to a certain frame, use gotoAndStop() or gotoAndPlay(), with the framenumber or framelabel as the parameter.

If you want to play a clip called myMc from the current frame to (lets say) frame 15, try this

myMc.addEventListener(Event.ENTER_FRAME, onEnterFrame);

function onEnterFrame(event:Event):void
{
 if (myMc.currentFrame < 15)
 {
   myMc.gotoAndStop(myMc.currentFrame + 1);
 }
}

As you can see we've created an enterframe listener. If you dont understand what it does; this makes the onEnterFrame function (defined below) execute every frame. Its needed because every frame it have to decide where to go, its not possible with a loop. It should be altered over time. This example works, but only if when you should play forward, or to be more specific; when currentFrame is under 15. If your at fram 20, your stuck. Lets fix that by make it play backwards too, if needed.

myMc.addEventListener(Event.ENTER_FRAME, onEnterFrame);

function onEnterFrame(event:Event):void
{
 if (myMc.currentFrame < 15)
 {
   myMc.gotoAndStop(myMc.currentFrame + 1);
 }
 else if (myMc.currentFrame > 15)
 {
   myMc.gotoAndStop(myMc.currentFrame - 1);
 }
}

ok, now it should also play backwards. Now, it would be betterr to stop checking when we've arrived at the target frame. Therefor we should remove the eventlistener.

myMc.addEventListener(Event.ENTER_FRAME, onEnterFrame);

function onEnterFrame(event:Event):void
{
 if (myMc.currentFrame < 15)
 {
   myMc.gotoAndStop(myMc.currentFrame + 1);
 }
 else if (myMc.currentFrame > 15)
 {
   myMc.gotoAndStop(myMc.currentFrame - 1);
 }
 if (myMc.currentFrame == 15)
 {
   myMc.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
 }
}

Now this should be the answer of your question. However it would be nice to make a function where you can give a target movieclip and endFrame.

function playClipTo(clip:MovieClip, targetFrame:int):void
{
  clip.targetFrame = targetFrame;
  // since MovieClip is dynamic, you can add non-excisting properties. 
  // If your using classes, you would extend MovieClip and create a public variable for this. But for now its fine.
  clip.addEventListener(Event.ENTER_FRAME, onClipEnterFrame);
}

function onClipEnterFrame(event:Event):void
{
 var clip:MovieClip = MovieClip(event.target); // this referres to any clip with a listener
 if (clip.targetFrame)
 {
  if (clip.currentFrame < clip.targetFrame)
  {
   clip.gotoAndStop(clip.currentFrame + 1);
  }
  else if (clip.currentFrame > clip.targetFrame)
  {
   clip.gotoAndStop(clip.currentFrame - 1);
  }
  if (clip.currentFrame == clip.targetFrame)
  {
   clip.removeEventListener(Event.ENTER_FRAME, onClipEnterFrame);
   delete clip.targetFrame;
  }
 }
 else
 {
   clip.removeEventListener(Event.ENTER_FRAME, onClipEnterFrame);
 }
}

With this function you could do this

playClipTo(myMc, 15); // ha, familiar example!
playClipTo(myMc2, 5); // another clip, play to frame 5

I hope this helps. By the way, if you need to jump from framelabel to framelabel, or need more control on speed, easing, delays, you could take a look at a tween engine, like TweenLite, this has very advanced but easy to understand way of animating with code. Its very common in the flash world, plus it saves development time for relative simple tasks like this.

于 2012-09-30T21:30:07.323 回答
-1

在循环里面放

if (movieClip.currentFrame < theNumberToIncreaseTo) {
     movieClip.currentFrame = movieClip.currentFrame + 1;
}
于 2012-09-30T17:41:45.407 回答
-2

也许

movieClip.currentFrame = movieClip.currentFrame + 1

代替

movieClip.currentFrame+1
于 2012-09-30T17:40:53.927 回答