Controlling frame navigation inside a sourced MC from inside another MC - actionscript-3

In my swf, I have a MC (let's call it MC1) as well as a scrollpane which is connected to a different MC in the library (let's call that one MC2).
Part of what I'm trying to do is make it so that when an element inside MC1 is dragged and dropped, it makes MC2 go to frame #2, and also makes MC2 scroll up to a certain position.
So far I'm just trying to get the frame navigation to work. Here is the code below. "squareDrag" is the instance name for the element in MC1 I'm going to drag and drop. "mcPane1" is the instance name for the scrollpane on the main timeline. I don't have an instance name for MC2 since it's not on the stage, only in the library. I read something about using "source" instead of an instance name when referring to something inside a scrollpane, so I tried that first.
import flash.events.MouseEvent;
this.addEventListener(MouseEvent.MOUSE_DOWN, startDragging, true);
this.addEventListener(MouseEvent.MOUSE_UP, stopDragging, true);
function startDragging(e:MouseEvent) {
squareDrag.startDrag();
}
function stopDragging(e:MouseEvent) {
squareDrag.stopDrag();
MovieClip(root).(mcPane1.source).gotoAndStop(2);
}
What is it I should be doing here? I'm assuming it's something simple I just haven't been able to find through google.
Thanks.

Related

AddChild with GetChildByName

Complete AS3 noob here - I've tried Googling this, but I can't seem to find what I'm looking for (I stumbled across this, http://ub4.underblob.com/as3-naming-elements-dynamically/, but it doesn't weem to work for me).
I'm trying to dynamically add a Movieclip inside another Movieclip through an external AS3 class
Something like this:
var bullet:Bullet = new Bullet(x, y, "right");
var stageBackground:MovieClip = (stage.getChildByName("back") as MovieClip);
stageBackground.addChild(bullet);
However, while this compiles correctly, at run time, I get error #1009 - Cannot access a property or method of a null object reference.
The debug panel tells me the problem is with this line:
stageBackground.addChild(bullet);
But I can't seem to figure out what's wrong with it. I've tried recasting stageBackground as a Sprite, but that didn't change anything. I know the MovieClip back exists - when I reference it through near identical code in my document class, it works perfectly.
You are accessing stage here to find your container, which is very likely the problem.
You are probably thinking that the stage property refers to "the stage" in Adobe Flash authoring environment.
That's not true.
If you placed a MovieClip on "the stage" in the Flash IDE, it ends up on the main time line, this however, is not the thing the stage property is referencing. stage is the topmost DisplayObjectContainer in the display list. It only exists at runtime. It more or less represents the FlashPlayer window, the runtime environment that executes your .swf file.
In short: you are simply looking for your back MovieClip in the wrong place.
The property of a container that represents the main timeline is root.
Do not use root either.
As you can see, your code becomes dependent on the display list
structure of your application. You are already struggling to find the
container that you are looking for. If you change the structure, your code breaks. Even changing the name of the container (for example to something like "background") will wreak havoc.
Instead, use Events.
You are in another class and you want to fire a bullet.
So you create that bullet same as you do now:
var bullet:Bullet = new Bullet(x, y, "right");
Next, dispatch an Event to notify the rest of your code that a bullet was created and it should be placed in the appropriate container:
dispatchEvent(new BulletEvent(BulletEvent.CREATED, bullet));
(Create a custom event class BulletEvent that extends Event, with the apropriate setter and getter for a Bullet object)
Register a listener on the object of your class that creates the bullets, you will catch this event and place the bullet in the container:
var object:YourClass = new YourClass();
object.addEventListener(BulletEvent.CREATED, addBulletToContainer);
function addBulletToContainer(e:BulletEvent):void
{
// adding the bullet to the container
back.addChild(e.bullet);
}
This code would be placed in the parent of your back MovieClip.
The Flash IDE automatically creates variables behind the scenes that have the same names as the instance names. That's why the variable back is available here.
Using events here allows you to literally fire the bullet into your code with somebody else taking care of it, where it's easy to figure out the container it belongs into.

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

Replacing movieclip

