as3 - getChildByName on contact - actionscript-3

Would it be possible to have a movieclip like a pad, where when it crosses over with other movieclips on stage that have a name, it would then display the name of the movieclips it crosses over with without having to hard code any of them? I know how to code the cross/hit pad, but would it be able to tell me the getChildByName of any movieclip upon crossing? I am curious to have this automated.

All MovieClips have a name. If you do not provide one, a name such as "instance[n]" will be generated automatically.
If you can handle the hit test part, then once you can reference the movieclip, you can just query the .name property.

Related

Flash AS3 play movieclip from main timeline on certain frame

I can't seem to find the right script to let my movieclip play when a certain frame reaches on the main timeline. Searched a dozen websites, I'm not quite the coder but hope you guys can tell me the best way.
Thanks!
There are some different solutions for this, the most appropriate would be to not code at all on the timeline. But if you are using the timeline, then write on that specific frame:
myMC.gotoAndPlay(frameNumber);
Where myMC is the INSTANCE NAMEof the movieclip object placed on the stage (you can change it in the properties panel) and frameNumber is the number of the frame where you want to play the movieclip from.

ActionScript 3 - Possible to access MovieClips which are an instance of another MovieClip?

I drew an image on the stage and converted it to a movieclip whose name is
originalMovieClip
. The movieclip is now in my library. I dragged the movieclip in my library onto the stage. I did this twice, so now I have two movieclips which are both an instance of
originalMovieClip
which was the original movieclip which I created.
Through actionscript 3, is there a way for me to target all movieclips which are an instance of
originalMovieClip
? I want to basically do
all Movieclips Which Are An Instance Of originalMovieClip.gotoAndStop(2);
In such a basic manner, the short answer is - NO.
Short story long - you need to put them into array and loop through it, and stop every movie clip inside.
Other technique (not appropriate) is to loop through all children of stage, and check if the MovieClip is specific type (again you should at least set linkage class to originalMovieClip).
I suggest using the first one :)

How do I edit a variable declared in the root contained within the frame of a movie clip?

So I have a main scene called Game. In that scene, I have a movieclip called Shop. Inside that movieclip called shop, I have another movie clip called upgradeweapon2. Inside upgradeweapon2, I have a button called "upgradeweaponpb".
I am working in actionscript, in the frame of the movie clip upgradeweapon2. I am trying to edit a variable called "weaponlvl" that was declared in Game. A picture to show what I mean:
http://gyazo.com/96b04ab89ea4a589bee560d53d165b03.png
I am getting the following error: Access of undefined property weaponlvl.
Please tell me there is a way around this... I know weaponlvl is defined in the root scene, game, but is there a way to make the declaration valid across levels of MovieClips, or at least a way to transfer the values accross?
Here is the code I am trying to add:
stop();
upgradepb.addEventListener(MouseEvent.CLICK, upgradeweapon5);
function upgradeweapon5(event:MouseEvent):void{
weaponlvl++;
}
EDIT: Alright I simplified my code, It's just a movieclip, not two layers. But still the same error. Any idea what I can do?
weaponlvl is on the frame of upgradeweapon2.upgradeweaponpb; it is not on the root layer, so therefore in action script it will not make sense. You have two options:
Get weaponlvl through MovieClip(root).weaponlvl or this.parent.parent (which is also the root).

as3 how can i prevent that a new instance is created by entering a frame?

i am working with several nested movieclip objects in a project. but i get into trouble with the buttons i created and implemented in the nested movieclips:
to describe it in a simple way:
I have a main movieclip with five frames, including two buttons with listeners to browse between the frames. Then inside of one Frame I have another movieclip with its own buttons. i instanciated it by hand not through code and gave it a specific name like "nestedMc".
Now I dont want to build the Listeners for those buttons inside the class of the nested movieclip class but in its parent class, which works fine until i then goto another frame in the main movieclips timeline and come back.
obviously every time flash enters a frame its contents get created anew (and therefore get new instance names). I could now try solve this through filling the frames via code.
But maybe there is another way to make sure the frame contains the same instance everytime i enter?
Timeline scripting is a dirty business, and really, a carry-over compatibility layer for Actionscript 2 projects. Whenever possible, I highly recommend not doing it, and simply keeping all of your code in your document class. As you're experiencing, timeline code causes headaches.
Consider instead just creating both states of your Stage (it sounds like that's what your two buttons are jumping between) and simply hiding them offstage or setting their alpha to zero and their mouseEnabled state to false. Furthermore, if the purpose of your frames is to play animation (a tween), consider instead switching to a much more powerful suite such as TweenLite. Moving an object over a hundred pixels (smoothly) can be as easy as:
TweenLite.to(redBall, 3, {x:100});
Now, if you're manually adding these items to the stage, as long as the object is a dynamic one, you can assign an instance name to it which will be saved between frame loads. Be aware the object name is not the same as the instanced name. For example:
var redBall:Ball = new Ball();
redBall.name = "bubbles";
The object's name is Ball, but it's represented as a variable called redBall. Its actual DisplayList name will likely be ambiguous (such as "Instance71"), and I can manually define it as "bubbles". 3 different names for the same object, all very different and necessary.
Even if you give the object a displayList name, you may not be able to reference it through code unless you enable Automatically declare stage instances, which basically creates on each object a pointer to the displayList object.
That said, you can always fetch the object by other means. Obviously, your buttons are always appearing, but you're trying to find a very specific object on the stage. At this point, we can use getChildByName() or getChildAt().
Hope that helps.
-Cheers

Accessing a subelement in Flash

In Flash CS5 I have a button with an instance name "btn", which is made up of movie clips with instance names "mv1" and "mv2".
The question is: can I use gotoAndStop or something similar on the movie clips inside the button from the stage in which the button is instantiated. In pseudocode:
btn.mv1.gotoAndStop(3);
btn.mv2.gotoAndStop(7);
This is likely to be a very basic question, but the one I could not find any information on in the last half an hour.
Probably yes, if btn happens to be a MovieClip. For (almost?) any other display class (DisplayObject, DisplayObjectContainer, Sprite etc) this will fail in compilation.
If your compiler refuses to run your code, try this:
var mv1:MovieClip = btn.getChildByName("mv1") as MovieClip;
mv1.gotoAndStop(3);
Yes you can.
btn.mv1.gotoAndStop(3); will work perfectly fine.