Using byteArray with getPixel (no 's') then setPixelS - actionscript-3

Here is what I am trying to do:
one byteArray (colorsByteArray) holds color values, that I created with the "getPixels" method;
for every pixel on screen, I make calculations and depending on that I get a rank that I use to retrieve the proper color value inside colorsByteArray;
finally, I write inside a second byteArray (pixelsByteArray) the color I just retrieved. Then I 'draw' this byteArray using the "setPixels" method.
Problem is I do not find the same image as if I simply wrote a "setPixel" for every pixel in my loop. The image I get is missing one line out of two, and is kind of repeated three times horizontally. Of course I guess I must be writing (or reading) something wrong from my second byteArray, but can someone explain me what I am doing wrong? Or even if my efforts are worth anything actually :-) cause I didn't find anything quite like this so far...
Thanks!
Here is a bit of my code to make things clearer:
colorsByteArray = bitmDext.getPixels(new Rectangle(0, 0, myShadeBMD.width, 1));
canvas.lock();
for(var i:int = 0; i < STAGE_WIDTH; i++)
{
for(var j:int = 0; j < STAGE_HEIGHT; j++)
{
//make some calculation on each pixel to get the 'colorRank' uint value
colorsByteArray.position = colorRank * 4;//unsigned int is 32 bytes, representing 4 slots of a byteArray
var colorValue:uint = colorsByteArray.readUnsignedInt();
//canvas.setPixel(i, j, colorValue);//works just fine
pixelsByteArray.writeUnsignedInt(colorValue);
}
}
pixelsByteArray.position = 0;
var myRect:Rectangle = new Rectangle(0, 0, STAGE_WIDTH, STAGE_HEIGHT);
canvas.setPixels(myRect, pixelsByteArray);
canvas.unlock();
added a picture that may help:
http://www.oghel.com/pictures/stackOverflow/example3
the expected part is on the right, the wrong one on the left.

Related

Send all instances of a container BEHIND everything else?

