Показаны сообщения с ярлыком ActionSctipt. Показать все сообщения
Показаны сообщения с ярлыком ActionSctipt. Показать все сообщения

суббота, 28 августа 2010 г.

Creating a simple image gallery with the Flex HorizontalList control



This request comes to us from a faithful reader who was wondering how you can display a full image when a user clicks on a thumbnail in a HorizontalList control in Flex. My solution was to put the thumbnail image and full image URLs in the data provider and then use bindings to the HorizontalList control’s selectedItem property. 


http://blog.flexexamples.com/2008/02/15/creating-a-simple-image-gallery-with-the-flex-horizontallist-control/

суббота, 21 августа 2010 г.

Actionscript 3.0 type casting and type checking



Type casting and type checking have always been central to any computer programming language, especially in Object Oriented languages like Actionscript.
In fact, knowing what type of object you are receiving as a parameter or the ability to "transform" your object into another type so that you can pass it along to method calls and class properties are key to one of the most important concepts of Object Orientation:Polymorphism.
According to this principle, any object that extends from a certain hierarchical line (class inheritance or interface implementation) can be used in substitution of any of its ancestors. This results from the fact that descendants have the exact same external programmable interface as their ancestors (either implicitly by means of class inheritance, or by mandatory interface implementation).
Automatic type casting
Sometimes type casting occurs automatically. For instance if you set a property typed asObject with a DisplayObject (which is an Object's descendant), Flash automatically casts that DisplayObject to Object. In other words, whenever you use a descendant in place of one of its ancestors, it automatically is cast to that ancestor's type.
This doesn't mean, however, that the object ceased to be an instance of the descendant type. Only that, in that particular reference in a class property or method argument, it is known as being of the ancestor type. And it will carry that type whenever it is accessed through that property or argument. In other words, what determines the type of the object you are accessing is not the instance creation type, but the type of the property or parameter you're using to access it.
Explicit type casting
Since the object instance doesn't loose its creation type when being cast to an ancestor type, it may still be used whenever that descendant type is required. This is where it gets interesting: If you are trying to set a property of the descendant type using an instance created with that same type but stored in a variable of one of its ancestor types, Flash will complain about type incompatibility.
In order to be able to do this you'll have to explicitly cast the instance back to the descendant type. It sounds somewhat redundant that you have to cast an object to a type that you know it already is, but if you remember that what determines the type is not the instance but the variable used to access it, it makes perfect sense.
Actionscript provides two different techniques for type casting and one for type checking. For type casting your either use the cast operation or the as operator.
The cast operation
Performing a cast operation is very simple. You just have to surround the variable with parenthesis and prepend it with the class into which it's going to be cast.
var receivingVariable:DisplayObject = DisplayObject(objectReference);


The as operator
Using the as operator is equally simple. Just place it between the variable and the Class name like this:
var receivingVariable:DisplayObject = objectReference as DisplayObject;
If you're casting just to reach a property or method of the object you can still use the asoperator:
(objectReference as DisplayObject).alpha = 0;
Yet in this particular case, I would recommend the cast operation instead. I will explain why in a moment.
When casting objects bear in mind that it can fail due to a number of reasons that are beyond the scope of this post and involve concepts like type conversion and boxing.
Differences between the cast operation and the as operator
Both the cast operation and theas operator, when successful, have the same result. They return a reference to the target instance as being of the cast type.
They only differ in how they deal with failure to cast. The cast operation throws a runtimeTypeError if the cast fails, while the as operator returns a null value.
When should you use one or the other?
If you are absolutely certain that the cast will succeed, or if you are willing to surround each cast with a try catch statement or you don't mind your application throwing runtime errors all over, you can safely use the cast operation.
If, on the other hand, you prefer to be able to deal with cast errors elegantly without having to surround each one with a try catch, you should use the as operator.
I tend to prefer the as operator as it makes the code more readable (the cast operation can be confused with constructor invocation and type conversion operations) and is more in sync with the type checking is operator, which I'll present in a moment.
The only time when I absolutely favor the cast operation is when I'm only doing the cast to access a property or method of the object. The reason why I do this is because invoking a property or a method in a null value (returned by the as operator in case of cast failure) will throw a null reference runtime error instead of a type runtime error and as a best practice it is always best to get your errors right so that you don't end up chasing for them in the wrong places.
Type checking
Type checking can easily be done using the is operator in exactly the same way as the asoperator:
var test:Boolean = objectReference is DisplayObject;
The is operator returns a Boolean value stating whether the object is an instance of that type or interface or any of its ancestors.
It would be interesting to know if there are some impacts performance and memory wise when using any of these techniques and which would be best to get that last performance crunch you need. Maybe in a future post. Stay tuned to InsideRIA for updates

воскресенье, 18 октября 2009 г.

Drawing lines with AS2



Logical following of the "Drawing shapes" tutorial and "Drawing curves" tutorial here comes the last of this series the "Drawing lines" tutorial. Is it that useful to be able to draw lines? The answer is simply YES. Drawing lines can have many application when designing your Flash file and can make your Flash developer life easier in some cases, so let's get started!


http://www.actionscript.org/resources/articles/730/1/Drawing-lines-with-AS2/Page1.html

Drawing curves with AS2

Logical following of the "Drawing shapes" tutorial, here comes the "Drawing curves" tutorial. Most likely, it will be followed by a "Drawing lines" tutorial. So let's get started and draw some curves! http://www.actionscript.org/resources/articles/729/1/Drawing-curves-with-AS2/Page1.html

Drawing shapes with AS2

Drawing any shapes with Actionscript2 is very easy. In this tutorial I will show you how to draw a basic shape and then, in part 2, how to apply this knowledge into a dynamic application. http://www.actionscript.org/resources/articles/727/1/Drawing-shapes-with-AS2/Page1.html

воскресенье, 30 августа 2009 г.

ActionScript Mouse Events and Delegation

If you’ve used Flash MovieClips events onRollOver, onRollOut, onRelease,onReleaseOutside or onPress you’ve probably encountered the annoying little Flash shortcoming, that you can’t define mouse event-handlers on MovieClips contained inside MovieClips that have a mouse event defined.

Suppose you have something like this:

this.onRelease = doSomething;
this.createEmptyMovieClip(”childMC”,this.getNextHighestDepth());
var childMC:MovieClip = this["childMC"];
//Add some code to put something inside childMC
childMC.onRollOver = doSomethingElse;

The code in the function doSomethingElse will never be reached, because all mouse events are “caught” by childMC’s parent MovieClip.

A workaround that I use is to check in the parent functions whether the events apply to children.

Here’s an example for the onRelease event:

this.onRelease = function()
{

if (childMC.hitTest(_root._xmouse, _root._ymouse, true)) {

doSomethingElse();

}

}

The MovieClip function hitTest is used to test whether the mouse is hovering above the childMC when released. hitTest takes as arguments the x and y coordinates of the mouse relative to the _root MovieClip and a boolean that specifies whether to do the hitTest using the actual MovieClip shape (true) or just it’s bounding box (false).

Delegation

Sometimes you may want to base the logic in a mouse handling event on data that’s stored in another MovieClip, of course you can expose the decisions variables by making them public variables and accessing them through some relative path like _parent.var1 or this.childMC.var1 but a cleaner solution might be using Flash delegate creator available in Flash 8 professional. I learned how to use it throughthis Delegate tutorial. With delegates you can define mouse event handlers that execute in a object scope other then the MovieClip object scope on which they are defined! This how I used it for example. I had a ‘parent’ MovieClip showing a JPG image. In the MovieClip was ‘child’ MovieClip called zoomer, that when pressed should open a new browser window containing a high-res version of the JPG. Isensed that it would be better program design to have the knowledge about the image low-res and high-res version in the ‘parent’ MovieClip. Without use of a Delegate I should have either copied the knowledge of the high-res JPG to the child MovieClip or exposed that knowledge through the parents public interface. In my humble opinion the delegate approach is the cleanest, really leaving all the data and logic concerning JPG’s in the parent MovieClip.

Here’s how I used the delegate functionality:

import mx.utils.Delegate;
zoomer.onRelease = zoomer.onReleaseOutside = Delegate.create(this, handleOnRelease);