flying star flash error message - actionscript-3

I'm trying to make flying stars in flash. I decided to combine two tutorials I found (http://www.lashf.com/page/basic/running_circles_effect and http://www.republicofcode.com/tutorials/flash/motion_guide_bc/), so I started first with this "running circles" tutorial. However, I got this error "Syntax error: expecting semicolon before leftbraces".
The code in question is
onClipEvent(enterFrame){
if(random(300) == 0) {
removeMovieClip(this)
};
};
I think the problem is because the actionscript from this tutorial is actionscript 2 while I'm working in actionscript 3. So how do I code it?

You should use addEventListener() to make your MC react on something that's happening in Flash. In order to perform actions on enter frame, add a listener to Event.ENTER_FRAME. The listener should be a named function that accepts one parameter of type Event or corresponding event type. Learn more here. Practically, wherever you see a onClipEvent(*) code, separate the code in brackets into a function, and use addEventListener() to attach a listener. Note, that in order to completely remove a MovieClip instance from anywhere, you should also remove attached event listeners, so write down what events does your MCs listen and use removeEventListener() where approppriate.

Related

AS3: How to dispatch function from class to mainframe

This might be a silly question. But how can I call a function (to execute) from a class to the timeline.
For example, I have the class "Test" and I want to execute the function "Next" on the timline (which it is only a function to show next slide).
Hope you understand what I'm trying to do.
Thank you!
The best practice for communication (in this scenario!) is to use Events.
The timeline create the object of your class Test and registers an
event listener.
The object of your class Test dispatch an Event.
The function that the timeline registered for that Event will be executed.
Please take a look at this question that wants to send additional information to the main timeline. In your case, you do not need a custom Event, because you do not want to send any information along. You only want to communicate the occurrence of the event. You can put that information into the type of the event. an example for a dispatch could look like this:
dispatchEvent(new Event("next"));
Creating a custom class allows you to put that String literal that describes the type into a constant, which prevents errors caused by accidentally misspelling the type. That might be a reason to create a custom Event class anyway, even only for the sake of a place to put those constants.
dispatchEvent(new PresentationEvent(PresentationEvent.NEXT));
Again, this would do the same as the previous line. this is also covered in the other question and the answer to it. Please take a look.

in what situation,gotoAndStop is not working

I have 3 frames, I don't show the whole code, it's too huge.and the main code is
gotoAndStop(2);
trace('frame:',currentFrame)
the output should be frame: 2
But in fact it's frame: 1, and objects in frame 2 cannot be loaded and become null
no compiler errors
When I delete some codes after it, the application sometimes operate right and stops at frame 2.
This shouldn't happen as any code after it should not involve the output
although I can solve this when I delete the first frame,but it's quite risky to keep developing.
Any ideas why this happen?
Before keep going,I should mention I actually have compiler errors due to null objects,but it's not the main point.
And I have a class called host,the code gotoAndStop is also in the first place of constructor.
I have put override function in host, and the output is
stopping at frame: 2 Called from: Error
at host/gotoAndStop()
at host()
frame: 1
TypeError: Error #1009: Cannot access a property or method of a null object reference
at host()
Then I tried the method2 Creative Magic says,the result shows
stopping at frame: 2 Called from: Error
at host/gotoAndStop()
at host()
displaying frame: 1
frame: 1
TypeError: Error #1009: Cannot access a property or method of a null object reference
at host()
displaying frame: 2
This quite confuses me what's happening in the frame,I wonder the cause is like you say old version of SDK,thanks for help
Your problem can be either you've written buggy code or you use old Flash SDK.
In the first case you should check if you're calling gotoAndStop(), play() or gotoAndPlay() methods of your MovieClip. To do so you can edit the class of your MC:
In Flash Professional in the library panel right-click on the MC
Select "Edit Class"
In your selected IDE add following function
override public function gotoAndStop(frame:Object, scene:String = null):void
{
trace("stopping at frame:", frame, "Called from:", new Error().getStackTrace());
super(this).gotoAndStop(frame, scene);
}
It will trace when the gotoAndStop() method was called and who called it. It will let you see if the frame was set to frame by something else. I recommend you to override play() and gotoAndPlay() functions as well.
If you're intimidated by this code you could just add a trace() on each frame of your MC, since it's only 3 frames it shouldn't be too much work. Your trace could look something like that:
trace("displaying frame:", this.currentFrame);
The other possible cause could lie within the SDK you're using. I, myself had a strange bug when the loaded MC wouldn't listen to stop(), gotoAndStop() and other functions. The problem was solved by upgrading the SDK: http://www.adobe.com/devnet/flex/flex-sdk-download.html
The explanation on how to replace the old SDK is there as well.

