How to load a movieclip and goto the first frame - actionscript-3

I use a button in my header of my website to remove one movieclip (page of my website) off the stage and tweened another onto the page like so...
function moviesClick(event:MouseEvent):void
{
pageContainer_mc.removeChildAt(1);
pageContainer_mc.addChild(pMov);
TransitionManager.start(pageContainer_mc, {type:Photo, direction:Transition.IN, duration:1, easing:None.easeNone});
}
I have encountered a problem - if i have been on this timeline previously and gone from frame 1 to say frame 2 (another page in that area of my website) when i use the button in my head of my website again, i stay on frame 2 but i want to be returned to frame 1 always when this button is pressed (code above for my button).
How do i force this timeline to start at frame 1 whenever the button in my header (on a seperate timeline) is pressed?

Is the timeline in question the pagecontainer_mc? If so:
pageContainer_mc.gotoAndStop(1);
Edit based on comment discussion:
Assuming that's the correct path, this should work.
moviesPage.moviesHolder.moviesNow.gotoAndStop(1);

Related

AS3 Change Frame/Instance within MovieClip

I am not a programmer, so please forgive me if my question is too noobish.
I created a MovieClip in my library called "skill". Then I added three instances of that MovieClip into my stage called "skill_01", "skill_02", and "skill_03" respectively. I selected all three instances and created another MovieClip called "diagram" to nest them inside it. I will later add more skill instances inside this MovieClip called "diagram" but for now, I need the basics to work with three instances.
The main timeline or stage only has 1 frame, and so does the "diagram" MovieClip instance. However, the skill instances all have two frames: in frame 1 I have an PNG image showing them as being locked/offline, and in frame 2 I have another PNG showing them as unlocked/online. I need to be able to toggle them by clicking and right clicking on them, just like in the example below (see link)
I am trying to make it so that when you left click the skill instances they toggle to Online/Unlocked by switching to the PNG image in frame 2, and when you right click them, it goes back to frame 1 (Offline/Locked). I've tried several different lines of code, including the ones recommended by Adobe itself, and others, and I can't figure out what I'm doing wrong. It will go to frame 2 and refuse to go back to frame 1 when right clicked.
This is the code I have in the MovieClip:
stop();
skill_01.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame);
function fl_ClickToGoToNextFrame(event:MouseEvent):void
{
gotoAndStop(2);
}
This part of the code doesn't work:
skill_01a.addEventListener(MouseEvent.RIGHT_CLICK, fl_ClickToGoToPreviousFrame);
function fl_ClickToGoToPreviousFrame(event:MouseEvent):void
{
prevFrame();
}
Screenshot 1:
gyazo (dot) com/e622baee46c3fabbd8c9a8d2df8396fe
Screenshot 2:
gyazo (dot) com/1b360716a5d243aa74e2be4127fe9a5b
(Sorry for typing the links like that, it won't let me add more than 1 link because I just signed up to this Forum)
For more context, I am trying to make a Skill Tree, that works similar to this one: http://www.dungeonsanddevelopers.com/#__6_Your
I will appreciate any help figuring this out!
if you have named your movieclip, skill_01, so what is the skill_01a ?
also add your event listener and its callback function in to the movieclip's parent and inside the callback function, for accessing the event owner, use
// stop(); not necessary to call stop if you have only 1 frame
with (this.diagram) {
skill_01.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame);
skill_02.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame);
skill_03.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame);
skill_04.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame);
skill_01.gotoAndStop(1);
skill_02.gotoAndStop(1);
skill_03.gotoAndStop(1);
skill_04.gotoAndStop(1);
}
function fl_ClickToGoToNextFrame(event:MouseEvent):void
{
if (MovieClip(event.target.parent).currentFrame == 1)
MovieClip(event.target.parent).gotoAndStop(2);
else
MovieClip(event.target.parent).gotoAndStop(1);
}
Edit: when an event is dispatched from clicked movieclip, only the last movieclip inside hierarchy tree will be passed as event.target in your exaple, that can be skill_01 (if currentframe is 1) or skill_1a (if currentframe is 2) athwart the question body, you have not only a png image inside skill's frame 1 and 2, they are movieclip's and will be returned as event.target. but if you only put your bitmaps (locked.png & unlocked.png) instead of skill_01 and skill_01a, the previous code works fine. bitmaps does not capture event so its parent is who accepted click event and its all a little confusing, i'm not good at explain
if its hard to comprehend
we shall use some thing like it, easier but larger code
with (this.diagram) {
skill_01.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame_s1);
skill_02.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame_s2);
skill_03.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame_s3);
skill_04.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame_s4);
skill_01.gotoAndStop(1);
skill_02.gotoAndStop(1);
skill_03.gotoAndStop(1);
skill_04.gotoAndStop(1);
}
function fl_ClickToGoToNextFrame_s1(event:MouseEvent):void {
toggle_buttons(skill_01);
}
function fl_ClickToGoToNextFrame_s2(event:MouseEvent):void {
toggle_buttons(skill_02);
}
function fl_ClickToGoToNextFrame_s3(event:MouseEvent):void {
toggle_buttons(skill_03);
}
function fl_ClickToGoToNextFrame_s4(event:MouseEvent):void {
toggle_buttons(skill_04);
}
function toggle_buttons(button:MovieClip):void
{
if (button.currentFrame == 1)
button.gotoAndStop(2);
else
button.gotoAndStop(1);
}

