Switching flash scenes within a MovieClip - actionscript-3

How do I switch scenes within a flash MovieClip object with Flash CS5 and ActionScript 3?

Hi Dani,
First of all, coding within MovieClip objects in AS3 is not recommended AND using scenes are not recommended.
Why?
Coding within MovieClip objects can be tempting if you are a beginner and this is alright.But if you are serious about the reusability of your assets and your code, you may want to dissociate your visual from your logic.I'm suggesting you write your logicFrom the main timelineFrom external classes (and using OOP)This is great!
Scenes are bad because of the timeline and code issues.If some of your code is in a scene, it may be not accessible to the other scenes.
Enough talking, here's the help you need.
How do I switch scenes within a Flash MovieClip object
This code is intended to be in a MovieClip frame
// === Let's put the stage in a variable (cleaner) ===
var main:MovieClip = this.parent as MovieClip;
// this.parent will return the DisplayObject of parent the current clip.
// You need to cast [... as MovieClip] to not cause errors because Flash
// thinks it is only a DisplayObject
// === Here's the interresting part ===
main.gotoAndPlay(0, "Scene 2");
// We tell the main timeline to go to frame 0 in the "Scene 2"
// Be cautious, it must be spelled exactly as displayed in Flash (IDE)
Don't forget: Deeper is your clip (Embeded multiple times in a clip), more "parent" will you need.
var main:MovieClip = this.parent.parent as MovieClip;
// If your object is inside a MovieClip who is itself in a MovieClip
// Tip: How much time you need to push the Back button to go to the timeline
// is the number of parents you need to write.
Hoping this help. If you have any questions, just comment this answer!

Related

How do you move to another scene in actionscript 3 using buttons

I'm trying to use a button to skip from one scene to the first frame from the next scene in Flash CS6, however I keep getting the
error 2108: the scene was not found.
stop();
btnNext.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextScene);
function fl_ClickToGoToNextScene(event:MouseEvent):void {
gotoAndPlay(1, 'scene2');
}
This is my code and ''scene2'' is spelled right, how can I correct this?
Checking the API for gotoAndPlay(), it is an overloaded argument which accepts either a String for the label, or an int for the frame. Furthermore, if a second argument is provided, it points to the frame number in the target scene. A Scene should not be confused with a frame Label. Scenes are like multiple MovieClip timelines, each with their own first frames. According to Adobe,
Using scenes is not the best approach because of a number of drawbacks...
If you are using the automatically generated names, then it should be Scene 2, and not scene2. Furthermore, although your command will work, it will appear to have done nothing unless you also add stop() to the first frame of your new scene.
Alternatively, you could just switch to the gotoAndStop() which won't cause your playhead to jump back to the first scene.
gotoAndStop(1, "Scene 2");

How to make a movieclip play on a specific frame in AS3?

I'm incredibly rusty at Flash having not touched it in probably 10 years and can't seem to figure this out, or find it online:
I have a MovieClip with two layers, each having a Shape Tween. Basically its a Door that opens and closes.
I dropped it onto the main timeline but now I need it to start and stop. This is where I'm now struggling since the last time I used Flash actions could go on specific keyframes.
I made a new layer called actions just to keep things organized and currently have:
barrier1.stop();
I just want something that lets me state a frame, say 57 to have barrier1 start playing on. Tried using play(); and Event.ENTER_FRAME with no luck. How would I set this up?
Well it is easy with the instance name of your movieClip
barrier1.stop(); // Stops the movieClip
barrier1.play(); // Resumes
barrier1.gotoAndStop(12) // Goes to 12nd frame and stop
barrier1.gotoAndPlay(12) // Goes to 12nd frame and play
barrier1.currentFrame // returns barrier currentframe
For capturing frame from scene level:
this.addEventListener(Event.ENTER_FRAME,onLoop);
function onLoop(event:Event){
if(barrier1.currentFrame == 57){
trace("BARRIER is in 57. frame");
}
}
Inside on the animation clip on the first frame
var root:MovieClip = this.parent as MovieClip
root.makeStartSceneAnimation()
**in timeline scene level [root]**
function makeStartSceneAnimation(){
/// barrier started to play
}
If you are using timeline, you can add Key frame on the desired frame, and then add stop(); as Action in the action layer. But bear in mind that if you do this in the main timeline - it will stop everything. If you want to stop that MovieClip, then you have to do this inside MoviceClip's timeline.

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
}
}

