Using stop in action script 3.0 for two objects - actionscript-3

I have an instance of Gear and one of bLine. When the Gear gets on the bLine, they both start moving simultaneously. I am trying to stop both of them at one frame, I don't want the objects to go off the stage. Since my gear's move was created through a motion tween and my bLine moves through a function, I am not sure how that can be accomplished.
Code inside of the Gear instance:
var ev2:Event = new Event("transfer");
dispatchEvent(ev2);
Code inside of bLine:
import flash.events.Event;
this.addEventListener(Event.ENTER_FRAME,Move);
function Move(e:Event):void {
this.x=this.x+3;
};
Main timeline:
gear.addEventListener("transfer",transferGear);
function transferGear(e:Event) {
bLine.gotoAndPlay(2);
};
I don't know if there is a way how I could stop the bLine inside of the Gear instance, in order to be able to stop both objects at the same time.

Related

On the use of frames and functions in Flash AS3

My question is, in a flash game I use different frames for levels. And I am confused on how functions work for this. My first frame works fine but I need help with using functions on other frames and keeping functions specific to one frame. Any help is appreciated, just a quick simple explanation
You cannot associate a function with a specific frame.
When you execute code on a frame, what actually happens is the MovieClip containing that frame will call a function called addFrameScript(), passing to it a representation of the code you write on the frame.
This means:
Until you visit a frame that defines a function, you cannot call said function.
Once you have visited a frame that defines a function, that function is attached to the parent MovieClip, and you are able to call the function at any point in the timeline that is earlier or later.
If you try to define a new function with the same name on a different frame, you will get a 1021: Duplicate function definition error.
Instead of making a new function for every frame or level, you should make a single function that is able to handle different information that is representative of a level, e.g.
function loadLevel(level:int):void
{
// Do stuff with the value of level.
// For example, this function might look at a data source that maps
// the level numbers to some level data representing tile placement.
}
This could be defined on the first frame, then on each subsequent frame:
loadLevel(1); // Frame 2
loadLevel(2); // Frame 3
// ...etc
All of this of course is not ideal and could be done better avoiding the timeline and instead using to OOP paradigm that AS3 provides.
I have found using multiple levels within the same scene causes no end to headaches for me.
I'm no expert & i'm sure its possible to do so in many cases and I have seen some great games created using 1 frame 1 scene & all code in an external .as.
However I find it much easier myself to just use 1 frame per level and put each level in a different Scene.

as3 symbol variables not initialized yet

I'm initializing symbols in my timeline, and trying to access the variables within those symbols, but they return 0 or undefined even though I set the variables in the symbol's timeline. For some reason the variables haven't been set yet, though the main timeline can see that they exist. How do I make the program wait until the variables have been set?
Best practice to work with classes, not coding in timeline and frames of MovieClip.
I assume you have MovieClip from designer and you want inject some logic to the specific frame. There are many options.
Events
You can trigger event in the specific frame, and you work in normal way (with classes and class members).
//Frame code
import flash.events.Event;
this.dispatchEvent(new Event("IntroDidFinish", true, true));
stop();
//Somewhere in class
myContainer.addEventListener("IntroDidFinish", onIntroFinish, false, 0, true);
function onIntroFinish(e: Event):void{
//Do your stuff
}
Events help you decouple logic from the design(predefined complex MovieClip, etc.)
Waiting for initialisation
As MovieClip reaches some frame, you should wait extra time for initialisation. Thats why 99.9% of AS3 developers don't like MovieClip as holder for any critical data or logic. It means if you call myMovieClip.goToAndStop(8); you can't get myMovieClip.someValue declared in 8 frame after goTo operation. If you still want to go with such approach, easiest solution for you will be Event.ENTER_FRAME, after goTo subscribe for ENTER_FRAME event, for only one update, and do your work ;)

AS3 Require Scope Guidance Please ?

