People seemed to do pretty well on yesterday’s quiz. I have lots of questions written that fall into that same category of being somewhere between beginner and intermediate level. I decided to write a trickier quiz for today. Tomorrow I’ll alternate back to some easier questions. This quiz is really more about operators and hex numbers than ActionScript…
Number of Questions : 5
Difficulty : Medium
Topic : Operators and Hexadecimal
var currentIndex:int = indexCanvas.getPixel(mouseX, mouseY);
if(currentIndex != 0xFFFFFF){
var currentBox:Array = info[currentIndex]
redRect.visible = true;
redRect.x = currentBox[0];
txt.y = redRect.y = currentBox[1];
if(mouseX <txt.width){
tf.align = TextFormatAlign.LEFT;
txt.defaultTextFormat = tf;
txt.x = redRect.x + 10;
}else{
tf.align = TextFormatAlign.RIGHT;
txt.defaultTextFormat = tf;
txt.x = redRect.x - txt.width;
}
txt.text = currentBox[2];
txt.visible = true;
}else{
redRect.visible = false;
txt.visible = false;
}
}
This is a simplified example of the technique discussed in yesterdays post. The idea is to use a BitmapData image to store index values for a large number of elements that need to be able to act as if the have MouseEvents. For a more detailed description of this technique see yesterdays post.
I'm working on a data visualization that contains a long path made up of approximately one million points. There is some information associated with every two sets of coordinates that needs to be displayed when the user rolls their mouse over any part of the line.
I took a little time to think about the best way to do this and came up with a few techniques. The first one I tried seems to work nicely - this snippet is the proof of concept for that first technique. I tested this snippet with 1,000,000 xy coordinates and it works nicely. It takes a little while to draw though, so for the purposes of this demo I've just included 20,000 coordinates.
The way this works is by drawing lines to two different BitmapData instances. I draw anti-aliased slightly transparent lines to a BitmapData instance called "canvas" (this is added to the display list) - I then draw aliased lines to a BitmapData called "indexCanvas" (this is never added to the display list) - each aliased line uses an incremental value for its color - this incremental value is also the index for a two dimensional array containing the coordinate information for the aliased line. I use getPixel() on the "indexCanvas" and use the return value as the index for the 2D array. The data from the 2D array is used to draw a red line with the graphics class. This technique enables you to have many many rollovers and all you ever have to do is call getPixel() and use the returned color value to look up info about what you're mouse is touching.
There are a few cool ways this could be repurposed and this is really only one solution to the problem of having many many things that you need to be able to rollover... there are others that don't use BitmapData at all... I may write those up in the next couple of days.