![]() |
Those are in the old XML Class (now called XMLDocument). In AS3 you should be using the new E4X XML class (XML). See:
XML vs. XMLDocument There are links to the APIs for each there. |
Sorry thats what I meant, in e4x is there an alternative for previousSibling and nextSibling? I looked in the flex help files but couldn't find one... Already checked the language reference I was just hoping maybe you know of a simple alternative?
|
no, but its not hard to do manually using parent() children() and childIndex() +/- 1 ;)
|
Thanks :D Il check it out.
Edit: Shouldn't this thread be stickied? |
(Didn't wan't to make a new thread...)
In flex, how do you repeatadely load XML at runtime? Using the HTTPService component loading it is fine, but if the XML is edited whilst the swf is running, the component doesn't load the new XML, instead it loads the XML that was originally loaded. Does that make sense? |
I forgot about kirupa forms and searching else where about actionscript3 knowledge. I am so happy to see thse tips and thanks a lot for providing this support to flash lovers.
|
STICKY!
|
just wondering...
Why would you need parent child siblings functions in AS3? |
Quote:
|
sorry forgot to quote.
Quote:
|
Just experimenting with loading XML in flex, and making a previous and next buttons. It would be easier using the previous and next sibling functions but doing it by hand isn't that difficult. Still, I thought AS3 was suppose to be simpler?
|
no i thought it was supposed to be more powerful, and more familiar to other languages...
and you can handle XML like arrays now (which is so much better for parsing data) cuz now they use XMLList, instead of childNode. I never really liked ActionScript Code:
child = child.nextSibling(); if (child != null) { trace(blah); } |
From Actionscript 3.0 Overview:
Quote:
|
How do you create a tiled background in as3? I tried converting a code I used for a old project, but then I came into a few problems and now I'm just confused... The biggest problem probably being that I couldn't use beginBitmapFill. Any ideas? Thanks. (sorry to be asking so many questions)
|
Pretty well the same as AS2:
ActionScript Code:
import flash.display.BitmapData; import flash.geom.Rectangle; var square:Rectangle = new Rectangle(0, 0, 10, 10); var tile:BitmapData = new BitmapData(20, 20, false, 0xFFFFFF); tile.fillRect(square, 0xFF0000); square.x = 10; square.y = 10; tile.fillRect(square, 0xFF0000); this.graphics.beginBitmapFill(tile); this.graphics.lineTo(this.stage.stageWidth, 0); this.graphics.lineTo(this.stage.stageWidth, this.stage.stageHeight); this.graphics.lineTo(0, this.stage.stageHeight); this.graphics.lineTo(0, 0); this.graphics.endFill(); |
Thanks :P But how would you do that with a bitmap from the library/ dynamic bitmap? Flash 9 doesm't let you set linkage Id's, so how would you connect to them?
|
In AS3, you must give objects in the library class association in order to instantiate them with AS. If you don't have a class for your bitmap (ie you don't need to extends BitmapData with more methods) then you can type in a class name and Flash will automatically create one for you which will extend BitmapData. Than you just call that classes constructor, assign the instance to a variable and tile it:
![]() ActionScript Code:
import flash.display.BitmapData; var tile:BitmapData = new MyImage(); this.graphics.beginBitmapFill(tile); this.graphics.lineTo(this.stage.stageWidth, 0); this.graphics.lineTo(this.stage.stageWidth, this.stage.stageHeight); this.graphics.lineTo(0, this.stage.stageHeight); this.graphics.lineTo(0, 0); this.graphics.endFill(); You really should ask these questions in a seperate thread :) |
Thanks! Since they were pritty simple questions, didn't wan't to make a new thread. But Il remember that next time.
|
Glad to help :hoser:
|
Quote:
|
nope.
Those functions have a built in endFill(). |
Didn't think so.
(Maybe Senocular should edit his post :P) |
No, they don't call endFill, so it is necessary to end the filling operation. Otherwise it will keep filling and you will get something like this if you keep drawing:
ActionScript Code:
// draw a blue rounded rectangle: var square:Sprite = new Sprite(); square.graphics.beginFill(0xFF); square.graphics.drawRoundRect(0, 0, 100, 50, 10, 10); square.graphics.lineTo(550, 400); square.graphics.lineTo(550, 0); addChild(square); |
^
Actually this is directly from the flash.display.graphics (from the Flex 2 Language Reference). Quote:
|
Well the example above is evidence to the contrary, it's certainly not the first time the Adobe LR has been wrong.
|
It would be kind of silly if any of those methods called endFill anyway. If you're allowed to draw with the other drawing methods after calling beginFill and before calling a convenience method, it wouldn't make sense to essentially restrict you from drawing after that without calling beginFill again.
|
lots of booboos. jeez...
|
In reference to bitmap from library tip();;
I understand your methods here, but can this be aplied to a movieclip with a time line in the library. I have an animation of a bird flying that had to be built in Flash authoring environment and I want this to be used on call within one of my classes. can I call this in the same way. Oh and it has already been set to a class so how do I reference that class do I target it through the full package path or just simply the class name? Cheers, Skin |
Senocular, in your post about creating custom events you used this:
public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void { eventDispatcher.addEventListener.apply(null, arguments); } I was wondering what is the apply() for? What does it do? Thanks, |
apply
http://livedocs.macromedia.com/flex/...on.html#apply() It lets you call a function with an array as its arguments instead of making you write them as a comma delimited list. This makes it much easier to work with functions that can take any number of arguments and you have values to be sent to that function as an array in the addEventListener example, I just used it to make it easier to transfer all of the arguments from the class's addEventListener to the addEventListener method in the eventDispatcher object. |
Quote:
|
How I got around this
Quote:
This is the WRONG way: <mx:Script source="MyClass.as"/> By changing it to this way the problem was fixed: <mx:Script> <![CDATA[ import MyClass; // then instantiate the class as usual, but make sure there is nothing // in it's constructor that will do anything visual, as the app has not yet // finished being created. private var mc:MyClass = new MyClass; ]]> </mx:Script> |
Quote:
|
Quote:
|
Overriding clone in Events
When creating custom events, you should always override the clone() method. Though this is not necessary in all situations, it is best practice and will save you from errors in the situations where it is.
Why do you need to override clone? Well, the clone method creates a copy of your event (or object - whatever object has the clone event; this isn't limited to Event objects). The default clone method inherited by the Event class or whatever class your custom class extends, will return an event object of the type of that class, not your custom event subclass. In the situations where a clone is needed, it is needed to be of the same type of your class, not the class it extends. When is clone needed? Clone is needed whenever you re-dispatch an already targeted event. Whenever you create a new event instance, that event instance has a null target. When it is eventually dispatched, that target is assigned to be whatever object did the dispatching. Should that, now targeted, event ever be re-dispatched or dispatched a second time, a clone of that event is created and the new target assigned to the clone. It is at this point when the overridden clone method is needed, otherwise an error will occur when you have a type mismatch. Note: when extending clone(), be sure to use the same method signature as defined by the Event class. This means the return value of clone must be Event, not your custom class. Because your custom class inherits from event, the type is still correct, but the actual instance returned will be, not an Event instance specifically, but an instance of your custom event. ActionScript Code:
package { import flash.events.Event; public class CustomEvent extends Event { public function CustomEvent(type:String, bubbles:Boolean, cancelable:Boolean) { super(type, bubbles, cancelable); } // override clone so the event can be redispatched public override function clone():Event { return new CustomEvent(type, bubbles, cancelable); } } } |
Class member enumeration
Enumeration (the ability to cycle through an object's members in a loop) in ActionScript 3 now only works with dynamic definitions in dynamic classes. Non-dynamic classes do not provide enumerable properties or methods. The following class, for example, has no enumerable members that would be recognized in a for..in loop.
ActionScript Code:
package { public class EnumerateClass { public var variable:String = "value"; public function method():void {} } } ActionScript Code:
var example:EnumerateClass = new EnumerateClass(); for (var key:String in example) { trace(key + ": " + example[key]); // no output } Even when dynamic, the above class will not have enumerable members because the variable and method definitions are themselves not dynamic. There is a setPropertyIsEnumerable method located in the Object class, but it too only works with dynamic properties. Consider the following amended class definition. ActionScript Code:
package { public dynamic class EnumerateClass { public var variable:String = "value"; public function method():void {} public function EnumerateClass(){ this.dynamicVar = 1; this.dynamicVar2 = 2; this.setPropertyIsEnumerable("dynamicVar2", false); } } } This class is now dynamic and has 2 dynamic properties, dynamicVar and dynamicVar2. As dynamic properties these would show up in for..in loop enumeration. The use of setPropertyIsEnumerable, however, prevents dynamicVar2 from showing up. ActionScript Code:
var example:EnumerateClass = new EnumerateClass(); for (var key:String in example) { trace(key + ": " + example[key]); // dynamicVar: 1 } Using Object.propertyIsEnumerable() you can test to see whether or not a property is enumerable for a class instance. ActionScript Code:
trace(example.propertyIsEnumerable("variable")); // false trace(example.propertyIsEnumerable("dynamicVar")); // true trace(example.propertyIsEnumerable("dynamicVar2")); // false |
Flash 9: Timeline navigation and code execution
In ActionScript 1 and 2, when you navigated between multiple frames during the same frame of execution, the code on each one of those frames would be executed before the frame completed in Flash. For example, consider a movie clip named "tracer" of three frames stopped on frame 1 and resting on the main timeline. In frame 2 of that movie clip is a trace statement tracing "frame 2". Similarly, in frame 3 there is a trace statement tracing "frame 3". The following code on the main timeline would cause both trace statements to run.
ActionScript Code:
tracer.gotoAndStop(2); tracer.gotoAndStop(3); In ActionScript 3, the Flash player waits until the screen is about to be rendered and at that point in time runs the frame script of the current frame and only the current frame, despite what other frames may have been traversed within the actions occuring during that frame. Note: You may find that scripts unexpectedly run more than once per frame if navigating across multiple frames during one frame cycle. If possible, you should not try to navigate more than one frame at a time. |
Flash 9: addFrameScript
When you publish an ActionScript 3 movie from the Flash Authoring environment, Flash will automatically convert timelines with scripts written in them into classes with a definitions based on the timeline scripts used. Variables and functions get converted into class members and frame scripts specific to frames become methods to be called on those frames. But how does Flash make that association? This is made by an undocumented addFrameScript() method. This method takes a collection of frame (zero-based) - method pairs that associates a method with a frame on the timeline. When that frame on the timeline is reached, the method is called.
ActionScript Code:
MovieClip.addFrameScript(frame:int, method:Function, [frame:int, method:Function...]):void; The following example has two methods, onFrame3 and onFrame6 which gets called on frames 3 and 6 respectively (note that they are added by addFrameScript as frames 2 and 5 since the frame value there is 0-based). ActionScript Code:
package { import flash.display.MovieClip; public class DocumentClass extends MovieClip { public function DocumentClass(){ addFrameScript(2, onFrame3, 5, onFrame3); } public function onFrame3():void { trace("frame 3: "+currentFrame); } public function onFrame6():void { trace("frame 6: "+currentFrame); } } } Only one method can be associated with any one frame. If you ever need to remove a frame, use null as the frame method ActionScript Code:
addFrameScript(2, null); // removes frame 3 script |
Key.isDown in AS3
In ActionScript 2, you could use Key.isDown(keyCode) to determine whether or not a key was being pressed on the keyboard. This command is no longer available in ActionScript 3. In fact, there is no Key object in ActionScript 3. You can still access keyCodes by name, but through the Keyboard (flash.ui.Keyboard) class. For key recognition in ActionScript 3, you have to use keyboard events or, more specifically the KeyboardEvent.KEY_DOWN and KeyboardEvent.KEY_UP events. When a key is pressed on the keyboard, the KeyboardEvent.KEY_DOWN is dispatched and you can tell which key was pressed by checking KeyboardEvent.keyCode in the event handler. Same applies to keys being released in the KeyboardEvent.KEY_UP.
Using these events, you could recreate the functionality provided by the Key class in ActionScript 2. However, you should know that the KeyboardEvents only work when the Flash player has focus. This means a key could be pressed in Flash and released outside of Flash without Flash knowing that it was released (not having received the KEY_UP event). As a result, you should assume all keys released upon deactivation (loss of focus) of the Flash player. The following recreation of the Key class takes that into consideration. ActionScript Code:
package { import flash.display.Stage; import flash.events.Event; import flash.events.KeyboardEvent; /** * The Key class recreates functionality of * Key.isDown of ActionScript 1 and 2. Before using * Key.isDown, you first need to initialize the * Key class with a reference to the stage using * its Key.initialize() method. For key * codes use the flash.ui.Keyboard class. * * Usage: * Key.initialize(stage); * if (Key.isDown(Keyboard.LEFT)) { * // Left key is being pressed * } */ public class Key { private static var initialized:Boolean = false; // marks whether or not the class has been initialized private static var keysDown:Object = new Object(); // stores key codes of all keys pressed /** * Initializes the key class creating assigning event * handlers to capture necessary key events from the stage */ public static function initialize(stage:Stage) { if (!initialized) { // assign listeners for key presses and deactivation of the player stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed); stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased); stage.addEventListener(Event.DEACTIVATE, clearKeys); // mark initialization as true so redundant // calls do not reassign the event handlers initialized = true; } } /** * Returns true or false if the key represented by the * keyCode passed is being pressed */ public static function isDown(keyCode:uint):Boolean { if (!initialized) { // throw an error if isDown is used // prior to Key class initialization throw new Error("Key class has yet been initialized."); } return Boolean(keyCode in keysDown); } /** * Event handler for capturing keys being pressed */ private static function keyPressed(event:KeyboardEvent):void { // create a property in keysDown with the name of the keyCode keysDown[event.keyCode] = true; } /** * Event handler for capturing keys being released */ private static function keyReleased(event:KeyboardEvent):void { if (event.keyCode in keysDown) { // delete the property in keysDown if it exists delete keysDown[event.keyCode]; } } /** * Event handler for Flash Player deactivation */ private static function clearKeys(event:Event):void { // clear all keys in keysDown since the player cannot // detect keys being pressed or released when not focused keysDown = new Object(); } } } |
Scale and Alpha Ranges
One thing to be weary of, especially when trying to port older code to ActionScript 3 is that the ranges for many popular properties have changed. A few properties that, before, may have had ranges of 0 to 100 are now 0 to 1. Here is a couple of properties that changed; be on a look out for others:
Code:
ActionScript 2.0 | ActionScript 3.0 |
| All times are GMT -7. The time now is 10:35 AM. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2007, Jelsoft Enterprises Ltd.
Copyright 2004 - kirupa.com