View Full Version : AS3, onLoad, EventListeners and DataGrids
Seb2812
August 21st, 2008, 11:12 AM
My greatest apologies for the following. Once upon a time I worked with AS1. That's some years ago and now I'm back in Flash. I bought the CS3 Premium (oh hell, I really bought it. 'twas pretty cheap, as I'm a student:hugegrin:), fired Flash up, typed the first coded, and got the message "Blabla isn't supported in AS3 anymore.".
Hell, AS3 is a whole new language!!!
In the past 3 days I tried diving into it, but, well... here we go.
What I want:
In the first frame of the main timeline should be an onLoad-action. How am I supposed to do some stuff only once if there's no onLoad-Function anymore?
In that onLoad is to be a DataGrid-Object (I've already noticed, that there's no EventListener for a changing variable).
Now follows XML-Import bla. Thanks to this site I've already managed that.
After that should an EventListener listen to a variable I change by a button.
Could anyone tell me:
a) How do I get around with a thing like an onLoad?
b) How do I listen to that variable? As I figured out I might have to use the DataGrid, but is that really neccesary?
Might be a tough first post. ;)
jwopitz
August 21st, 2008, 11:34 AM
I suggest you check out the Flex API Docs. Tho it will have loads more classes than you will be using, it still covers all of the AS3 classes supported by CS3. - http://livedocs.adobe.com/flex/3/langref/
AS3 makes alot more use of the event-driven nature of the Flash player. So to answer your question about the event and the Button, you should say something like:
myButton.addEventListener(MouseEvent.CLICK, myButton_clickHandler)
jwopitz
August 21st, 2008, 11:36 AM
I should say regarding the API Docs, any package that starts with flash.package.* is what you will be using. All of the packages that start with "mx.package.*" are Flex related.
Seb2812
August 21st, 2008, 12:00 PM
Already got the EventListener for the button.
Here's my code on that button:
function mouseUpHandler(e:MouseEvent):void {
trace(this.buttonID);
MovieClip(root).menuID = this.buttonID;
trace("root: "+MovieClip(root).menuID);
}
button.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
The button is build dynamically and got the var buttonID.
Question is, how do I track changes in MovieClip(root).menuID outside of that button?
Already tried to get the best out of these docs but their more confusing than helping.
jwopitz
August 21st, 2008, 12:56 PM
I don't quite follow what you mean by "how do I track changes" on root.menuID. Are you talking about how you might be able to access the movieClip from various points within the application that might not reside in the same scope?
Seb2812
August 21st, 2008, 04:33 PM
Nope, I've got something in mind like:
//goes in the main timeline
function doSomething ( e:Event ):void {
trace("Fifteen men on a dead man's chest, Yo ho ho and a bottle of rum");
}
var myWhatsoeverVar;
myWhatsoeverVar.addEventListener(Event.CHANGE , doSomething);The button is actually a MovieClip which contains a graphic, a DynamicText and a blind button.
//goes in the timeline where all that stuff is in
function myButtonAction (e:Event):void {
MovieClip(root).myWhatsoever = 1;
}
this.myButton.addEventListener( MouseEvent.MOUSE_UP , myButtonAction );Now, when I click on that button it should say "Fifteen men on a dead man's chest, Yo ho ho and a bottle of rum" but it doesn't.
I know that the CHANGE event is only for DataGrid and (as I think I read in that docs) XML. But is there a way to work around that? Or is there any mistake?
The second problem is, that I need to declare some vars etc, that I only want to be set ONCE.
So I'm looking for an equivalent for the AS1+2 onLoad method.
ActionScript3
August 21st, 2008, 06:45 PM
try this example
listening for changing a value.
package {
import flash.events.IEventDispatcher;
import flash.events.EventDispatcher;
import flash.events.Event;
public class AdvancedVar implements IEventDispatcher {
private var dispatcher:EventDispatcher;
private var _value:*;
public function AdvancedVar(value:* = undefined) {
_value = value;
dispatcher = new EventDispatcher(this);
}
public function set value (value:*):void {
_value = value;
dispatchEvent(new Event(Event.CHANGE));
}
public function get value ():* {
return _value = value;
}
public function toString():String {
if(_value != undefined)
return _value.toString();
return undefined;
}
// IEventDispatcher methods :
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void{
dispatcher.addEventListener(type, listener, useCapture, priority);
}
public function dispatchEvent(evt:Event):Boolean{
return dispatcher.dispatchEvent(evt);
}
public function hasEventListener(type:String):Boolean{
return dispatcher.hasEventListener(type);
}
public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void{
dispatcher.removeEventListener(type, listener, useCapture);
}
public function willTrigger(type:String):Boolean {
return dispatcher.willTrigger(type);
}
}
}
TEST :
var myWhatsoever:AdvancedVar = new AdvancedVar("AS3.0 Cool!");
myWhatsoever.addEventListener(Event.CHANGE, onChange)
function onChange(e:Event):void {
trace("the AdvancedVar is changed and the new value is " + myWhatsoever);
}
// on your button click, whatever..
myWhatsoever.value = 5;
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.