How do I put a button on specific frame in flash cs4

i am using actionscript 3.0 , i want to create a button in a specific frame. I've created a button on frame 1 that goes to a specific frame, for example frame 2. this is the code i am using for the button.
button1.addEventListener (MouseEvent.CLICK, btn1);
function btn1(event:MouseEvent):void{
gotoAndStop(2);
}
then, on frame 2, i want to put another button that only visible or appear on frame 2 when you click the button on frame 1. Thanks
its a bit hack and bash but by default have the button on frame 2 set as invisible buttonName.visible = false..
Then add the following to your event handler
button1.addEventListener (MouseEvent.CLICK, btn1);
function btn1(event:MouseEvent):void{
**buttonName.visible = true;**
gotoAndStop(2);
}

Applying action to a button in one frame makes it apply to whole movieclip in Flash CS6

I have a movieclip with several layers and frames. Some of those contain buttons. When I click button 1 I want it to go to the next frame. When I click button 2 I want it to go to frame 23. This is the code I'm using for button 1:
this.addEventListener(MouseEvent.CLICK, convnext);
function convnext(evt:MouseEvent):void
{
MovieClip(parent).gesprekkencivcen.nextFrame();
}
and this is the code for button 2:
this.addEventListener(MouseEvent.CLICK, convend1);
function convend1(evt:MouseEvent):void
{
MovieClip(parent).gesprekkencivcen.gotoAndStop(23);
}
What happens now is that when I click either of the buttons, or in fact anywhere inside the movieclip (even layers I haven't applied actions to) it executes both functions at the same time so I end up going to frame 24. Can somebody provide an answer to this problem?
Obviously this in both cases refers to the same object, and not to the buttons in particular. Record the names of those buttons as you've named their instances on the timeline, say button1 and button2 and write the code employing those names.
button1.addEventListener(MouseEvent.CLICK, convnext);
function convnext(evt:MouseEvent):void
{
parent.parent.gesprekkencivcen.nextFrame();
}
button2.addEventListener(MouseEvent.CLICK, convend1);
function convend1(evt:MouseEvent):void
{
parent.parent.gesprekkencivcen.gotoAndStop(23);
}
With this, however, you will need to update the link to gesprekkencivcen in both listeners, as those buttons will have this as parent, and their target apparently is not a child of this. I have tried to plainly set a call to parent.parent.gesprekkencivcen, which might not work.

Set a button to invisible once clicked

I'm trying to set a button to invisible in AS3 however when the I leave the frame and come back to it the button is visible again. This is for a jeopardy game I make making for comm tech class.
Here is what I currently have:
a1.addEventListener(MouseEvent.CLICK, a1mouseClick);
function a1mouseClick(mouse:MouseEvent) {
a1.visible = false;
gotoAndStop("A1");
trace("Going to A1");
}
however when it comes back to the frame with the a1 button it is visible again.
Here is my current animation: https://dl.dropbox.com/u/23938245/jeporady.fla
While moving through the timeline flash player can recreates sprites, movie clips and text fields, so your buttons appears visible again. To prevent recreation move all controls to separate level without key frames. If key frames are required try to set the same instance name for this button in all keyframes.
#fsbmain and #prototypical they are right.
While moving through the timeline flash player can recreates sprites, movie clips and text fields, so your buttons appears visible again. To prevent recreation move all controls to separate level without key frames. If key frames are required try to set the same instance name for this button in all keyframes.
I was looking at your project, and offer a quick fix, you need to do the following:
Create a new layer on top to manage a few of actions availables for all frames with the following actions:
import flash.display.DisplayObject;
// Manages the buttons visible state
var buttonsStates:Object = {
"a1":true, "b1":true, "c1":true, "d1":true, "e1":true,
"a2":true, "b2":true, "c2":true, "d2":true, "e2":true,
"a3":true, "b3":true, "c3":true, "d3":true, "e3":true,
"a4":true, "b4":true, "c4":true, "d4":true, "e4":true,
"a5":true, "b5":true, "c5":true, "d5":true, "e5":true
};
// Checks the buttons visibility
function checkVisibility () {
for (var buttonName:String in buttonsStates)
{
var child:DisplayObject = this.getChildByName(buttonName);
child.visible = buttonsStates[buttonName];
}
}
// Saves the visible satatus to false
function setVisibilityToFalse(target:*) {
buttonsStates[target.name] = false;
target.visible = false;
}
Every time you want to check the visibility of the buttons you must call the checkVisibility() function. For example, every time you return to the button list.
Finally the event handler for each button must be like this:
function a1mouseClick(mouse:MouseEvent) {
setVisibilityToFalse(mouse.currentTarget); // Saves the visible state to false
gotoAndStop("A1");
trace("Going to A1");
}
You can download the edited file here http://cl.ly/Lt6X
You are missing a fundamental aspect of how the flash timeline and keyframes function. Once you move away from that frame, the stage instance of the content of that frame and it's properties/states are gone. When you return to that frame, the instance is created again based on the keyframe contents.
I think the best solution given your current approach is to put the main board persistent throughout all the frames. You can do that by creating a layer for it, and have it's keyframe extend from frame 2 to frame 27. However, your next issue will be adjusting visibility of all the elements on that screen when you don't want them visible.
My suggestion would be to put all the elements of that screen into a movieclip symbol of it's own and add that movieclip, and all code for it's listeners, to this new layer you created. For example you might name that instance - main_board and therefore you could modify it's visibility with main_board.visible property. If you did choose that solution, you would need to modify all the code on that frame to use that instance name as well ie :
main_board.a1.visible = false;
Also, you'd need to modify all you addEventListener lines as well :
main_board.a1.addEventListener(MouseEvent.CLICK, a1mouseClick);
Your approach for this game could be greatly simplified, but even further beyond the scope of this question than I have already gone!

How can a loaded child swf 1 communicate to next child swf, child swf 2, which is currently not loaded

I have a parent swf with three buttons: next button, back button, ticker on/off button.
I have three child swfs, each with a ticker mc. When I click on ticker button on parent swf
it dispatches a custom event which brings ticker MC on stage and thus ticker becomes visible (ticker off button displaces it out of stage by changing the value of ticker_mc.y).
the problem I am facing is when ticker is on and I click next or back button to load next or previous child swf, the ticker goes off. How can I make sure that ticker is on all the time and closes only when I close it. How can a loaded child swf 1 communicate to next child swf, child swf 2, which is currently not loaded, that since the ticker is on on swf 1 so you please keep the ticker on on swf 2 as well?!
I think I know the problem. I have put a default value of ticker_mc = 160 (makes ticker inactive) on every child swf. So, the ticker becomes inactive everytime I load a new child swf. If someone could guide how can I do this, my problem will be solved: When first child swf is loaded, the default value of ticker_mc.y should be 160. I want to hold this value in a variable. When I click ticker active btn, position of ticker_mc changes to 194 so the value of variable should change to 194. When I click next btn, the variable value (160 or 194) should be sent to next child swf and ticker_mc.y should be equal to variable value.
This is the code so far
"Next button"
go_mc.next_btn.addEventListener(MouseEvent.CLICK, playNext);
function playNext(e:MouseEvent):void {
if (swf_no < (clips.length-1)) {
swf_no++;
SoundMixer.stopAll();
nextClip();
pp.play_btn.visible=false;
pp.pause_btn.visible=true;
}
}
function nextClip():void {
thisLoader.load(new URLRequest(clips[swf_no]));
}
//Every time I click next or back button this event is called and I remove previous swf from stage.
thisLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, doneLoading);
function doneLoading(e:Event):void {
stopAllChildMovieClips(stage);
stage.removeChild(thisMC);
thisMC=MovieClip(thisLoader.content);
thisLoader.unloadAndStop();
thisMC.x=37;
thisMC.y=78.50;
stage.addChild(thisMC);
playAllChildMovieClips(stage);
}
//Ticker active button on parent swf
viewUnviewTxt_mc.viewTxt_btn.addEventListener(MouseEvent.CLICK, viewText);
function viewText(e:MouseEvent):void{
var viewText:String = "viewTxtFunc";
dispatchEvent (new Event(viewText, true));
}
//To turn off ticker(default). Ticker mask is off/invisible by default(out of stage).
thisMC.tickerMaskMC.y = 160;
//custom event received on child swf
parent.stage.addEventListener("viewTxtFunc", eventHandler);
function eventHandler(e:Event):void{
tickerMaskMC.y = 194;
}
Use your parent swf as the controller for all of your child swf's.
When a child swf wants to communicate to another child, send an event or call up to the parent swf and then back down to the other child swf that you want to receive the call.
Though in your example it sounds more like you're having a layering issue than a communication issue. If you want to assure that when you click next or back that if the ticker is active, it stays active, then be sure to load that new swf in a DisplayObject that is behind the ticker DisplayObject. In this instance both DisplayObjects are your loaded swf's.
Take a look at the getChildAt and addChildAt methods of DisplayObject. The first DisplayObject added to the Stage or another DisplayObject will have an index of 0, the second will be 1 and so and so forth. Using that you should be able to correctly layer your ticker to always stay on top and be displayed when you want it to be
Update from your comments:
You could look into an Object called LoaderContext. It has a property called parameters on it which is an object you just pass along with the load request for your SWF.
var lc:LoaderContext = new LoaderContext;
var thisLoader:Loader = new Loader();
lc.parameters = { "tickerHeight" : "myVariable" };
thisLoader.load(new URLRequest(clips[swf_no]), lc);
Then in the child swf try:
trace(loaderInfo.parameters.tickerHeight);
I forgot to add this as well, your compiled SWF target must be at least version 10.2 or higher to use LoaderContext