Found this topic: AS3 setChildIndex to front I'm trying to accomplish the exact opposite.
"addChildAt" doesn't work for me when I'm setting the container behind everything else or something along the lines. And as for stars themselves, I can't send them back anymore than layer 0 for some odd reason (it'll give me error 2006, stating "The supplied index is out of bounds"). Here's the code:
starsSpawn function:
var starContainer:MovieClip = new MovieClip();
addChildAt(starContainer, 20);
starContainer code:
function starsSpawn()
{
for(var i:int= 0; i < 30; i++)
{
var newStar = new starCode();
var scaleXY = Math.random()*(2)+0.1;
newStar.width = scaleXY;
newStar.height = scaleXY;
var positionX:Number = Math.random()*(stage.stageWidth + (1* newStar.width));
var randomY:Number = Math.random()*(stage.stageHeight - newStar.height);
newStar.x = positionX;
newStar.y = randomY;
starContainer.addChild(newStar);
}
}
Essentially how it works is that a container is set up and the for loop creates 30 stars, each with the outlined code.
When you addChildAt(myChildMC, 0); it gets added at index 0 and everything else gets bumped up. Z order indexes in AS3 are contiguous, meaning every space from zero to the highest z order must be filled. If I remove whatever is on layer 4, layer 5 will slide down to fill, and so on
And I don't think you can add something to index 20 unless all the other indexes have children in them. That may be what is causing your particular error. To add something to the top level just do addChild(myChild).
edit
Since it sounds like you have the concept of how z-order works in reverse in your mind, you probably are wanting to have the stars "in front" of everything else i.e. Always on top i.e. Always visible. To do that just do addChild(starsContainer) after all the other containers are added or Sprites or MovieClips. If you add something to the stage at runtime, just add the stars container again (this won't create a second container, it literally just changes the z-order if there is already a container by that name... this is an often misunderstood point).

Color based on distance

This question already has a working answer at : Weird graphical bug in > AS3?
I'm trying to make a simple tile lighting system in AS3 and the problem I've come across is that I have no idea how to change the color based on distance using hexadecimal color codes
heres the code for the color changing part:
for (var i:int = 0; i < buttons.length; i++) {
lightFact = getDistance(lightSource, buttons[i])
colorTransform2.color = 0x000000
DisplayObject(buttons[i]).transform.colorTransform = colorTransform2;
}
lightFact being the distance from the lightSource, if you can't figure that out from the function name.
I'm not sure what the final objective is for this code but here we go.
getDistance() should return a numeric value. Next step is to create a function or algorithm that codify a number into a color. This could be just setting the R of the RGB color equals to the distance (setting a cap in 255) or many if-else statements like:
if(lightFact < XValue)
{
colorTransform2.color = 0xFF0000;
}
else if(lightFact < YValue)
{
colorTransform2.color = 0x00FF00;
}
else
{
colorTransform2.color = 0x0000FF;
}
or any other more complex codification you might like. Any codification you choose will, either way, need some kind of logic that checks lightFact's value and based on it (and any other variables you may have) makes a calculation or decision to set a different value to colorTransform2.color. Otherwise colorTransform.color will always be 0x000000 (black/turned off).

Find count of elements in a Vector

If I assign a Vector like
var vec1:Vector.<Number> = new Vector.<Number>(3);
vec1 = (1,2);
the result of vec1.length is 3. Is there any built-in method to return the number of the elements actually present in the vector?
I'm an ActionScript noob so any help would be appreciated.
Well you can solve your problem by creating an empty vector instead of a defining the vector size at the time of its declaration and then you gradually add and remove elements to the vector. In this way you will always get the total number of elements inside the vector when you call vector.length
For example:
var vec1:Vector.<Number> = new Vector.<Number>();
vec1.push(5);
vec1.push(6,7);
vec1.pop();
Then vec1.length would give you 2.
Its been awhile, heres the reference: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Vector.html
I believe the .length is the normal way to check its length. If you want to check for "empty elements" you will need to loop through it with a loop.
I don't remmember the exact syntax, but to loop, do something like:
int count = 0;
for (int i = 0; i < vec.length; i++) {
if (vec[i] == ... );
count++;
}

Replace the selected pixel

How do I replace the selected pixel of the image? I used set pixel and get pixel concept but not getting the desired effect.
http://www.digital-photography-school.com/wp-content/uploads/2009/07/before-after.jpg
var s_color = 0x0083C7;
color_picker.addEventListener(ColorPickerEvent.CHANGE, changeColor);
function changeColor(ColorPickerEvent)
{
var _color = color_picker.selectedColor.toString(16);
var color = String("0x"+_color);
for (var j = 0; j <m_inputImage.width; j++)
{
for (var k = 0; k < m_inputImage.height; k++)
{
if (m_inputImage.getPixel(j,k)== s_color)
{
m_inputImage.setPixel(j,k,color);
}
}
}
s_color = color;
}
I want similar type effect.
Please guide me.
This is not a job for BitmapData, you should use Pixelbender for this.
http://www.adobe.com/devnet/flash/articles/pixel_bender_basics.html
You can find all shaders here, there are a lot of hue/saturation and color manipulation filters so pick one that suits you the best.
http://www.adobe.com/cfusion/exchange/index.cfm?event=productHome&exc=26&loc=en_us
I would use Photoshop instead of Flash to achieve the effect you desire.
However Photoshop is kind of expensive, so I would use the Bitmap class in conjunction with the BitmapData class and use an algorithm to run through each pixel and check for a certain threshold of red and convert it to the right threshold of yellow. If you would post the code you already have written I could possibly add to it, I'm not going to spend the next hour writing an example though.

AS3: How can I construct this Linkage problem?

I want to do something like this with AS3, but I'm new to AS3 and I'm not sure how to go about the coding.
What I need to do is I have 1 x blank MC called all_mc, inside that I need to have 200 x empty_mc all lined up one after another on the x axis.
Each empty_mc is 100px wide and load from a Linkage in the library called panelClass (which is a MovieClip).
The empty_mc itself is called emptyClass in the library.
I need the all_mc to display on the stage from start. It should look like this image. The I need 200 of these red squares.
I know instead of adding all 200 MCs manually, I should be making a loop? But I can not get my head around it for the life of me. Can someone please kindly help me out?
Just make a loop and dynamically create your MovieClips:
var mcWidth:Number = 100; // Using hardcoded value because MovieClip.width is not always reliable (if the MovieClip contains shapes with strokes, etc.)
for (var i:int = 0; i < 200; i++) {
var mc:panelClass = new panelClass();
all_mc.addChild(mc);
mc.name = "empty_mc" + i; // set a name so that it can be accessed later on
mc.x = mcWidth * i;
}