How to listen mxml event by using as3 class method?

I have components in mxml and simply I want to listen these component's events from custom class methods.
I tried these;
list.addEventListener(NativeDragEvent.NATIVE_DRAG_START, new CustomDragClass().DragStart);
error: incorrect number of arguments, Expected 1.
In this question, Amargosh says to use _customDragClass.DragStart but didn't worked, too. Same error
How can I assign class method as event listener method ?
Edit: This is a working programming. I fixed the issue by clean build project on freakin FlashBuilder IDE.

AS3 Event architecture

I'm having difficulty with the last piece in the puzzle on AS3 events.
I understand target classes inherit from EventDispatch or implement IEventDispatch and can register (amongst other methods) event listeners.
However what do the target classes register with? If an Event happens, how does AS3 know to pass the Event to the target classes?
Regards,
shwell.
Read this article about event phases and it will make more sense:
http://livedocs.adobe.com/flex/3/html/help.html?content=events_02.html
Hope this helps. Have a great day.
You can look at how starling event works
starling even dispatcher
When a displayObject bubbles an event, it will check if the parent of the displayObject exist and add the parent to bubbleList if exist util the ancestor of displayObject is null.
The following code is in starling eventDispatcher
var element:DisplayObject = this as DisplayObject;
var chain:Vector.<EventDispatcher> = new <EventDispatcher>[element];
while ((element = element.parent) != null)
chain[int(length++)] = element;
In AS3, EventDispatcher is an implementation of the observer design pattern. This class implement the addEventLister, removeEventListener, dispatchEvent' andhasEventListener` methods. Internally, it also maintains a dictionary or similar data structure that contains the events which are currently being listened for, and a list of methods which have to be called when the event is dispatched. Something like this -
{"event1": [method7, method5, method3], "event2": [method3, method2], "event3": [method1]};
When addEventListener is called on an object, it creates a new key for the event in question and adds the method reference to its associated value list.
When dispatchEvent is called on the class, it fetches all the methods associated with the event and calls the methods attached with it. Each method is called with an instance of the Event class or its subclasses.
Removing an event listener obviously does the opposite of what adding does.
I guess you're missing of addEventListener() mechanics. This thing has a global side effect on event engine, registering a callback function along with caller this value to provide correct context of a fired event, with possible update of event.localX and event.localY properties by calling globalToLocal() either statically or dynamically, as the event bubbles up and down.
If you are, like me, confused about how does Flash player determine the target of an event - there is an internal "focus" pointer that determines which component of the SWF has keyboard focus, and that one is used to target keyboard events. For mouse events, most likely Flash engine calls getObjectsUnderPoint() to query for topmost IEventDispatcher compatible objects (not all of the DisplayObjects can process events), and that one is sent a mouse event, with the previous event's target to receive a say MouseEvent.ROLL_OUT or MouseEvent.MOUSE_OUT if the target has been changed. For other events, most likely the entire display list can react.
For objects in the display list, the following excerpt from Adobe is the answer "When Adobe® Flash® Player dispatches an Event object, that Event object makes a roundtrip journey from the root of the display list to the target node, checking each node for registered listeners.".
For non display objects, AS3 run time maintains a dictionary of all AS3 events containing bound variables. The bound variables are a reference to the event listeners.

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.