Thanks for taking the time to read... here is my question/scenario, its a quick one:
I have:
Stage -> SWF Loader Root -> SWF Loader -> MovieClip -> Nested MC
From within "Nested MC": I can only access "SWF loaders root" time line, I can't seem to get access to the stage's functions...
Within "Nested MC" I used:
this.parent <- shows "MovieClip"
this.parent.parent <- shows "SWF Loader"
this.parent.parent.parent <- shows "SWF Loader Root"
this.parent.parent.parent.parent <- SHOWS NULL!!!!
Im trying to call on a function which resides on the main time line.
Is there any way to access the main timeline?
Any suggestions will be greatly appreciated.
Am I missing something trivial? Im learning
Sam
You probably want to dispatch an event from your nested MC then listen for the event from the main timeline. Sounds like you're a few layers deep in the display, so make sure you set "bubbles" to true.
From nested MC:
dispatchEvent(new Event("your_custom_event_name", true));
Then on the main timeline:
addEventListener("your_custom_event_name", customEventHandler);
function customEventHandler(e:Event):void {
mainTimelineFunction();
}
function mainTimelineFunction():void {
trace("success");
}

ActionScript-3 timing issue / unwanted background multi-threading

I created a new class with private vars and public get properties.
When I create a new instance of the class, it loads text files content to the private vars - it probably takes a bit of time to load it.
After the new instance created, I try to get the value of the private var with the get property:
var item1:MyItem = new MyItem("0001");
trace(item1.ItemName);
Well, The output is blank.
The string that ItemName points to is not undefined, it contains data.
So, it's like a timing issue, and ActionScript is probably running the code using background multi-threading, so it's calling the trace command before it finished to run all methods in the MyItem c'tor (methods that load the text file data into the String var that ItemName points to).
Is there any way to force ActionScript avoid using this unwanted "background multi-threading", and run the code normally (by the order of the commands)?
I mean like "Don't run the 2nd command until you finished running the 1st one".
Thanks for any help..
Freddy
It's not multi-threading, loading a text file externally is just a completely asynchronous operation. Within your MyItem class, you need to have an Event.COMPLETE handler for that Loader. From there, there here is what I'd do:
Option 1: In MyItem's COMPLETE handler for the file load, set a flag. The class that uses the getter must check the flag via another getter to see if the data is there, and either use it (if it's there) or set up a listener to wait for it (if it's not). Once the data is loaded, you have immediate synchronous access to it (it's cached).
Option 2: An alternative to keeping the "isLoaded" flag within MyItem would be to have MyItem's Loader COMPLETE handler dispatch it's own TEXT_LOADED event. Then, the instance responsible for creating the MyItem instance would listen for that, and know not to ask for the contents of that text file until it was there.
Either approach will work. It is up to you to figure out which one makes sense. My first option potentially avoids an unnecessary listener if you don't expect to access the loaded data right away (i.e. you're loading the text at startup for use sometime later). The second approach makes sense if you do expect to make use of the text file's data the instant it loads.
Ideally what you'd want to do is implement the Observer Pattern:
Add an event handler to your MyItem class for the Loaded event of whatever component/class you're using to load your XML.
Add an event to your MyItem class that will bubble the Loaded event from Step 1 so that clients of your MyItem class can attach a handler to it. Fire this event inside the Loaded handler you created in Step 1.
Attach an event handler to the event you created in Step 2 and place your trace code in that handler.
Your code is in fact running in a single thread, however the typical data loading classes in Actionscript are asynchronous. It is likely that the data has not loaded before you call trace. To be notified of when the data is loaded you should use an event listener on the loading object.
var loader:URLLoader = new URLLoader();
loader.addEventListener( Event.COMPLETE, onLoadComplete );
loader.load( new URLRequest( 'path_to_data.xml' ) );
function onLoadComplete( event:Event ):void
{
trace( loader.data );
}
The AS3 API docs have a more complete example.

Getting a null object reference error for a button instance

I am trying to use this script to jump to a certain point on my timeline:
feature1_btn.addEventListener(MouseEvent.CLICK, feature1);
function feature1(event:MouseEvent):void {
gotoAndPlay(620);
}
I have the instance of my button labeled as "feature1_btn". Why am I getting this error?
You're likely getting this error because you're attempting to add the listener to the instance before the instance has been created and/or initialized. If this code is in your timeline you need to make sure that feature1_btn exists on the stage at that point in the timeline.