Adding stop(); to a movieclip on the timeline is causing the tweens in said movieclip to not play, the movieclip just skips to the end - actionscript-3

I've got a movieclip that is composed of 3 separate symbols. 2 of the symbols have their alpha tweened over 60 frames. 1 of the symbols is not tweened at all. All symbols are in separate layers, and there is a 4th, empty layer with a keyframe on frame 60 for actionscript.
The actionscript on frame 60 is simply "stop();" .
I am adding an instance of the movieclip to the stage dynamically from the document class. When I have "stop();" in there, the movieclip appears on the stage and skips straight to frame 60, where it succesfully stops.
Without "stop();" in there, the movieclip plays the alpha tweens perfectly, but obviously continuously loops.
Manually dispatching an Event.COMPLETE and listening for it does not work either and I would prefer not doing it that way anyway.
Here is the code that adds the movieclip to the stage:
//initialize the gameplay, remove title screen.
private function initialize_gameplay():void
{
//remove the title screen
initialize_title_screen(true);
this.screen_transition_obj = new tide_out_video();
this.addChild(this.screen_transition_obj);
this.game_board = new tidepool_gameboard();
this.screen_transition_obj.addEventListener(Event.COMPLETE,swap_transition_video_for_screen);
}
//replace the current transition video with the screen it was transitioning to
private function swap_transition_video_for_screen(e:Event){
this.addChild(this.game_board);
if(this.screen_transition_obj != null){
if(this.getChildByName(this.screen_transition_obj.name)){
this.removeChild(this.screen_transition_obj);
}
this.screen_transition_obj.removeEventListener(Event.COMPLETE, swap_transition_video_for_screen);
this.screen_transition_obj = null;
}
}
The movieclip's class is tidepool_gameboard and the property of the document class that stores the reference to it is game_board.
Any idea why putting stop(); on frame 60 of the movie clip is causing it to skip to the end without tweening?
UPDATE:
Adding the movieclip to the stage instantly instead of as the result of an event listener works properly, the problem only occurs when the movieclip is added in the event listner.

I can't believe I overlooked this, as it appears to be fairly obvious to me now.
In the code posted in the question, I've initialized a new instance of this.game_board, then added it to the stage after a delay based on an event listener for a video clip. The animation is playing, but it's playing before the clip ever is added to the stage.
Thanks go to alecmce who answered this question.
I did his Event.ADDED_TO_STAGE event listener, and it worked, which led me to realize that the MovieClip does not wait until it is added to the stage to start playing its own timeline, it simply starts the second you instantiate the object.
This is the new, fully functional code:
//initialize the gameplay, remove title screen.
private function initialize_gameplay():void
{
//remove the title screen
initialize_title_screen(true);
this.screen_transition_obj = new tide_out_video();
this.addChild(this.screen_transition_obj);
this.screen_transition_obj.addEventListener(Event.COMPLETE,swap_transition_video_for_screen);
}
//replace the current transition video with the screen it was transitioning to
private function swap_transition_video_for_screen(e:Event)
{
this.game_board = new tidepool_gameboard();
this.addChild(this.game_board);
if (this.screen_transition_obj != null)
{
if (this.getChildByName(this.screen_transition_obj.name))
{
this.removeChild(this.screen_transition_obj);
}
this.screen_transition_obj.removeEventListener(Event.COMPLETE, swap_transition_video_for_screen);
this.screen_transition_obj = null;
}
}

Related

How to play animation and then play reverse on hover, start playing again until end on hover out using in Adobe Animate (Flash)?

