AS3 Having difficulty getting movieclips to stay on top over other clips - actionscript-3

So I have 5 movieclips on screen that when clicked (DOWN state) expand. They all come to the top of the screen because of this code:
addChildAt(this.Step0btn,1);
addChildAt(this.Step1btn,1);
addChildAt(this.Step2btn,1);
addChildAt(this.Step3btn,1);
addChildAt(this.Step4btn,1);
My issue is that on each of the expanded movieclips is another movieclip that also expands, but when I click that it still displays underneath everything else. I thought I could just add another addChildAt command like:
addChildAt(this.Step0btn.Step0img,1);
But when I do that I get this error:
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/addChildAt()
at SMARTTTraining_fla::MainTimeline/SMARTTTraining_fla::frame5()[SMARTTTraining_fla.MainTimeline::frame5:14]
And then the expanded movieclip still displays underneath everything. What could be wrong here?

Step0img is null when that line of code runs. Likely explaination could be that Step0img doesn't exist on the frame that Step0Btn is on.
For your layering issue (and your error I suppose too), this would be what you'd want to do:
this.addChild(Step0btn);
OR
this.setChildIndex(Step0btn,this.numChildren-1);
They do the same thing. This will bring to the foremost layer above the other step buttons, you don't want to change parent/child relationship by doing addChild(step0btn.step0img);
If you are using the same code that I provided in your other question, this is what the onPress method would look like now:
function onPress(event:MouseEvent):void
{
// toggle between frame 1 and 3 on button press
gotoAndStop(Step0btn.currentFrame == 3 ? 1 : 3); //this expands your button presumably
parent.addChild(this); //moves this (this button) to the foreground
}

Related

Object changing layer when going between frames?

I have a child of an object that stays on the scene when the frame changes. So I gotoAndStop(2); and the object is still there. However, when I come back to frame one. The object is on the lowest layer, despite the fact that I originally added it using addChildAt(character, 1); I think this adds it to the first layer? Anyone know how I can fix this issue of keeping a movieclip object on the top layer despite changing frames? Thanks.
In AS3/Flash, the bottom most layer is 0. So doing addChildAt(character, 1) would make your character the second to the bottom layer. addChildAt(character, 0) would make it the very bottom/back layer.
If you want to make it the top most layer, you do any one of the following:
addChild(character); //this is the shortest amount of code
addChildAt(character, numChildren-1); //the is exactly the same as above
setChildIndex(character, numChildren-1); //this is also the same but requires the character already be present on the display list
The latter (setChildIndex) may be preferred IF your character originates on the timeline (eg not created through code). The reason being, if you change through code the parentage of something created on the timeline, it will not go away when no longer present on the timeline.
If you want a way to force something to always be on top, you can do something along these lines:
this.addEventListener(Event.ADDED, bringToTop);
function bringToTop(e:Event):void {
setChildIndex(character, numChildren-1);
}
Doing that, makes it so whenever any other object is added as a child of this, it will set the character to the very top most layer/z-index.

As3 (code snippets) change property of object on stage from within another object

I've been searching for the past few hours how to do this but with no luck. First of all I'm new to AS and not sure what to search for.
So here's what I have and what I want to do:The stage has only 1 frame and on the stage I have 1 button (b1), 1 movie clip that is not visible (area) and 1 visible movie clip that is an animation of 20 frames(ani). For the moment when I press button "b1" it will start the movie "ani" (movie is stopped initially) that will stop when it reaches frame 20. Now what I want is when it reaches the last frame to make movie clip "area" visible. Since I am inside "ani" and on frame 20, I cannot use directly area.visible = true; as I would get the error "Access of unidentified property area." What would be the way to access "area"'s properties from within the other object ?
Inside your "ani" MovieClip (at last frame) add the following sentence,
MovieClip(this.parent).area.visible = true;
Here, parent is your main timeline.
(Note: This approach is not recommended).
Instead, use External classes approach. e.g. use Loader class to load animated swf with COMPLETE event and with contentLoaderInfo get swf object and detect for last frame and make area MovieClip visible.
I'm not sure I completely understand everything you said, but I think what you want to do is something like this on your stage where both ani and area have scope :
ani.addEventListener(Event.ENTER_FRAME, frameCheck);
function frameCheck(e:Event):void
{
if (ani.currentFrame == ani.totalFrames)
{
ani.removeEventListener(Event.ENTER_FRAME, frameCheck);
area.visible = true;
}
}
That was just an example of how you could detect ani hitting the last frame and dealing with that appropriately.
You would need to add the event listener EACH TIME the button is pressed.

