Jumping back and forth inside a Movieclip with a button? - actionscript-3

So, I'm very much a beginner in AS3. I've been reading and figuring out things as I go, though I can't wrap my head around this.
So, I have 4 frames. Each frame has a different movie clip, MC1,MC2,MC3,MC4
Inside those four movie clips, there is another movie clip with the same instance name for each: BC, and inside that movie clip there are two frames. Frame 1 has a dot, and frame 2 does not.
MC1>BC>(2 frames)
MC2>BC>(2 frames)
and so on....
What I'm trying to do: I wanted to see if there was any way to control the frame navigation of BC inside all four MC clips at the same time with one button.
I want to switch back and fourth between the two frames inside the BC movie clip.
I'm at a loss, I've tried quite a few things.

You should be able to do so by giving them all the same instance name (so long as there is only ever one of them on screen at once).
So lets say you have a button that spans all 4 frames with an instance name of navBtn and you gave each of the MC1-4 clips the same instance name of MC. You could do the following on frame 1:
navBtn.addEventListener(MouseEvent.CLICK, navBtnClick);
function navBtnClick(e:Event):void {
if(MC.BC.currentFrame == 2){
MC.BC.gotoAndStop(1);
}else{
MC.BC.gotoAndStop(2);
}
}
Reading your question again, perhaps what are looking for is to have each clip automatically go to the same frame for their BC child when they load? If that is the case, then follow the example in the comment on your question by #Organis. Here is one way you could accomplish this:
Create two variable on frame one of your main timeline:
var BC_NAV_CHANGE:String = "BC_NAV_CHANGE";
var BC_CurFrame:int = 1;
Then, when you need to change the frame of the BC objects, do the following:
//create a function that you call when you want to change the BC frame
function toggleBCFrame(e:Event = null){
MovieClip(root).BC_CurFrame = MovieClip(root).BC_CurFrame == 1 ? 2 : 1;
//the line above is a if/else shorthand, that is setting a new value to the `BC_CurFrame` var,
//if the current value is `1`, it will set it to `2`, otherwise it will set it to `1`
MovieClip(root).dispatchEvent(new Event(MovieClip(root).BC_NAV_CHANGE));
//this line (above) dispatches a event telling anything that's listening that the variable has changed
}
If the code above is on the main timeline, you can forgo all the MovieClip(root). parts of the code.
Now, on the timeline of your BC MovieClip(s), put the following code:
//create a function that goes to and stops at the frame stored in the global variable
function updateFrame(e:Event = null){
gotoAndStop(MovieClip(root).BC_CurFrame);
}
//next listen for the BC_NAV_CHANGE event, and call the above update function above any time that event happens
MovieClip(root).addEventListener(MovieClip(root).BC_NAV_CHANGE, updateFrame);
//lastly, call the update function right away so when the BC clips loads it immediately goes to the correct frame
updateFrame();

Related

Trigger a function when specific frame number is reached - AS3

I wanted friends, do the following when my MC get a determanido internal quandro, it triggers a function to another MC. for example when the ball hit the wall, a person goes to search - la, I have tried using :
("root") {root.MC.play ()}
Translation from comments:
Friends, I have an MC_1 with 10 frames, when it reaches frame 5, I want another movieClip MC_2 to respond (eg: to move or fade etc)
creating a ENTER_FRAME listener for your MC_1 is the easiest way to achieve this
MC_1.addEventListener(Event.ENTER_FRAME,respond);
function respond(e:Event):void{
if(MC_1.currentFrame>=5)
MC_2.gotoAndPlay(2);
//or any other respose you want from MC_2
}
addFrameScript can be used in AS3 to specify a function to execute on reaching a MovieClip frame.
MovieClip.addFrameScript(index:int, func:Function);
A sample implementation of this:
// addFrameScript's index is zero Based, hence 4 means frame 5
MC_1.addFrameScript(4, funcToExecute);
function funcToExecute():void{
// this will get called when MC_1 reaches frame 5
// do stuff here, like manipulating MC_2, etc...
}

AS3. How to change character's template in adobe flash cs5 using AS3?