In flash with as3.0, I have to call a function on the main stage from a movieClip

I have to call a function that is defined on the main stage of my project, and I have to call it from a MovieClip, what can I do?
I'm using flash cs5.5 and AS3.0
There are multiple ways to access the MainTimeline from objects on the stage. Probably the most reliable is 'root', but there is also 'parent' (but only if you MovieClip is a direct child of the main timeline).
// root should always work
Object(root).myFunc();
// parent will only work if your movieclip is a direct child of the main timeline
Object(parent).myFunc();
Note that you have to cast these are generic Objects (or MovieClip would work) because they return typed classes that don't have a 'myFunc' function in them.
You'll need this code on your main timeline:
function myFunc():void {
trace("My function got called!");
}
When I read your question it sounds as though you have a function defined in an action frame of your main timeline.
My answer may be out of reach for your current project, and ToddBFisher's answer is perfectly right. That said - I'm going to answer the question differently.
Instead of defining a function on the main timeline, set up a document class, define your functions there, and access the class's functions in your code. Keep as much code off your timelines as possible.
Downloadable files for Document Class example: http://www.isgoodstuff.com/2008/06/06/actionscript-30-documentclass-in-plain-english/
Setting up an AS3 class: http://www.adobe.com/devnet/flash/quickstart/creating_class_as3.html
Assuming your movie clip is a direct child of your main stage, in you movie clip you can do:
MovieClip(parent).theFunctionToCall();
if your MovieClip has a class, just add it to your main class using like:
var m:MovieClip = new MovieClip();
**addChild(m);
then you can get access into it's public function like typing:
m."functon name";

hitesting the weapons

i have a movieclip on my stage called dude inside the moviclip is a frame with a movie clip called axeframe with yet another movie clip called axe. what i want to do is make a hittest in the axeframe a so that when the axe (only axe not the character) hits an enemy(named enemy) on the stage he will disappear. this is my code:
addEventListener(Event.ENTER_FRAME, axehit);
function axehit(event:Event):void {
if (axe.hitTestObject(enemy)) {
removeChild(enemy.stage)
}
}
it gives me this error
1120: Access of undefined property enemy.if (axe.hitTestObject(enemy)) {
1120: Access of undefined property enemy.removeChild(enemy.stage)
You can't just reference to enemy without any further specification (it will assume enemy is a child of the movieclip in which you placed the code. Try stage.enemy instead, and this, or this.parent instead of axe.
(Assuming enemy is a movieclip on the stage, and the code you posted is inside axe)
Also, you should change removeChild(enemy.stage) to stage.removeChild(stage.enemy), and you should probably look into variable scopes.
Edit: Nope.
Sorry for that, just pretend you didn't read that (Forgot you can't just reference objects through Stage)
To be completely honest with you, this is the way I started as well but it's not the proper approach to flash coding. Firstly, you should try to keep all your code on the main timeline, and not in separate movieclips, in order for it to work together better. Once you get the hang of it, you should check out Object Oriented programming as well. It really increases the workflow and enables you to create bigger and way more complicated scripts.
more edits:
So to put this on the main timeline, would require something such as:
stage.addEventListener(Event.ENTER_FRAME, loop);
function loop(event:Event):void {
if (axe.hitTestObject(enemy)) {
this.removeChild(enemy)
}
}
Which is cleaner and more readable (and easier to find) as well. (Assuming axe and enemy are movieclips on the main stage)