AS3 - How do I code a particular frame? - actionscript-3

So its like I want a code just for frame 2. Is there any other way other than just going to frame 2 and typing in the Actions Panel? For example I am using a class called supportForce and I want a part of the code to apply only for frame 2. This might be stupid but just in order to get a doubt cleared I asked this question, forgive me if I am wasting your time. Thanks in advance.

Sure, simply write your code from the parent scope of supportForce inside a statement such as this one:
if(supportForce.currentFrame == 2) { //DO MAGIC ON FRAME 2! }

Related

MovieClip(root.this) not working

I'm having a issue in AS3 with using MovieClip(root.this).
So I have a MovieClip called Slime and I have code inside of slime.idle in the 1rst frame. The code is: MovieClip(root.this).gotoAndStop(2);
For some reason that will not work and does not make the slime go to frame 2. I don't want to do MovieClip(root).gotoAndStop(2); because I have more than 1 slime in the stage that I do not want all of them going to frame 2. That is why I need to use MovieClip(root.this). Does anyone know my problem and how to fix it? Thank you.
MovieClip(root.this) is not valid syntax for multiple reasons. I don't think this behaves how you think; this refers to the object that the script belongs to and cannot be used the way you are trying to use it.
Are you trying to target a specific "slime" to go to frame 2 within its own timeline? In that case you just need to call gotoAndStop() on a reference to that specific slime. For example: MovieClip(root).slime123.gotoAndStop(2). How you get the reference depends on your current code and display structure.
If your code is within the "slime" symbol timeline, you can make it go to frame 2 without referring to root at all, because this is already the target you want. For example: this.gotoAndStop(2) (or gotoAndStop(2); this is usually optional).
If you post more code and explain what you thought this should refer to, I can help more.

Is it possible to add a Movieclip in a specific frame?

I add a menu button in the first frame (from an external class).
When i click it, it goes to the second frame, and it's still there.
Is it possible to add a mc in a specific frame?
I try to remove it while clicking on it but it gives me error...
Thanks!
You can remove your button by clicking like that :
function clicBouton(e:MouseEvent):void
{
gotoAndStop(2);
e.target.parent.removeChild(e.target);
}
You can use the addFrameScript() function to do so, as you already seem to have found out.
Here an example:
http://www.flashwork.biz/blog/?p=229
To tell you what you exact problem is you most likely need to post more details, but looking at your comment, you might have simply missed that addFrameScript() frame numbers start with 0 (zero) while frames in the IDE start at 1. Substract 1 from the frame number that the IDE shows.
An example:
If you want to add a script for frame 1 you should use
addFrameScript(0, onFrame1);

How do I check if objects in a frame are loaded yet or not after a gotoAndStop(5) action?

I am facing a problem, where there is a function executed the next line right after a gotoAndStop(130) action, the function depends in its work on the objects inside this frame. but the problem is that the function seems not seeing any children.. why? why? why? why?
myMc.gotoAndStop(130);
create_waypoints(par1,par2,par3);
the function creat_waypoints depends on the drawn obstacles that exists in frame 130 inside the myMc.
it seems (but I don't know for sure) by the time that creat_waypoints executes its actions, frame 130 was just entered but not yet loaded...
What can I do? Thanks in advance..
after many experiments, I have come to a working solution but it is twisted, it had to skip an entire loop of actions and I am not happy with it, but for now it is all what I have..
myMc.gotoAndStop(2);
myMc.addEventListener(Event.ENTER_FRAME,mc_ef,false,0,true);
function mc_ef(e:Event):void {
if (e.target.loaded==null) {
e.target.loaded=true;
} else {
e.target.loaded=null;
creat_waypoints(par1,par2,par3);
e.target.removeEventListener(e.type,arguments.callee);
}
}
A simpler work-around would be including the "obstacles" and their dependencies in earlier frame where the depending logic is written. They can be included as "out-of-stage" invisible members.

Can two buttons with the same class instance act differently in Flash?

Say I have this specific button class in Flash called cont_button and it's supposed to be used to break out of a loop, but I want to use the class more than once. Is there a way to give every instance of this class some kind of parameter so that it knows which frame it nees to go to?
Example:
I have an instance of cont_button on frame 200 and there's a loop between 200 and 210. This cont_button executes a gotoAndPlay(211). But later on I have another instance of the button on frame 315 and a loop between 315 and 325. Is there a way to make it so each instance knows which frame it specifically needs to go to via the use of a variable? Or am I going to have to make an actionscript file for each individual one?
Pretty new to ActionScript so I appreciate the help and if there are good coding references to AS3 you guys recommend, I'll gladly look those over.
Sure, this is possible. One way you can do this is make the frame numbers class variables and when the button is clicked, they reference whatever value is stored in them, rather then hardcoded numbers. To get a better idea, can you post the relevant parts of your button class?
As you say, you need to pass a parameter to each instance of the button. There are lots of different ways you could do this, but I'd be tempted to just do it via the instance names.
You could name each button loopBreakTo211, loopBreakTo326 and so on, then in your button's class have:
var breakFrame:Number = Number(name.replace("loopBreakTo", ""));
(parent as MovieClip).gotoAndPlay(breakFrame);
Admittedly that's not a very robust way of doing it (for example, it will break if a button is named incorrectly and breakFrame ends up as NaN, so you might want to add a check for that), but it keeps the parameter together with the instance instead of in the timeline somewhere.

Adobe flash can't run code in a frame

Ok, I'm fairly new to Flash, and I was trying to code some ActionScript now for the last two days. I can't a simple piece of code to work:
stop();
So... pretty simple right? ;)
I have a 10 frame animation, and I'm trying to make the animation stop at frame 5. So I select frame 5 from my timeline and then I open the action menu and I simply write stop(); in the window. When I run it I get the following error message:
In ActionScript 3.0 code cannot be placed directly on objects. Please select an object or use the Code Snippet panel to apply code to the current selection on stage.
I totally understand this is a simple question and the answer might be obvious, but I can't find it...
Thanks
EDIT:
I tried debugging it, and it works when in the debugger but not when in flash...
You're putting the code "on" the object, not allowed in AS3 (thx god).
To avoid it, create a new layer and name it "code" (or whatever you want), and put the code in it