08-18-2012, 10:48 PM
|
#1
|
|
Registered User
Join Date: Aug 2012
Posts: 23
|
Calling a Variable from Movie Clips to Stage (created in Stage)
Hello everyone.
I'm having a great problem that's driving me crazy !!
Ok, what I want to do is modify a variable from a movieClip, create as public var in the main class.
First I want to tell you what I want to do and what I have done so far.
My goal is to create a MC curtain after a scene (kind a fade out) then loads the next scene and the same MC curtain comes up again (kind a fade in)
The thing is that I have a series of MCs create before (first an "intro" I removeChild it then addChild "MainMenu") Inside MainMenu I have some buttons. When I click the button, loads another MC (ex: MCInstructions) (that works great) Now I want that when I click MCInstructions or other MCs instide MainMenu, load the MCCurtain and when that curtain is above the scene, load the next MC (MCInstructions) then fade in.
So far I have been doing it with a MC Curtain, so:
- How can i do the curtain effect? What I tried is create a global variable from the main class and change it in different states of the curtain, like curtainState = down . Then in main class: if curtainState is down, load next MC. I haven't accomplish this because when I call the var from inside the MCCurtain (trace(MovieClip(root).curtainState )) it says:
TypeError: Error #1009: Can't access to a property or method of a reference of a null object.
at MCCurtain/frame1()
- I just thought, is there another way to do it? Like create a fade out effect of the actual MC then when that number is something like 0, load next MC with fade in.
Thanks so much , for real I have like 3 hours tryin' this.
Last edited by jeteran; 08-18-2012 at 10:50 PM.
|
|
|
08-18-2012, 11:37 PM
|
#2
|
|
DontJustEnjoyIt,SvEnjoyIt
Join Date: Sep 2005
Location: California
Posts: 1,388
|
Welcome to the forum.
Here's a very simple example of probably the easiest (but not the best practice) method
This is all on the main stage timeline, mc1, mc2, and mc3 are the movieclips that I want to display in order after the curtain fades in and out.
ActionScript Code:
import flash.display.MovieClip;
//Create an array of the movieclips we want to display.
//This isn't absolutely necessary, but makes this demonstration easier
var array:Array = new Array();
array.push(mc1);
array.push(mc2);
array.push(mc3);
for (var i:int=0; i<array.length; i++) {
array[i].visible = false; // Loop throug them and make them all invisible
}
var counter:int=-1; // start from -1 so that when counter++ gets called the first time it will be 0
var currMC:MovieClip; // used to hold a reference to the movieclip that is currently visible
addChild(curtain_mc); // This will bring the curtain to the top (z-index)
function showNextFrame():void {
if (currMC) { // This makes sure a movieclip exists
currMC.visible = false;
}
counter++;
if (counter > array.length-1) { //Check if counter is beyond our array's length
counter = 0; // if it is, reset it to 0
}
currMC = array[counter]; // set our currMC variable to point to the new movieclip
currMC.visible = true; // make it visible
curtain_mc.play(); // playt the curtain
}
Then I have a movieclip of a curtain tweening up and down. when it is down I have the following code:
ActionScript Code:
stop();
Object(this.parent).showNextFrame();
Object(this.parent) so that flash doesn't get angry that this.parent may or may not have a function named showNextFrame().
It calls the showNextFrame() function, which makes the current mc invisible and the next one visible.
Makes sense?
Last edited by svenjoypro; 08-18-2012 at 11:39 PM.
|
|
|
08-19-2012, 12:56 AM
|
#3
|
|
Registered User
Join Date: Aug 2012
Posts: 23
|
Hey svenjoypro, thanks so much for your message and welcome.
What you say makes perfect sense. But before I dig even more in your suggestions, I tried another thing; look at this:
Code:
package {
import flash.display.MovieClip;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
public class Tempo3 extends MovieClip {
public function Tempo3() {
var sceneIn = new Tween(tempo3,"alpha",Regular.easeIn,0,1,1,true);
sceneIn.addEventListener(TweenEvent.MOTION_FINISH, onFinish1);
function onFinish1(e:TweenEvent):void {
trace("load next scene");
var sceneOut = new Tween(tempo3,"alpha",Regular.easeOut,1,0,1,true);
sceneOut.addEventListener(TweenEvent.MOTION_FINISH, onFinish2);
}
function onFinish2(e:TweenEvent):void {
//delete both Tweens
}
}
}
}
What I did is create a MC that appears with a Tween, then I check when it finish, load the scene and then make it desappear again.
So far, this works perfectly and is tuned with my main objective (BUT if I can get it move from up to down (like a curtain) will be better)
The thing is, can I remove both as soon I finish loading them? In that case I will have to wrap it in a function right? (I want to stick to good programming practices)
The other thing is, instead of creating the same function over and over but with a different scene, can I have some sort of "event.currentTarget" that can read the scene I will load when the curtain is fadded in? Without of course making tons of fadeIn and fadeOut to each scene?
So far so good and I REALLY appreciate your guidance !!
|
|
|
08-19-2012, 05:31 AM
|
#4
|
|
Senior Member
Join Date: Dec 2011
Location: Tucson, AZ
Posts: 1,898
|
Declare your Tweens in the class block. (outside of any methods)
TweenMax / TimelineMax are a better choice for sophisticated Tween sequencing.
|
|
|
08-19-2012, 04:57 PM
|
#5
|
|
Registered User
Join Date: Aug 2012
Posts: 23
|
Thanks snick for your help.
Quote:
Originally Posted by [afz]snickelfitz
Declare your Tweens in the class block. (outside of any methods)
TweenMax / TimelineMax are a better choice for sophisticated Tween sequencing.
|
I tried to call them in the class block but, as soon as I create them, it activates. What can I do to activate it when I call it? I thought of creating 2 functions that includes one of them, what do you think?
Quote:
Originally Posted by [afz]snickelfitz
TweenMax / TimelineMax are a better choice for sophisticated Tween sequencing.
|
I googled it and looks fantastic, gonna give it a try.
Thanks once again !
|
|
|
08-19-2012, 05:17 PM
|
#6
|
|
Registered User
Join Date: Aug 2012
Posts: 23
|
Hey snick, look what I did:
Code:
package {
import flash.display.MovieClip;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
public class Tempo3 extends MovieClip {
public function Tempo3() {
var curtain:Curtain = new Curtain;
activateScene();
function activateScene() {
addChild(curtain);
var sceneIn = new Tween(curtain,"alpha",Regular.easeIn,0,1,1,true);
sceneIn.addEventListener(TweenEvent.MOTION_FINISH, curtainDown);
}
function curtainDown(e:TweenEvent):void {
trace("load next scene");
var sceneOut = new Tween(curtain,"alpha",Regular.easeOut,1,0,1,true);
sceneOut.addEventListener(TweenEvent.MOTION_FINISH, curtainUp);
}
function curtainUp(e:TweenEvent):void {
//delete curtain
trace(finish);
removeChild(curtain);
}
}
}
}
I'm calling the curtain from the library and making an instance in the code. Then I put both fades in different functions and, as soon as it finishes the animation, I kill curtain.
I believe this a good aproach of what i'm looking. The thing is:
- Are those funtions still in memory? I believe so because I'm just killing curtain and they are not related to the function. Is it possible to do it?
- With all this in mind, is it possible that the functions receives the name of the scenes I want to use for fadeIn and fadeOut?
So far so good, this is very similar to the disolve effect in PowerPoint.
Thanks once again snick !!
|
|
|
08-19-2012, 09:48 PM
|
#7
|
|
Senior Member
Join Date: Dec 2011
Location: Tucson, AZ
Posts: 1,898
|
Here's an example of declaring instances and Tweens in the class block.
This mitigates the common problem of GC sweeping the Tweens before they complete.
Not sure about your other questions.
How you handle these things (passing references into methods) depends on the overall structure of your project.
ActionScript Code:
package
{
import flash.display.MovieClip;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
public class Tempo3 extends MovieClip
{
private var curtain:Curtain;
private var sceneIn:Tween;
private var sceneOut:Tween;
public function Tempo3()
{
curtain = new Curtain();
addChild(curtain);
sceneIn = new Tween(curtain,"alpha",Regular.easeIn,0,1,1,true);
sceneIn.addEventListener(TweenEvent.MOTION_FINISH, curtainDown);
}
private function curtainDown(e:TweenEvent):void
{
trace("load next scene");
sceneOut = new Tween(curtain,"alpha",Regular.easeOut,1,0,1,true);
sceneOut.addEventListener(TweenEvent.MOTION_FINISH, curtainUp);
}
private function curtainUp(e:TweenEvent):void
{
trace("finish");
removeChild(curtain);
}
}
}
|
|
|
08-21-2012, 02:03 AM
|
#8
|
|
Registered User
Join Date: Aug 2012
Posts: 23
|
Hey snick, thanks so much for your reply.
Ok I did what you told me, but really I can't make it work !!
Look at this code:
Code:
package {
import flash.display.MovieClip;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
import flash.events.MouseEvent;
import flash.events.Event;
public class Tempo4 extends MovieClip {
private var curtain:Curtain;
private var sceneIn:Tween;
private var sceneOut:Tween;
public var scene1:Scene1 = new Scene1;
static public var curtainDownFlag:String = "no";
public function Tempo4() {
// constructor code
curtain = new Curtain();
function makeCurtain() {
addChild(curtain);
sceneIn = new Tween(curtain,"alpha",Regular.easeIn,0,1,0.5,true);
sceneIn.addEventListener(TweenEvent.MOTION_FINISH, curtainDown);
function curtainDown(e:TweenEvent)
{
curtainDownFlag = "yes";
sceneOut = new Tween(curtain,"alpha",Regular.easeOut,1,0,0.5,true);
sceneOut.addEventListener(TweenEvent.MOTION_FINISH, curtainUp);
}
function curtainUp(e:TweenEvent)
{
curtainDownFlag = "no";
}
trace(curtainDownFlag);
}
function detectClick(event:Event)
{
function squareActivate(event:MouseEvent)
{
makeCurtain();
if (curtainDownFlag == "no" ) trace("no");
else if (curtainDownFlag == "yes")
{
addChild(scene1);
}
}
square1.addEventListener(MouseEvent.CLICK, squareActivate);
}
addEventListener(Event.ENTER_FRAME, detectClick);
}
}
}
Crap I have been on this like 3 more hours, googling and learning stuff but I can't do it.
Everything runs great, but...
I have a flag that tells me when the curtain is down. Ok so when the function curtainDown runs, I change it to yes. If I trace it inside that function says yes, but outside says no.
I tried so many different ways, like calling another function that makes that flag change, calling it with MovieClip(root) and even create a new event when that function is activated, (and much much more) with no results.
Then I have a function that detects when the square is clicked, if so make a funtion that activates the square, runs the curtain but always the flag will be no.
Any idea of what is happening??
Thanks so much for your guidance, trust me, I really try to do it by myself, if I'm here because I', deeply stuck !!
|
|
|
08-21-2012, 03:17 AM
|
#9
|
|
Senior Member
Join Date: Dec 2011
Location: Tucson, AZ
Posts: 1,898
|
You have a curtain covering the stage.
You want to add content behind the curtain, then raise the curtain to reveal it when a button is clicked.
I see no need for the downflag thing. the motion complete does the job.
ActionScript Code:
package
{
import flash.display.*;
import fl.transitions.easing.*;
import fl.transitions.*;
import flash.events.*;
public class Tempo4 extends MovieClip
{
public var square1:MovieClip;
private var scene1:Scene1;
private var curtain:Curtain;
private var sceneIn:Tween;
private var sceneOut:Tween;
public function Tempo4()
{
scene1 = new Scene1();
curtain = new Curtain();
addChild(scene1);
addChild(curtain);
addChild(square1);
square1.addEventListener(MouseEvent.CLICK, squareClick);
}
private function squareClick(e:MouseEvent):void
{
sceneIn = new Tween(curtain,"alpha",Regular.easeIn,0,1,0.5,true);
sceneIn.addEventListener(TweenEvent.MOTION_FINISH, curtainDown);
}
private function curtainDown(e:TweenEvent):void
{
sceneOut = new Tween(curtain,"alpha",Regular.easeOut,1,0,0.5,true);
}
}
}
BTW, do not nest functions inside other functions.
Declare all instances in your code, even if they are named objects on the stage.
|
|
|
08-22-2012, 01:01 PM
|
#10
|
|
Registered User
Join Date: Aug 2012
Posts: 23
|
Snick, thank you SO much for your help.
Ok I understand your code, I made some adjustments and now it runs correctly.
What I did is that, in the squareClick function, i addChild the curtain, because if I add it before it, It will appear in the scene.
Then, in the curtainDown function, I addChild scene1 and set it's child index.
Like this works like a charm.
Now I have some questions, is this the right way to do it?
I noted that , after I click the square and everything works, I click again the square and nothing happends, why is that?
If I want to use the curtain again, what can I do?
Quote:
|
BTW, do not nest functions inside other functions.
|
This suggestions works for all the code we write?
Quote:
|
Declare all instances in your code, even if they are named objects on the stage.
|
This is a good one, why do I have to declare them again? (curiosity)
That's all for now, really hope you can help me with this Qs; and thanks once again for your time and guidance !!
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 12:59 AM.
///
|
|