how can i add a movie clip on a specific frame using addChild called in a class with AS3 ,
my problem is how access to the specific frame
thank you
The tricky thing about this would be to handle the frame 'live cycle'...
The way I would do it, is:
Add an event listener for FRAME_CONSTRUCTED
Send your movieclip and stop it at your desired frame.
Attach your symbol on the handler of the event.
Something like this:
mc.addEventListener(Event.FRAME_CONSTRUCTED, _addChild );
mc.gotoAndStop(2);
function _addChild(e:Event):void{
mc.addChild( new Square) //Library symbol
}
Related
I have a map with different countries. When i hover over one of the countries, i want it to play a certain frame of a movieclip. I've made a movieclip for each country, which has 2 frames. The 1. frame is just the picture of the country like it appears on the map. Frame 2 is the country with the name over it. The second frame contains a stop(); Here is my code:
rømskog.addEventListener(MouseEvent.MOUSE_OVER,mover);
halden.addEventListener(MouseEvent.MOUSE_OVER,mover);
askim.addEventListener(MouseEvent.MOUSE_OVER,mover);
rømskog.addEventListener(MouseEvent.MOUSE_OUT,outer);
halden.addEventListener(MouseEvent.MOUSE_OUT,outer);
askim.addEventListener(MouseEvent.MOUSE_OUT,outer);
function mover(e:MouseEvent):void {
gotoAndPlay(2);
}
function outer(e:MouseEvent):void {
gotoAndPlay(1);
}
I know this code is incorrect, but i'm struggling to solve the problem. My movieclip starts automatic also, as the name of the country is visiable from the start. If somebody could learn me in the easiest way possible i would be very happy!
Your current code will do gotoAndPlay on the main timeline. You need to make the country movie clips timeline go to the appropriate frame instead.
All you should need to do is modify your two function like this:
function mover(e:MouseEvent):void {
//e.currentTarget is a reference to the object that was clicked. To avoid a compiler warning, we tell flash it's a movie clip.
MovieClip(e.currentTarget).gotoAndStop(2); //if you don't want to actually play the timeline, use gotoAndStop
}
function outer(e:MouseEvent):void {
//since this code is in the main timeline, doing gotoAndPlay will apply to it, not the object clicked
//so like above, we tell the current target of the event to gotoAndPlay
MovieClip(e.currentTarget).gotoAndStop(1);
}
Okay so I have a movieclip called a_mc, if you click the movieclip, it goes to frame 5, and then on frame 5 there is a button called close_btn where if you click the button, it goes back to frame 1 and it is supposed to make a_mc invisible. Here is the actionscript code for frame 1.
stop();
a_mc.addEventListener(MouseClick.CLICK, aClicked);
function aClicked(event:MouseEvent):void {
gotoAndStop(5);
}
and on frame 5, the actionscript code is
stop();
close_btn.addEventListener(MouseEvent.CLICK, closeCLicked);
function closeClicked(event:MouseEvent):void {
gotoAndStop(1);
a_mc.visible = false;
a_mc.removeEventListener(MouseEvent.CLICK, aClicked);
}
see, the problem is, in frame 5, I make a_mc invisible and remove the event listener and go back to frame 1 and on frame one, it always executes the actionscript code so it again creates the event listener and makes a_mc visible. Any idea on how to stop this from happening?
I tried putting the code from frame 1 into a package and then a class and then a constructer method but it is saying
"Syntax error: package is unexpected"
Could you put all the code that you want to execute once in frame 1? - don't call stop() and let it run to the next frame.
Then put the rest of your code in other key frames and don't use gotoAndStop(1) so frame 1 is only called once?
You can try not removing the event listener on a_mc in frame 5, and then in frame 1 check if the event listener is already present (a_mc.hasEventListener()) as a signal that frame 1 has already been shown. Not exactly a 'bets practices' solution, but it might work.
Unfortunately, depending on the actual conents of those clip, and what happens in other frames, it may be the problem you're having is a consequence of the way movieclip object works in flash. When a frame is changed, flash instantiates new objects on the stage (added in new frame), and removes the ones not needed anymore (depending on the contents, but generally it's true). The 'a_mc' object that you manipulate in frame 5 may not be the same 'a_mc' object that is on the stage when you go back to frame 1. It may have been deleted and reinstantiated in the meantime.
To avoid things like that, it would be a better solution to have controlling code in a class outside of the timeline of the animating clip, or at least to keep the state in a separate object. I work in Flash Builder so I can't help you with the details of such organization in Flash Pro (which I presume you're using), but you could probably have all code on the frame 1 of the main clip, and then put the other movieclips with buttons and stuff as children of the main clip. That way main clip can control the state, and know what to show when.
I have a movie with several movieClips. One of the movieClips has a timeline with a motion tween. I created an instance of this movieClip:
var board:Board = new Board();
and used board.gotoAndStop(1); to keep the movieClip from playing at the beginning of the movie.
An eventListener checks to see when board collides with something; when it does, a function is invoked that uses board.gotoAndPlay(2) to get the board's timeline going.
I want the playhead on that timeline to stop at the end of the action, rather than looping. I tried to put a stop() on the last frame of that timeline, but Flash tells me I can't put an action on an object.
Can you help me solve this?
Thank you very, very much!
If memory serves, you need to make sure you have clicked in the frame in the timeline, so the frame is selected, and not an object on the stage. Then enter your actionscript.
Make sure you select a single frame where you would like to add actions then add your code
stop();
To make the timeline stop looping
I have a Player movieclip object being placed on frames, where an example player is, then making the example player invisible. But when it runs the player is placed where the example is in the first frame, instead of in each current frame.
parent class:
playerStartX = exPlayer.x;
playerStartY = exPlayer.y;
Is there a way to either look only at the example in the current frame or else to remove only the example in the current frame after it has already been "copied"?
Not sure, if I understood your question completely, but maybe you should use an event handler for the Event.ENTER_FRAME event. This event is dispatched in sync with the frame rate of your SWF. Your handler could look like this:
function enterFrameHandler(event:Event):void
{
playerStartX = exPlayer.x;
playerStartY = exPlayer.y;
}
Because I don't no enough about how you structured/organized your MovieClips on the stage, I cannot say where to add the handler. But somewhere you have to do:
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
I want my movieclip to play once and stop on the last frame. I use the following code in the loop of my movieclip class. (this is as3)
if(currentFrame == 120)
stop();
120 is the last frame. It plays once. but the problem is it goes back to frame 1 again. is there a better method of stopping a movieclip on a particular frame.
If you want to do it with actionscript and not add a stop() to the last frame on the timeline manually, then you can use the undocumented addFrameScript() method.
mc.addFrameScript(mc.totalFrames - 1, function():void
{
mc.stop();
});
Can't remember the scope of the function, but you can either use mc.stop(), or just stop().
*EDIT - Added -1 to the first parameter of addFrameScript, because it is zero based (so to put a frameScript on frame 10 you would put 9).
On the timeline, you can just put a keyframe at the end, and add the stop(); method there. When it reaches that frame, the stop(); method will execute, and the clip will stop.
Assuming you are using the timeline that is, but sounds like you are.
Easiest way:
Just select the last Frame in the timeline. Open the Actions pane(F8). Ensure the frame u selected is still selected, if not then select again while the Actions pane is open.
After selecting, just add the simple stop() function in the the 'Action-Frame' pane.
//add the following line to the actions pane of the last frame.
stop();
You're done. You can optionally add a replay button if needed.
This seems more along the lines of what you're looking for:
addEventListener(Event.ENTER_FRAME, function(e:Event){
if(currentFrame == totalFrames){
stop();
removeEventListener(event.type, arguments.callee);
}
});
I added an event listener (for Actionscript 3) so on every frame this function will fire and check what frame it is on. If it's the last frame it will stop. Based on your example I'm presuming you have a class that's extending MovieClip.