I have this
function fl1(floor)
{
this.("fl"+floor).visible=true;
}
how to visible it, if the location of the object in "MovieClip(root)"
i try this, but is'nt work... (sorry of my bad english)
function fl1(floor)
{
this.("MovieClip(root).fl"+floor).visible=true;
}
You should do
this["fl" + floor].visible = true
In general, if you want to access the property by the instance name, then you should use the array subscript operator ( [] ) directly on the object without using dot (.)
What is floor's variable type passed to the function fl1?
If it is String and you construct the name of the object which visibility you want to change you need to use getChildByName.
var myFloor:* = parentObject.getChildByName(floor);
if(myFloor)myFloor.visible = true;
To access so called root, you can use stage property of any object added to display list.
var root = this.stage;
If floor is already object, you simply:
floor.visible = true;
Related
i want to access the child of Object!
for instance:
we a have child(that name is a1) in k1 object.
how can i access it(my means a1 which in the k1)
var k1:keypad=new keypad();
k1.x=7.05;
k1.y=229.20;
add Child(k1);
this.k1.a1.addEventListener(Mouse Event.CLICK,ts);
function ts(event:Mouse Event):void{
trace("OK");
};
thank you.
event has a property target. this one is the child which was clicked.
function ts(event:MouseEvent):void{
trace(event.target);
};
You are doing it wrong. You declare a local variable k1:
var k1:keypad
which is not a member of this object, you cannot access it
this.k1
So your code will probably work this way:
// Bad class name: lowercase.
var k1:keypad = new keypad;
k1.x = 7.05;
k1.y = 229.20;
// This is addChild() method, it is spelled without space.
addChild(k1);
// Then you access k1 just as you did above, without this.
// This will work if you designed your keypad with
// Adobe Flash IDE on default publish settings.
k1.a1.addEventListener(Mouse Event.CLICK, onA1);
// This will work too if a1's instance name is a1:
// k1.getChildByName("a1").addEventListener(Mouse Event.CLICK, onA1);
// Event handler. MouseEvent is a class name,
// it has no space inside.
function onA1(e:MouseEvent):void
{
trace("OK");
}
// Normally you don't need ; after function body.
I have a MovieClip inside library, linkaged to MyObject and it contains a textField.
I don't know how I can access this textField without using the getChildByName method.
Apparently, the 3rd section works when object is on stage (without using addChild). But when using addChild I think there has to be some kind of casting; which I don't know how.
var childElement: MyObject = new MyObject();
childElement.name = "theChildElement";
container.addChild(childElement);
btn.addEventListener(MouseEvent.CLICK, changeText);
function changeText(event: MouseEvent): void
{
var targetBox:MovieClip = container.getChildByName(childElement.name) as MovieClip;
targetBox.textField.text = "hello"; // THIS WORKS
// This works too:
// MovieClip(container.getChildByName("theChildElement"))["textField"].text = "hello"; // THIS WORKS TOO.
// THIS DOESN'T WORK. why?
// container["theChildElement"]["textField"].text = "hello";
}
As confusing as it may seem, instance name, and name are not the same. From your code you should always be able to get to your MC by it's variable name. To get your last like to work you could just use this.
childElement["textField"].text = "hello";
There is a difference between Symbols created by the Flash IDE, which aggregate other DisplayObjects and programmatically created DisplayObjects.
When a DisplayObject is created in the Flash IDE, it's instance name can be used to resolve the instance as a property - which means it can be accessed via []. The [] can be used to access properties or keys of dynamic declared classes - like MovieClip. This necessary because you'll most likely down cast to MovieClip instead of using the symbol class created by Flash. That is not possible when simply using addChild, addChildAt or setChildAt from the DisplayObjectContainer API.
It is always the save way to access it via getChildByNameand check for null because otherwise your app, website or whatever is doomed for 1009 errors as soon as someone is changing the symbols.
I'd create a bunch of helper methods, like
// not tested
function getChildIn(parent:DisplayObjectContainer, names:Array):DisplayObject {
var child:DisplayObject, name:String;
while (names.length > 0) {
name = names.shift();
child = parent.getChildByName(name);
if (!child) {
// log it
return null;
}
if (names.length == 0) {
return child;
}
}
// log it
return null;
}
function getTextFieldIn(parent:DisplayObjectContainer, names:Array):TextField {
return getChildIn(parent, names) as TextField;
}
function getMovieClipIn(parent:DisplayObjectContainer, names:Array):MovieClip {
return getChildIn(parent, names) as MovieClip;
}
Your third method doesn't work because you are trying to call the ChildElement by it's name
without using getChildByName method. On the other hand, you shouldn't call your textField textField, because that's already an actionScript property.
Your should rather call it 'displayText' for example.
For a textField called 'displayText' contained in childElement :
function changeText(event:MouseEvent): void
{
childElement.displayText.text = "hello";
}
I want to be able to access a instance on the stage dynamically by looping through an array containing Strings that describes the path.
private var clockKeeper:Array = new Array("LB.anim.clock.lbclock");
trace(stage.LB.anim.clock.lbclock.text);
for (var key in clockKeeper) {
trace(stage[clockKeeper[key]].text);
}
When i access it manually with the first trace statement, it works.
When i do it dynamically it seems like Flash tries to find an object named "LB.anim.clock.lbclock" not LB.anim....
How can i change this behaviour and make it work?
You should try splitting the "path" which should then consist of locally available names, and address each object in order. "Locally available names" means there should be stage.LB, and that object should have a property anim, etc etc.
function getObjectByPath(theRoot:DisplayObjectContainer,
thePath:String,separator:String='.'):DisplayObject
{
var current:DisplayObjectContainer=theRoot;
var splitPath:Array=thePath.split(separator);
while (splitPath.length>0) {
var named:DisplayObject = current.getChildByName(splitPath[0]);
var addressed:DisplayObject=current[splitPath[0]];
// either named or addressed should resolve! Otherwise panic
if (!addressed) addressed=named; else named=addressed;
if (!named) return null; // not found at some position
splitPath.shift();
if (splitPath.length==0) return named; // found, and last
current=named as DisplayObjectContainer;
if (!current) return null; // not a container in the middle of the list
}
// should never reach here, but if anything, just let em
return current;
}
This provides two ways of resolving the path, by name or by property name, and property name takes precedence. You should then typecast the result to proper type.
Yes, call this as follows:
trace((getObjectByPath(stage,clockKeeper[key]) as TextField).text);
How can I pass the keyword this OR an instance name as a parameter inside a function?
function (reference:InstanceName):void // kind of what I want
{
reference.gotoAndPlay("frameLabel");
}
To clarify jozzeh's correct answer: your problem is variable scope. The "this" keyword's scope is contained to the owning object - you would need to establish the proper scope of the parent timeline in your function call:
function goTo( reference:MovieClip ):void
{
reference.gotoAndPlay("Start");
}
goTo(this.root); // variable scope of "this" is now at the class level
Obviously, we sometimes need parameter initializers, but in this case - a reference to 'this' is going to throw an error. If this is a function that has a changing value, sometimes the focus of which is of its own root, you'd need to handle the initializing logic outside of the method sig.
good luck!
I have never seen this being referenced like that...
The 'this' within a function is a reference to the object that initialized the function.
example:
var test:String = "testing the test."
test.myfunction();
function myfunction():void{
//this = the variable "test"
this.indexOf('test');
}
Also if you want to pass a variabel to a function it should be like this:
var test:String = "testing the test."
myfunction(test);
function myfunction(mystring:String):void{
var indexer:Number = mystring.indexOf('test');
}
I'm pretty positive what I want to do isn't possible with ActionScript, but it would be nice to be wrong.
I need to pass a variable reference to a function, and have the function change the value of the variable.
So, simplified and not in completely correct syntax, something like this:
function replaceValue(element:*, newValue:String):void
{
element = newValue;
}
var variableToModify:String = "Hello";
replaceValue(variableToModify, "Goodbye");
trace(variableToModify) // traces value of 'Hello', but want it to trace value of 'Goodbye'
Of course, in the replaceValue function, element is a new reference to fungibleValue (or, rather, a new reference to fungibleValue's value). So, while element gets set to the value of newValue, fungibleValue does not change. That's expected but totally not what I want, in this case.
There's a similar question, for Ruby, here Changing value of ruby variables/references
As the question points out, Ruby has a way to accomplish this. BUT is there any way to do this in ActionScript?
If so, it's going to make something stupid a lot easier for me.
No it's not possible the function will always get the value and not the reference. But if you are able to call replaceValue why not returning the new value from your function :
function replaceValue(element:*, newValue:String):String
{
// .. do your work
return newValue;
}
var variableToModify:String = "Hello";
variableToModify = replaceValue(variableToModify, "Goodbye");
trace(variableToModify)
If you pass an Object or a Class, you can modify one fiels based on his name as :
function replaceValue(base:Object, fieldName:String, newValue:String):void {
// do your work
base[fieldName] = newValue;
}
var o:Object={ variableToModify:"Hello" };
replaceValue(o, "variableToModify", "Goodbye");
trace(o.variableToModify);