Using a variable to replace portion of code? AS3 - actionscript-3

How do I get the var sliderthumb to replace [sliderthumb] in my code below? Thanks for your help.
var sliderthumb:String = "foo";
function natureThumb(){
Sprite(naturepage.sliders.[sliderthumb].getChildAt(1)).height = Sprite(naturepage.sliders.[sliderthumb].getChildAt(1)).width = 65;
}

Just remove the dot.
Sprite(naturepage.sliders[sliderthumb].getChildAt(1)).height

There is a method for displayObjectContainer called getChildByName that allow to access child display objects by its name. Hope this helps!
var sliderthumb:String = "foo";
function natureThumb(){
Sprite(naturepage.sliders.getChildByName(sliderthumb).getChildAt(1)).height = Sprite(naturepage.sliders.getChildByName(sliderthumb).getChildAt(1)).width = 65;
}

Well both answers provided can sorta be right depending on how the OP set things up, but wanted to add some clarification. First I would modify the code to be this:
var sliderthumb:String = "foo";
function natureThumb(){
var sp:Sprite = naturepage.sliders.[sliderthumb].getChildAt(1) as Sprite;
sp.height = sp.width = 65;
}
if you setup the object like
var mySprite:Sprite = new Sprite();
mySprite.id = "foo";
then this will work (assuming you want the second child of the object foo which is a child of sliders)
function natureThumb(){
var sp:Sprite = naturepage.sliders[sliderthumb].getChildAt(1) as Sprite;
sp.height = sp.width = 65;
}
if you instead setup your object like
var mySprite:Sprite = new Sprite();
mySprite.name = "foo";
then this will work
function natureThumb(){
var sp:Sprite = naturepage.sliders.getChildByName(sliderthumb).getChildAt(1) as Sprite;
sp.height = sp.width = 65;
}

Related

event listener not being called

How can I resolve my problem?
I load all images and image buttons when I click on a button but after that when I try to click on close button (image4) the event listener cannot listen because everything is set in another function.
private function onShop(evt:MouseEvent)
{
shop.removeEventListener(MouseEvent.CLICK, onShop);
var container:Sprite = new Sprite();
var ibutton:Sprite = new Sprite();
addChild(container);
addChild(ibutton);
ibutton.buttonMode = true;
function loadImage(path:String):Loader
{
var request:URLRequest = new URLRequest(path);
var loader:Loader = new Loader();
loader.load(request);
return loader;
};
var image1 = loadImage("image/inventory/shop_windoww.png");
image1.x = 200;
image1.y = 40;
this.addChildAt(image1,8);
var image2 = loadImage("image/inventory/title_window.png");
image2.x = 200;
image2.y = 14;
container.addChild(image2);
var image3 = loadImage("image/inventory/icon_window.png");
image3.x = 177;
image3.y = 7;
container.addChild(image3);
var image4 = loadImage("image/inventory/close.png");
image4.x = 891;
image4.y = 39;
ibutton.addChild(image4);
var image5 = loadImage("image/inventory/button_ok.png");
image5.x = 450;
image5.y = 485;
ibutton.addChild(image5);
var image6 = loadImage("image/inventory/button_ok.png");
image6.x = 782;
image6.y = 485;
ibutton.addChild(image6);
image4.addEventListener(MouseEvent.CLICK, test);
}
private function test(evt:MouseEvent)
{
image4.removeEventListener(MouseEvent.CLICK, test);
trace("ca marche");
}
Declare your variables outside of the onShop scope. They seem to have more sense as global variables, not only local (because you are trying to use the same variables declared on the onShop method on test method).
I also recommend you some points:
Use an external file (JSON/XML) to get all image URLs, or just use a simple Array with hardcoded content;
Use a for loop to load all images (after following the previous suggestion);
Use event.currentTarget in your test method, so, you can deal with those images with a more generic way.
[event.currentTarget] The object that is actively processing the Event object with an event listener. For example, if a user clicks an OK button, the current target could be the node containing that button or one of its ancestors that has registered an event listener for that event.

Why does (myMC.myArray = myOtherMC.myOtherArray;) cause any changes to myMC.myArray to also change myOtherMC.myOtherArray?

var myArray:Array = new Array();
var myMC:MovieClip = new MovieClip();
myMC.myArray = myArray;
trace(myMC.myArray[10]); //Output: undefined
var newMC:MovieClip = new MovieClip();
newMC.myOtherArray = myMC.myArray;
newMC.myOtherArray[10] = [];
newMC.myOtherArray[10][0] = 100;
trace(myMC.myArray[10]); //Output: 100
Why does that happen, and is there any way to avoid it?
EDIT:
Found a function that can clone associative arrays here.
Here is the function (from the above link):
function clone(source:Object):*
{
var myBA:ByteArray = new ByteArray();
myBA.writeObject(source);
myBA.position = 0;
return(myBA.readObject());
}
Does making the function return type "*" mean that it can be any type? or is it something specific to objects/arrays?
It's because you have passed a reference of the original array to the new clip. Arrays, as objects, are not primitives and therefore will always be referenced.
If you would like to clone an array, keeping the original intact, use this method:
var b:Array = a.concat();
b will be a new array. a can modified without changing b.

TextField as3 getting the input