ActionScript 3 goto and play event not working

I have a navigation and inside the navigation movie clip, I have buttons and I put this code in my of my button frames
aboutbtn.addEventListener(MouseEvent.CLICK,goAbout);
function goAbout(e:MouseEvent){
this.gotoAndPlay('245');
}
But this didnt work and when I click my button it does nothing, is there something wrong with my code?
this.gotoAndPlay('245');
'245' is a string and gotoAndPlay interprets as a frame label. Remove the quotes, passing an integer, to go to the frame 245.
aboutbtn.addEventListener(MouseEvent.CLICK,goAbout);
function goAbout(e:MouseEvent){
this.gotoAndPlay(245);
}
If that still doesn't work, make sure this refers to the movieclip you want to change the frame. For example, if this code is in the document class, referring to the root instance, this will change the stage's frame.
If you want to change the button frame (only makes sense if it's a SimpleButton instance), change this to aboutbtn, for example.
If it's anything else, you'll to give us more context, it could be a load of other things (different stage? is there a frame 245? is the button mouse enabled? is there any invisible buttons on top of the object?).
Edit:
After clarification: if you want to change the frame of the object above nav, its parent, use:
aboutbtn.addEventListener(MouseEvent.CLICK,goAbout);
function goAbout(e:MouseEvent){
MovieClip(parent).gotoAndPlay(245);
}

Flash Action Script 3: drag movie clip issue

I am new to as3. I create a rectangle and convert it to symbol. Then I go into the symbol and create two sliders. Also, I make the symbol draggable.
controlPanel_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragStartF);
controlPanel_mc.addEventListener(MouseEvent.MOUSE_UP, dragEndF);
function dragStartF(e:MouseEvent){
e.currentTarget.startDrag();
}
function dragEndF(e:MouseEvent){
e.currentTarget.stopDrag();
}
It works fine, but I can't use the slider. If I drag the slider, the entire movie clip moved. How can I solve this problem? Cheers!
Without more info on the structure of your FLA-file I assume that controlPanel_mc is the container containing the rectangle[background] and the two sliders.
What it seems like you did in your code was to add event listener to the container, what that means is that "if a click occures anywhere on this object, do the following". Since that clip "owns" the 2 sliders, the sliders will not get any MOUSE_DOWN-events since the parent is always the one handling the event first.
You probably want to convert the background into a symbol and add the eventListeners to that object instead.
bg.addEventListener(MouseEvent.MOUSE_DOWN, onBgClick);
bg.addEventListener(MouseEvent.MOUSE_UP, onBgRelease);
function onBgClick(e:MouseEvent){
controlPanel_mc.startDrag();
}
function onBgRelease(e:MouseEvent){
controlPanel_mc.stopDrag();
}
That should solve your issue :)

dispatchEvent not working in piecemaker 2

i have tried to research this issue, but being a flash newbie i struggle to wade through the terminology...
i am using the piecemaker 2 slider on the landing page of my BudeStrings Dev Site. the slider contains a total of 5 slides; the first is a movie clip containing multiple frames and the following four are animated text clips consisting of only one frame each.
as per instructions i found on the interwebs, i added a keyframe after last frame of the first (multiframe) clip and added the following code:
dispatchEvent(new Event(Event.COMPLETE));
stop();
to stop the clip looping and to enable the autoplay behaviour of the slider. this works absolutely fine with the first (multiframe) movie clip, but when i try to add the same code to the other four (single frame) clips it does not have the desired effect.
these four clips use the following code:
var myString:String = "Sample text "
var myArray:Array = myString.split("");
addEventListener(Event.ENTER_FRAME, frameLooper);
function frameLooper(event:Event):void {
if (myArray.length > 0){
TextField.appendText(myArray.shift())­;
}
else {
removeEventListener(Event.ENTER_FRAME, frameLooper);
}
}
to display some text with an animated typing effect. if i add a keyframe with the dispatchEvent code as i did on the first clip it stops working and when i append the dispatchEvent code to the above actionscript it has no effect, and piecemaker 2 stops autoplaying when it hits slide number 2.
beyond that i have no idea what is causing the problem. please help.
regards, jan
First of all, you should use the appendText method on a textfield instance, not the class itself (the compiler should give you an error). So if the textfield on your stage is called 'txt' it would become 'txt.appendText..'. Secondly, you should define the event dispatching within the else section as at the time it runs, your animation would be completed.
[edit] make sure you enable strict mode in your publish settings