I m currently working on converting one of my as2 game project into as3 (flash pro cs4). But there is a problem on the background movieclip.
I have a function called disposemc:
function disposemc(mcname:MovieClip):void{
mcname.parent.removeChild(mcname);
mcname = null
}
Another function called changelayer:
function changelayer(mcname:MovieClip,layer:MovieClip):void{
layer.addChild(mcname)
}
So in main timeline frame 2 I have a movieclip on stage (placed in IDE) with instance name "bg_mc", then on main timeline frame 3 I have a DIFFERENT mc on stage with the SAME instance name "bg_mc". The purpose of this is to replace the old bg_mc by the new one.
The problem is,
In frame 2, I applied changelayer function to bg_mc and moved it into a child mc of root by addChild, and then applied a function of my CPU image post processing framework and added the bg_mc into an array.
After some stuffs happened, I went nextFrame to frame 3, and found that the new bg_mc didnot replace the old one, instead it overlaps.
So I tried disposemc function when leaving frame 2 to get rid of the old bg_mc, and at frame 3 I applied changelayer to bg_mc and added bg_mc to my render array.
And I found that the old bg_mc is off the stage but it is still rendering onto my bitmap data, means it is not nullified like it suppose to be in disposemc function. And it is also hard to access it because the name overlaps. When I call bg_mc in frame 3 it is the new one, but the old one still exists, and from the rendered bitmap data it can see it is still playing.
Will making the old mc stop in disposemc function help?
Can anyone give any help? If my structure of making bg is bad, is there any other way to override an instance with a different mc in library?
The purpose of this is to replace the old bg_mc by the new one
You work with MovieClip and frames. So use keyframes by design. If you need to place different background in third frame, don't program it, place it. If you want program, don't use MovieClip with keyframes as main navigation instrument.
Main timeline could be easily swapped with very simple logic and several movie clips with only one frame
//Some MovieClips from the library, with only one frame, still designed in Flash IDE
var currentScene:DisplayObject;
var home: MyHove = new MyHove()
var intro:MyIntro = new MyIntro();
navigate(home);
//after some interaction from the user, go to another page
navigate(intro);
//It will work like gotoAndStop... but better, and give you more control
function navigate(scene: DisplayObject):void{
if(currentScene != null){
removeChild(currentScene);
}
currentScene = scene;
if(currentScene != null){
addChild(currentScene);
//apply logic for swapping background
}
}

accessing functions between two (2) movieclip inside a nested movieclip AS3

ok im a noob for AS3 since i just started, i have two (2) movieclips inside a movieclip, the main mc is called main_mc then the two movieclips inside named trigger_mc and move_mc, trigger_mc has the instance name of start_ani, then inside the timeline of main_mc i have this code:
import flash.events.MouseEvent;
start_ani.addEventListener(MouseEvent.CLICK, correctans);
function correctans(e:MouseEvent):void {
move_mc.animate();
}
then i created a motion as actionscript 3.0 using move_mc then i inserted the code inside the timeline of the move_mc itself, and i made a function for that motion called animate, my problem is how do i access a function between two movieclips which both are inside another movieclip, i know this method is not programming wise, but i kinda need to learn this, pls help me, i badly need this, thank you in advance.
Parent is a property, not a function - you don't need ():
this.parent.move_mc.animate();
Also, you didn't mention the instance name of the move_mc movieclip, but access like the above requires the instance name to be move_mc - the movieclip symbol name doesn't matter in actionscript.
Update 1: To clarify, you said: trigger_mc has the instance name of start_ani
Good, then this code will work:
start_ani.addEventListener(MouseEvent.CLICK, correctans);
But you didn't say: move_mc has the instance name of ???
So we don't know if this code will work:
function correctans(e:MouseEvent):void {
???.animate();
}
Fill in those ??? for one.
Update 2: do you know if the click handler is being fired? Why not add a trace statement?
function correctans(e:MouseEvent):void {
trace("got click event!");
???.animate();
}
Because for a CLICK event, you need:
start_ani.buttonMode = true;
Though as you say, this isn't good programming practice because this assumes those two movieclips are siblings of the same parent. It's not extensible. If they're not, your code could throw errors. Just keep that in mind.

How can I make a button inside a Movieclip send me to a frame in the main timeline?

I made a movie clip with an animation of a pull-down menu in which 3 buttons appear. The thing is, I'd like for those buttons to send me to an exact frame within the main timeline, and I haven't quite understood how it can be made.
The movieclip is located within a frame inside the main timeline, and its structure is kind of like this one.
The frame "Sucesos" is not within the timeline of the MovieClip, but in the Main Timeline, whereas the Button "IrSucesos1" is located inside a frame within the movie clip. The code I use is this one.
function LinkSucesos1 (event:MouseEvent):void
{ gotoAndStop("Sucesos1");
}
IrSucesos1.addEventListener(MouseEvent.CLICK, LinkSucesos1);
I don't know whether I should put the code inside the timeline of the movieclip or on the main timeline in the frame the movie clip is located, or if the code is the right one.
How do I make the buttons work by sending me to the frame that I want to get to in the main timeline and then have it stop there? What is the code, and where should I put it?
If I've understood your structure the way I think I have, the following should work. On your main timeline, add a new layer called Actions and on the frame that has the movieclip containing your buttons add a keyframe to this layer. Go to the movieclip that contains the buttons. Open the actions panel and type Stop(). Create a new layer called Button actions, click on the (blank) kayframe on this layer and add the following code:
import flash.events.MouseEvent;
my_button.addEventListener(MouseEvent.CLICK,on_click)
function on_click(e:MouseEvent)
{
MovieClip(root).gotoAndStop(5)
}
You'll need to change my_button to the button instance you'd like to assign the event listener to and 5 to the frame you want to go to on the main timeline. Test your movie - hopefully this should work for one button. For the others to work, simply give them each their own event listener and function to do what you want them to (ask if you get lost!).
stage is available anywhere where the displayObject is linked to the stage.
You can give a reference object to that control button. Your timeline object won't change so its fine.