i have a problem witch i cant solve, and i wanted to ask what am i doing wrong. The idea should be that when i create the textfield i want to read from it, but it doesnt
function click2(e:MouseEvent):void{
e.currentTarget.removeEventListener(MouseEvent.CLICK, click2);
fx=e.target.x+400;
fy=e.target.y+300;
var i:int;
i=2;
trace(str);
trace(e.target.name);
var line:Shape = new Shape();
line.graphics.lineStyle(1,0xFF0000,2);
line.graphics.moveTo(sx,sy);
line.graphics.lineTo(fx,fy);
this.addChild(line);
var inputField:TextField = new TextField();
inputField.border = true;
inputField.type = TextFieldType.INPUT;
inputField.width = 23;
inputField.height = 18;
inputField.x = (sx+fx)/2;
inputField.y = (sy+fy)/2;
inputField.multiline = false;
inputField.maxChars = 3;
inputField.restrict = "0-9";
str=inputField.text;
addChild(inputField);
}
In this code i create a line, and near it appears a textfield , where you need to input the value of the line, but i can`t get it , when i want to trace the STR value, it is null,the text should be written by the user and i should read it ...
If you want to check the data the user added to the textinput you have to listen for the change-event. After that you can access the provided text.
function click2(e:MouseEvent):void{
...
inputfield.addEventListener(Event.CHANGE, checkInput);
}
function checkInput(e:Event):void {
//receive input value and validate it
var textfield:TextField = e.target as TextField;
var str:String = textfield.text;
...
}

AS3 removing dynamically created child movieclips

I'm fairly new to AS3. Anyways, I'm try to remove a dynamically created child movieclip when clicked on. When a dirt block is clicked on, which is a child movieclip of 'world' I want to remove it.
I've tried various ways of removing it using removeChild. I've also tried moving the function inside/outside of the for loop that creates the movieclips.
var blockCount:Number = 0;
var blockArray:Array = [];
var world:MovieClip = new World();
world.x = 50;
world.y = 50;
world.name = "world";
addChild(world);
for(var i:Number=1;i<=100;i++){
blockCount++;
var tempGrassBlock:MovieClip = new GrassBlock();
tempGrassBlock.x = i*16;
tempGrassBlock.y = 256;
tempGrassBlock.name = "b"+blockCount;
world.addChild(tempGrassBlock);
tempGrassBlock.addEventListener(MouseEvent.CLICK, removeBlock);
function removeBlock(event:Event){
world.removeChild(getChildByName(event.target.name));
}
}
Thanks for the help.
Try this
function removeBlock(event:Event){
world.removeChild(event.currentTarget as DisplayObject);
}
No function definition should be inside a for. I changed that in your code and rewrited a little below:
var blockCount:Number = 0;
var blockArray:Array = [];
var world:MovieClip = new World();
world.x = 50;
world.y = 50;
world.name = "world";
addChild(world);
for(var i:Number=1;i<=100;i++){
blockCount++;
var tempGrassBlock:MovieClip = new GrassBlock();
tempGrassBlock.x = i*16;
tempGrassBlock.y = 256;
tempGrassBlock.name = "b"+blockCount;
world.addChild(tempGrassBlock);
tempGrassBlock.addEventListener(MouseEvent.CLICK, removeBlock);
}
function removeBlock(event:MouseEvent){
trace("Is click really working? This target name is " + event.currentTarget.name);
world.removeChild(event.currentTarget));
}

AS3 - Adjust image colors

I'm adjusting image colors through the function below. The problem is that if I need to switch a colorFilter value to 0 it's not working but if I enter 0.1 instead of 0 it works.
How to make it work without that workaround?
import fl.motion.AdjustColor;
import flash.filters.ColorMatrixFilter;
var colorFilter:AdjustColor = new AdjustColor();
var mColorMatrix:ColorMatrixFilter;
var mMatrix:Array = [];
var MC:MovieClip = new MovieClip();
function adjustColors():void
{
colorFilter.hue = 50;
colorFilter.saturation = 50;
colorFilter.brightness = 50;
colorFilter.contrast = 12;
mMatrix = colorFilter.CalculateFinalFlatArray();
mColorMatrix = new ColorMatrixFilter(mMatrix);
MC.filters = [mColorMatrix];
}
I tested this by adding an argument to adjustColors() and calling it twice, and I see the same problem. I think it's just a bug where it ignores zero values.
It's not much of a better workaround, but if you just create a new AdjustColor each time, it should work correctly:
import fl.motion.AdjustColor;
import flash.filters.ColorMatrixFilter;
var colorFilter:AdjustColor = new AdjustColor();
var mColorMatrix:ColorMatrixFilter;
var mMatrix:Array = [];
var MC:MovieClip = new MovieClip();
function adjustColors():void
{
colorFilter = new AdjustColor();
colorFilter.hue = 50;
colorFilter.saturation = 50;
colorFilter.brightness = 50;
colorFilter.contrast = 12;
mMatrix = colorFilter.CalculateFinalFlatArray();
mColorMatrix = new ColorMatrixFilter(mMatrix);
MC.filters = [mColorMatrix];
}
Here is my workaround for reference:
Just use the logical OR assignment when you set each property.
That way if a value is 0 it will evaluate to false and .1 will be assigned instead:
var colorMat:ColorMatrixFilter = new ColorMatrixFilter();
var colorAdjust:AdjustColor = new AdjustColor();
const colorsAdj:Array =
[
// BRIGHTNESS, CONTRAST, SATURATION, HUE
[-20,0,20,-50],
[0,0,0,0],
[0,0,0,17]
];
function setColorMat(colorID:int):void
{
colorAdjust.brightness = colorsAdj[colorID][0] ||= .1;
colorAdjust.contrast = colorsAdj[colorID][1] ||= .1;
colorAdjust.saturation = colorsAdj[colorID][2] ||= .1;
colorAdjust.hue = colorsAdj[colorID][3] ||= .1;
colorMat.matrix = colorAdjust.CalculateFinalFlatArray();
}
That way you avoid recreating a new ColorMatrixFilter each time, in case it really change something...
And you keep a nice clean array... ;-)