Actionscript 3 - Toggling visible status of a Movie Clip - actionscript-3

I have a MovieClip of instance name BeachMovie.
On the same frame I have another MovieClip, inside this MovieClip is actionscript that will play sounds and toggle pictures on and off. It is also used so that when pressed the .visible status of BeachMovie is toggled on and off.
How can I communicate with BeachMovie MovieClip when the actionscript to do it is used (and is needed) within another Movieclip?

Ideally your instance names should begin with a lowercase letter.
Try
this.parent.beachMovie.visible = false;
or MovieClip(root).beachMovie.visible = false;

Related

How to make a movieclip play on a specific frame in AS3?

I'm incredibly rusty at Flash having not touched it in probably 10 years and can't seem to figure this out, or find it online:
I have a MovieClip with two layers, each having a Shape Tween. Basically its a Door that opens and closes.
I dropped it onto the main timeline but now I need it to start and stop. This is where I'm now struggling since the last time I used Flash actions could go on specific keyframes.
I made a new layer called actions just to keep things organized and currently have:
barrier1.stop();
I just want something that lets me state a frame, say 57 to have barrier1 start playing on. Tried using play(); and Event.ENTER_FRAME with no luck. How would I set this up?
Well it is easy with the instance name of your movieClip
barrier1.stop(); // Stops the movieClip
barrier1.play(); // Resumes
barrier1.gotoAndStop(12) // Goes to 12nd frame and stop
barrier1.gotoAndPlay(12) // Goes to 12nd frame and play
barrier1.currentFrame // returns barrier currentframe
For capturing frame from scene level:
this.addEventListener(Event.ENTER_FRAME,onLoop);
function onLoop(event:Event){
if(barrier1.currentFrame == 57){
trace("BARRIER is in 57. frame");
}
}
Inside on the animation clip on the first frame
var root:MovieClip = this.parent as MovieClip
root.makeStartSceneAnimation()
**in timeline scene level [root]**
function makeStartSceneAnimation(){
/// barrier started to play
}
If you are using timeline, you can add Key frame on the desired frame, and then add stop(); as Action in the action layer. But bear in mind that if you do this in the main timeline - it will stop everything. If you want to stop that MovieClip, then you have to do this inside MoviceClip's timeline.

how do I link a button inside a movie clip into the mainframe (flash CS6 AS3)

i have made a movie clip with some buttons inside. The idea behind this was to allow a scrolling system to be used. The only problem is i can't seem to figure out how to link a button inside a movie clip with a frame from outside of the movie clip and on the main stage.
My code atm is:
stop();
import flash.events.MouseEvent;
sports1.addEventListener(MouseEvent.CLICK,sport1)
function sport1(e:MouseEvent)
{
MovieClip(root).gotoAndStop(sports)
}
What i would like to happen is that each button inside the movie clip will take me to a certain frame on the main stage, like a navigation system. I am really new to flash so i may not understand all the technical terms yet, so be easy on me :)
So if you are creating a Main Movie Clip on the Stage and have inner Movie Clips that are acting as buttons. You want those inner buttons to take you to different frames on the main stage when the user interacts with them.
This is how you would do so.
What you would need to do as you already done give the Main Movie clip holding the inner buttons an instance name sports1 any inner button would also need an instance name say a button has an instance name of mcButton to access that button you would need to call on it like so:
sports1.mcButton.addEventListener(MouseEvent.CLICK,sport1)
sports1.mcButton.buttonMode = true;
then if you want that button when clicked to go to frame 2 on the main stage you would simply do this in the sport1function:
function sport1(e:MouseEvent):void
{
gotoAndStop(2);
}
I threw in the sports1.mcButton.buttonMode = true; that way it shows that it is interactive ie click able

Differences in assigning on-stage or code-created object instances to stage.focus?

I am trying to focus on a button(a MovieClip) with up/over/down/disabled effects and I am facing the following issue.
When I specifically place an instance of the MovieClip to the stage and name it properly, I can then simply assign that MovieClip to stage.focus and when I export the .swf file, the MovieClip is present on the scene with the down animation visible.
I am trying to create a few of those buttons on the fly. However, when I create an instance of that button (and of course add it to the scene) by writing code and then assign that newly created button instance to stage.focus, the instance is visible on the scene, but the up animation is visible instead of the down animation.
I tried using gotoAndStop() to manually get the instance to show down animation, before assigning it to stage.focus, but that way the down animation is not fixed, and when I get my mouse on and off of the object without any clicking.
What may be the reason, and more importantly, the solution to this issue? How can I get down animation to be persistently visible?
The code below is what I wrote in the frame to create the button instance and assign focus to it. When I manually put an instance on the stage, I just used the last line of the code below, after naming the instance someBtn, of course.
var someBtn = new SomeButton();
addChild(someBtn);
someBtn.x = 500;
someBtn.y = 500;
someBtn.visible = true;
stage.focus = someBtn;

how to set actionscript code to execute only once

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.

Bitmap as button?

How to set a bitmap as a button so that i can apply button mode and mouse-event stuff on it, without adding the bitmap to a Movie Clip?
var bmpFull=new Bitmap(event.currentTarget.content.bitmapData);
bmpFull.smoothing=true;
bmpFull.name="photo";
bmpFull.alpha=0;
//fullMC.buttonMode=true;
fullMC.addChild(bmpFull);
Unfortunately, Bitmap objects do not extend from the InteractiveObject class - that is, they don't have (and cannot easily get) the ability to receive mouse events.
As pointed out by antpaw and Jeremy White in the previous answer, the simplest container class that does receive mouse events is the Sprite class. Therefore, if you wanted to have a Bitmap receive mouse events, and not use a MovieClip, you could use a Sprite:
var bmpFull:Bitmap = new Bitmap(event.currentTarget.content.bitmapData);
bmpFull.smoothing = true;
bmpFull.name = "photo";
bmpFull.alpha = 0;
var bmpContainer:Sprite = new Sprite(); // can receive mouse events, for example:
bmpContainer.addEventListener(MouseEvent.CLICK, clickHandler);
bmpContainer.buttonMode = true; // this just makes it show the hand cursor, and is not necessary for the mouse events to work
bmpContainer.addChild(bmpFull);
In fact, I would recommend using a Sprite, as they're simpler objects than MovieClips and thus do not require as much memory.
Now, if you wanted to make a Bitmap dispatch mouse events without using any sort of container clip, you'd probably need to write your own extension of the Bitmap class that had it's own event manager. That would be far, far more complicated. I highly recommend just using a Sprite as a container.
buttonMode is a property of Sprite
inheritance of a movie clip goes like this
MovieClip >> Sprite >> DisplayObjectContainer >> InteractiveObject >> DisplayObject >> EventDispatcher >> Object
Bitmap >> DisplayObject >> EventDispatcher >> Object