I'm creating flash game. Here will be abillity to choose one of two (or more) character's. So I have in library created symbol hero. It have 7 animations on click (moving, jumping, attacking etc..)
So I want to create something like hero 2, that player could choose which one likes more. Just how to do that? Create new layer in hero and add animations or how?
I'm asking that because in Action Script 3 I'm adding hero in this case and It always will add the same:
private function create_hero()
{
addChild(Hero);
Hero.gotoAndStop("stay");
Hero.x = stage.stageWidth/2;;
Hero.y = ground.y - 60;
Hero.x_speed = 0;
Hero.y_speed = 0;
}
Maybe here is abillity to make something like that layer2.addChild(Hero);?
Or I need to create new symbol hero2? I don't like this idea, because I have long code to control hero, so for every character I'll need to dublicate code. Could you help me? Thank you.
The proper way is to dynamically create an instance Hero at game start based on the game player's selection. You indeed create two (or more) symbols in Flash CS, with common frame labeling (you can use different animation lengths for different heroes), then, once you've got your hero selection (hero1,hero2,hero3 etc, regardless of their amount) you get the class name from selection and get your Hero variable to be assigned an instance of the respective class. An example:
static var heroes:Array=[hero1,hero2,hero3]; // note: symbol names!
var Hero:MovieClip; // note, not "hero1" or anything, but general MovieClip type
public function selectHero(what:int):void {
// this is called with correct "what", design yourself. I use array index
var whatHero:Class = heroes[what]; // get selected hero symbol
if (Hero && Hero.parent) Hero.parent.removeChild(Hero);
// clean up previous hero. Drop listeners here, if any
Hero = new whatHero(); // get new hero
// process as usual, don't forget to "addChild(Hero)" somewhere
}
How this works: First, you give the player a hero selection dialogue, and call selectHero with proper value of what (0 for hero1, 1 for hero2, etc, as you make your heroes array). Then, the whatHero variable is assigned the corresponding class (yes, one can assign classes to variables in AS3!) And then the class gets instantiated via new construction, and the resultant movie clip is then assigned to Hero variable. After this is done, you can use your hero as before.

movie clip within parent flash move stops entire movie

I'm a bit new to Flash. As I was developing my movie, I introduced 3 layers of sequential animation that I decided I wanted to loop X times. So I copied the frames, created a new Movie Clip symbol, pasted the frames into it, then deleted the original on the stage and dragged this new clip as an instance (my_animation) in its place. All good so far.
However, when I play the entire movie, my_animation looped indefinitely, so I added a stop() action into that movie clip. That worked - plays once. I then added the following as an action on the very last keyframe of the main scene to try and get my_animation to loop X times:
var i = 1;
for (i = 1;i < 5;i++)
{
my_animation.play();
}
stop();
However, my_animation still only plays once. It does not appear to loop as I expect. I also tried replacing my_anmiation.play() with gotoAndPlay(117) where 117 is the frame containing the movie clip, but still the same.
Any help appreciated.
Thanks
UPDATE: I added a trace(i); statement within the loop. In the Output tab, I see:
1
2
3
4
…etc. SO - my_animation is not firing within the loop.
if you call my_animation.play(); with in "for loop" like that, it just calls 5 time consecutively and it just starts animation .
if you call my_animation.play(); during animation of my_animation nothing
happens. it works if it has stopped...
. so you need to listen add callback for to understand that animation finished and you can start that animation again..
var animatedCount:int = 0;
my_animation.addFrameScript(my_animation.totalFrames-1, onMyAnimationEnds);
my_Animation.play();
function onMyAnimationEnds():void{
animatedCount++;
if(animatedCount < 5)
{
my_Animation.play();
}
}
this will animate my_animation for 5 time...

AS3 Error #2109

I am trying to make a simple dress up game.
I have a movie clip called shirts, in which I have:
I have an AS3 action of stop() on key frame 1;
I have a shirts layer were I put all my shirts in key frames. Each key frame for a shirt.
I have a labels layer were I put the labels for shirts. Please note that all labels are like:
shirt1
I also have an items movie clip, in which I have 3 movie clips (pictures of the actual shirts). Each of these movie clips have instance name of...shirt1 etc.
In my AS3 layer from items movie clip, I have something like:
var shirtsArray = [shirt1, shirt2];
for each (var shirtItem in shirtsArray)
{
shirtItem.addEventListener(MouseEvent.CLICK, onShirtClick);
shirtItem.buttonMode = true;
}
function onShirtClick (event:MouseEvent):void
{
MovieClip(parent).shirts.gotoAndStop(event.target.name);
}
When I run the file and click on one of the shirts, I get this:
ArgumentError: Error #2109: Frame label instance229 not found in scene instance229.
at flash.display::MovieClip/gotoAndStop()
at sportbarbie_fla::Symbol3_101/onShirtClick()
Any thoughts?
According to the error message, it seems that event.target is the same MC as MovieClip(parent).shirts (as both have the same auto-assigned instance name instance229). It's hard to guess the reason without knowing the layout of your scene.
Note: it's not necessary to add click listener to each of the MCs, you can just add it to their parent MC if it doesn't contain items of other types. This is called event bubbling (see this article).

How to refer to object from next frame AS3

I've problem with Flash CS4.
I've TextFields on first frame, and other TextFields on second frame, etc.
And at first frame I've TextField to puting number, and button Calculate, which calculate value to all TextField.
And I've menu to navigate throught the tabs (frames).
So... when I put number and click Calculate I get values at TextFields at first frame, but when I switch to next tab (frame) I see clear TextFields and error at output (Error #1009).
I know, that reason is add values in first frame to TextFields from next frames, but I don't know how I can fix it.
Please for help.
When the next tab is clicked, store the value of the textfield in a variable.
If writing your ActionScript on the timeline, this is the code:
// On frame 1:
// Create the variable to store the textfield value
textfieldValue:String = "";
function tabClicked(event:MouseEvent):void {
// Store the value of myTextField
textfieldValue = myTextField.text;
}
// On frame 2
// Populate the new instance of myTextField with the stored value
myTextfield.text = textfieldValue;