Sorry this is so specific but I have combed through so many pages and videos and tutorials and can't figure this out.
I have all of my animations within a MovieClip. In the movie clip is also a stage sized white square button with the instance name "btn". Back on the main stage I have a second layer called "actions" with the following code applied to the first (and only) frame. It's not working. At all. (HUGE) tia
stop(); // this will stop the movie from playing at the start
btn.addEventListener((MouseEvent.ROLL_OVER, playMovie);
btn.addEventListener((MouseEvent.ROLL_OUT, stopMovie);
function playMovie(evt:MouseEvent):void {
play();
}
function stopMovie(evt:MouseEvent):void {
stop();
}
The problem is when you say play(); or stop(); which object are you really commanding? Your playMovie function could be in theory used to control many MovieClips at once, in different ways, so be specific with your commands...
btn.play(); //start the playback of "btn" MC
btn.stop(); //stop the playback of "btn" MC
Also consider using MOUSE_OVER/OUT instead ROLL_OVER/OUT etc but whatever works for you.
For reversing you will use btn.prevFrame(); together with an ENTER_FRAME event function. This function reads your Document settings for the FPS. For example, if you set 30 frames-per-sec then whatever instructions you put inside the event function will be processed 30 times per second.
See this other Answer for advice about reversing the playback of a MovieClip.
#VC.One is correct in how you should implement a solve to your issue, however in response to your comment on their answer, I thought I would demonstrate how to implement this fully for you - incase they don't.
var removeUpdate = false;
btn.addEventListener(MouseEvent.MOUSE_OVER, playMovie);
btn.addEventListener(MouseEvent.MOUSE_OUT, stopMovie);
function playMovie(evt:MouseEvent):void {
// Stop rewinding the movie clip and play it
if(removeUpdate){
stage.removeEventListener(Event.ENTER_FRAME, update);
removeUpdate = false;
}
// play our button
btn.play();
}
function stopMovie(evt:MouseEvent):void {
// stop our button
btn.stop();
// ... and rewind it
stage.addEventListener(Event.ENTER_FRAME, update);
removeUpdate = true;
}
function update(evt: Event){
// moves the button movie clip backwards one frame.
btn.prevFrame();
// If we have finished rewinding the movie clip, then stop
if(btn.currentFrame == 1){
stage.removeEventListener(Event.ENTER_FRAME, update);
removeUpdate = false;
}
}
It is important that you remove the update event because if you don't, the movie will never play again, because it will go one frame forward and then back again every frame due to; btn.play(); btn.prevFrame();

setchildindex is creating problems

I have made a simple drag and match game for kids.
I used setchildindex for movie clips to be dragged but when I click next button and go to another frame but movie clips are remaining in the same stage. What should i do?
Here is my code I used: (drag_1, this.numChildren0);.
When I reload it's not working.
drag_1.buttonMode = true;
drag_1.addEventListener(MouseEvent.MOUSE_UP, dropMe_1);
drag_1.addEventListener(MouseEvent.MOUSE_DOWN, dragMe_1);
var back_1X:Number = back_1.x;
var back_1Y:Number = back_1.y;
var hit_2X:Number = hit_2.x;
var hit_2Y:Number = hit_2.y;
function dragMe_1(event:MouseEvent)
{
drag_1.startDrag()
setChildIndex(drag_1, this.numChildren-1);
}
function dropMe_1(event:MouseEvent)
{
drag_1.stopDrag();
if(drag_1.hitTestObject(drop_2))
{
TweenMax.to(drag_1, 0.5, {
x:hit_2X,
y:hit_2Y,
ease:Cubic.easeOut
});
drag_1.mouseEnabled = false;
SoundMixer.stopAll();
}
else
{
TweenMax.to(drag_1, 0.5,
{
x:back_1X,
y:back_1Y,
ease:Bounce.easeOut
});
}
}
You need to remove the MovieClips using removeChild().
Now, why do you need to do that here? Well, this is one of those odd problems you get when you mix the timeline with code. When you place a symbol on the timeline keyframe, the Flash Player will instantiate that symbol when it reaches that frame. After that, any frame on the timeline that updates the symbol (tweens, effects, etc) will do just that, and any frame that lacks the symbol will remove it. However, the Flash Player is very picky about identifying that symbol on each frame of the timeline. When you move it using setChildIndex you are basically breaking the timeline link, and the Flash Player no longer identifies it and removes it based on the keyframes. You'll also find that if you revisit a keyframe that had that symbol, the Flash Player will instantiate a second one regardless if the one you moved is still there. As you can see, it can get pretty messy.

Stop(); in nested movie clip doesn't work AS3

I have a movie clip on my timeline, which I then move on main timeline using classic tween from one side to another. I do not want that mc to loop so once the animation inside it finishes it should stop and all frames should be visible until the tween on main timeline finishes. To stop it from looping I added new keframe with stop(); at the end inside the movie clip. But it doesn't work, the movie clip keeps looping. I have changed the property type on first frame in the main timeline from movieclip to Graphic so that I can preview the movie clip in the timeline. In previous versions of Flash it worked always fine, but in CC the stop(); is ignored.
I know AS won't work with Graphics, but as far as the AS is inside that Graphic that shouldn't matter. Could anyone explain it to me and provide some solution please?
Your animation is happening on the root timeline so you need to put your stop(); on the last keyframe on your main time line. Or create the animation inside your movieclip and put the stop(); on the last keyframe in there instead and put the movieclip on the stage.
Try this code it works 100%
Paste the below code your main time line and called MovieClip_name.stopAllClips();
MovieClip.prototype.stopAllClips = function():void {
var mc:MovieClip = this;
var n:int = mc.numChildren;
mc.stop();
for (var i:int=0; i<n; i++) {
var clip:MovieClip = mc.getChildAt(i) as MovieClip;
if (clip) {
clip.stop();
clip.stopAllClips();
}
} }

MouseEvent not dispatching on movie clip

I know this must be easy but somehow I have not been able to figure it out after spending more than hours. I have two movie clips in a class that extends Sprite. When I add event listener to the stage every thing works fine. But when I try to add event listener to one of the movie clips, the event never dispatches it seems. Here is how it looks like -
public class MyClass extends Sprite
{
private var movieclip1:MovieClip, movieclip2:MovieClip;
private function init(e:Event == null):void
{
movieclip1 = new MovieClip();
movieclip2 = new MovieClip();
//works fine, dispatches event
stage.addEventListener(MouseEvent.MOUSE_DOWN. mousedown);
//not working
movieclip1.addEventListener(MouseEvent.MOUSE_DOWN, mousedown);
addChild(movieclip1);
addChild(movieclip2);
}
}
Actually I want both the movie clips to work mutually exclusively, i.e., a mouse event on one movie clip should not interfere with that of the other. Any pointers?
Empty MovieClips and Sprites CANNOT be sized, they will be 0,0 and then they will not be able to dispatch MouseEvents.
You can resize a MovieClip when it has some content.
The Sprite will be of the size of the rectangle that encloses the 2 MovieClip.
If the MovieClips are empty = 0,0 the Sprite will be 0,0
About the events between the 2 MovieClips:
They are not going to interfere because the events when are bubbling goes upwards.
So the listeners put on MC1 will listen ONLY onClick on MC1 and the same does MC2
You can trace the width and height of the movieclip1 or movieclip2, you will get 0 both.
So, you can't click them.
You can use graphcis to draw a shape in these two mcs, and try again.

AS3 Getting to Control Stage

I am building a Flash Game, I am doing this:
MainStage, with 11 frames
Mainstage Loads > Lobby.swf (1 frame)
Lobby Loads > Arrows.swf (1 frame)
I just want to control the main Stage from Arrows.swf , get the current frame, right arrow mainStage.current frame + 1, left arrow mainStage.current frame -1
I have tried the parent/child method
this.parent.parent
parent.parent
this.root
parent.root
every combination, I am also tracing and when I finally hit the mainStage it is saying cannot convert Stage to Display Object which I understand.
but how can I just get current frame of the "mainstage" needle and control it with gotoAndPlay or other such methods ?
I have solved it.
I didn't control the main stage what I did was, I reduced the main stage layer in the lobby layer when I load the left and right arrow I attach an event listener for a custom event.
In the arrows movie I have written a function to dispatch the event here are the lines of code I used
in the "main" stage of the movie you want to control:
//ball loader prep and load
var ballLoader:Loader = new Loader();
var ballRequest:URLRequest = new URLRequest("lobbyBalls.swf");
ballLoader.load(ballRequest);
ballLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, ballsLoaded);
function ballsLoaded(event:Event):void{
trace("ball Loader complete");
ballLoader.x = 50;
ballLoader.y = 200;
stage.addChild(ballLoader);
ballLoader.content.addEventListener("Previous", ctrlPrevFrame);
ballLoader.content.addEventListener("Next", ctrlNextFrame);
}
In the movie from which you want to control
import flash.events.Event;
leftArrow.addEventListener(MouseEvent.CLICK, prevEvent);
rightArrow.addEventListener(MouseEvent.CLICK, nextEvent);
function prevEvent(event:Event):void{
trace("prev");
dispatchEvent(new Event("Previous", true));
}
function nextEvent(event:Event):void{
trace("next");
dispatchEvent(new Event("Next", true));
}
Thanks anyways....