Set private var string from input text - actionscript-3

The other day I tried setting a string var inside flash, I need somehow to set the var as the text box. I tried doing this:
private var name: String = fromthis.text;
and it doesn't work, anyone knows why?

If I guess the question correctly:
private var name: String = fromthis.text;
means that you declare a variable called name and make its value equal to fromthis.text at that very moment. If you type something into fromthis, it doesn't change the name variable. You need to listen to TextFiled CHANGE event to keep the variable up to date.
update:
You are getting Error #1009 because fromthis is not visible from where you declare the name variable and so it is equal to null, which can't have any fields (including field called text), so you are getting this error.
It may happen becuase it is not yet created at that moment or because the code is located inside of an .as class file and fromthis is located on the stage itself and thus can not be accessed like this.
update 2:
If name is located in a class file, and fromthis is just dragged into the stage in the editor, the best way would be to just pass it to the class constructor:
private var _tf:TextField;
function MyClass(tf:TextField){
_tf = tf;
//or if you need the string from textfield just once you may pass that string
}
And call the class constructor new MyClass(fromthis) (assuming you have access to fromthis at where you instantiate your class).

Related

Actionscript 3 instance name property not working

I have a class of a movieclip symbol that is called third_scene_border, I create 12 instances of this class like so:
public var border_1:third_scene_border = new third_scene_border();
public var border_2:third_scene_border = new third_scene_border();
public var border_3:third_scene_border = new third_scene_border();
and so on, I also set the name of the first instance to be "first_border" like so:
border_1.name = "first_border";
Then when I trace its name I get "instance(some numbers)".
Why isn't the name property being set correctly? Ive done it for a lot of other instances and its working just fine. I am trying to see on which border a draggable object is being dropped.
Edit: When I write trace(border_1.name) I get "first_border", but when I add an event listener that listens for clicks and put
trace(event.target.name);
in its function, I get instance(some numbers).
Edit: trace(event.target.parent.name); returns first_border which is correct but when I try to trace the dropTarget in the function of a MouseEvent.MOUSE_UP like so: trace(event.target.dropTarget.parent.name), I get the following error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at mainClass/up()
It's hard to find a 100% solution for your case, because you didn't attach the part of your code with adding the event listener. But, I'd hazard a guess, that you should try to use the event.currentTarget parameter instead of event.target.
You may read more about differences about theese 2 properites here:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/Event.html#target
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/Event.html#currentTarget

Objects within Objects

I have an object in my Library called Bottle. Bottle is made up of "Glass" and "Cap" instances. There are two more symbols in my library called Cap, and Glass.
When I click on the cap of Bottle, it says that this object is of class Cap, and when I click on the glass, it says it is of type Glass. Each one of these objects has base class flash.display.MovieClip.
However, in my code when I do:
var bottleOnStage:Bottle = new Bottle();
addChild(bottleOnStage);
var newColor:uint = 0x00ff00;
var newColorTransform:ColorTransform = new ColorTransform();
newColorTransform.color = newColor;
bottleOnStage.Glass.transform.colorTransform = newColorTransform;
I get this error:
TypeError: Error #1010: A term is undefined and has no properties. at MethodInfo-1()
Am I accessing the Glass property wrong? Is it because I haven't created an instance of Glass? I am confused on how objects within objects work in Flash.
EDIT
var cap:Cap;
var glass:Glass;
Above is what is in my Bottle.as file. In my Main.as file I have:
var bottleOnStage:Bottle = new Bottle();
bottleOnStage.cap = new Cap();
bottleOnStage.glass = new Glass();
addChild(bottleOnStage);
var newColor:uint = 0x00ff00;
var newColorTransform:ColorTransform = new ColorTransform();
newColorTransform.color = newColor;
bottleOnStage.glass.transform.colorTransform = newColorTransform;
When I run this code, no changes occur to the "glass" portion of the bottle. Why is this? I know that it is this line; I have traced and debugged all of the other lines, and the colors I am tracing are correct, etc. When I add "cap" and "bottle" to "bottleOnStage" using addChild, I get a duplicate of these two symbols, so this is apparently not the way. Basically, how do I modify "cap" and "glass" on stage?
It looks like your are confusing Classes with instances. Instance names cannot have the same name as a Class name (in the same scope).
Glass is your class. If you have variable with the name of "Glass" inside your bottle class, you need to rename it so it isn't ambiguous with your class name Glass.
bottleOnStage.glassInstanceName.transform.colorTransform = newColorTransform;
As a tip, to avoid this situation best practice is always make your instance names begin with a lower case letter, and always make your Class names begin with an upper case letter. (That also helps with code highlighting in most coding applications as well as here in Stack Overflow - notice how your uppercase items are hightlighted?)
As far as your error goes, you likely don't have an actual object in your variable yet.
Doing the following:
var myGlass:Glass;
Doesn't actually make an object (the value is null), it's just defining a placeholder for one. You need to instantiate using the new keyword in order to create an actual object.
var myGlass:Glass = new Glass();
Now you'll have an object in that variable.
EDIT
To address your edit, sounds like your probably want to something like this:
package {
public class Bottle extends Sprite {
public var cap:Cap;
public var glass:Glass;
//this is a constructor function (same name as the class), it gets run when you instantiate with the new keyword. so calling `new Bottle()` will run this method:
public function Bottle():void {
cap = new Cap();
glass = new Glass();
addChild(cap); //you want these to be children of this bottle, not Main
addChild(glass);
}
}
}
This keeps everything encapsulated and adds the cap and glass as children of the bottle. So bottle is a child of main, and cap and glass are children or bottle.
Whats is the name of the Glass attribute in the bottle?
if you have for example:
public class Bottle {
public var glass : Glass;
}
You can access the glass with:
var bottle : Bottle = new Bottle();
bottle.glass = new Glass();
Glass is the class. bottle.glass is the attribute "glass" of the class Bottle.
Hope it helps.

ActionScript 3 - Define and construct a class from a variable

I'm trying to easily add in some dynamic attributes to a few variables that I construct at the beginning of my movie clip.
The variable is called with the line:
var clipToUse:CustomClip = new CustomClip();
I need to replace the CustomClip class (which is created in the library) with a variable that's changes earlier in the function. I tried setting a variable and then using the root[variable] command, which threw an error saying that a semicolon was expected on the right bracket.
I'm at my wit's end trying to get this custom class to be defined by a variable.
You can try:
var ClassName:Class = getDefinitionByName('CustomClip') as Class;
//DisplayObject/DisplayObjectContainer/Sprite/MovieClip, the base class you are using in your CustomClip
var clipToUse:DisplayObject = new ClassName();
addChild(clipToUse);

Error #1056: Cannot create property *** on ***

I have a weird problem, and I don't know why this is happening.
I have a movieClip with the name of wellcomeMenu. It is exported for AS with the name of WellcomeMenu, and in the document class I do this:
public var _welcome:WellcomeMenu = new WellcomeMenu();
public function MainTest()
{
_welcome.x = stage.stageWidth * 0.5
_welcome.y = stage.y
addChild(_welcome);
}
All simple stuff. Then I go into the WellcomeMenu movieClip and make a shape with the name Box, then I make it a movieClip too, and give its Instance Name the name specialItem.
To sum up: I dynamically call a wellcomeMenu movieclip, which contains another movieClip with an instance name of specialItem. Then I compile and get this error:
ReferenceError: Error #1056: Cannot create property specialItem on WellcomeMenu.
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at WellcomeMenu()
at MainTest()
what am I doing wrong?
When I remove its instance name, it shows just fine, but I can't manipulate the movieclip within the WellcomeMenu.
I think the problem is in specialItem attribute in the WellcomeMenu class (by the way, you may have an extra l in there if it's English). If the environment is failing to create the attribute, it is probably already there, but with the wrong permission.
If you declared specialItem manually, make sure it's public and not private (public Sprite specialItem) or else the environment won't have permission to set its value.
Another possible issue is that you declared specialItem manually and still enabled the "automatically declare instance", the environment may try to redeclare the attribute and fail. So either remove the manual declaration or disable that option.
The error can happen if you assign an object of one type to another.
var square:Square = new Square();
square.row = 9; //OK, There is a row property in the Square class
var block:Block = new Block();
square = block; //this is not a compiler error, but probably a mistake
square.row = 0; //error if there is no row property on Block

Trying to understand a function

Im trying to understan a function that I found on the web.
Iknow what the function does, It get the information about the webcam in your computer and post it on the textArea,
But the individual line are just a bit confused.
Any help ?
Thanks
private var camera:Camera;
private function list_change(evt:ListEvent):void {
var tList:List = evt.currentTarget as List;
var cameraName:String = tList.selectedIndex.toString();
camera = Camera.getCamera(cameraName);
textArea.text = ObjectUtil.toString(camera);
}
private var camera:Camera;
This line creates a variable of the class type Camera. It does not create an instance of the variable.
private function list_change(evt:ListEvent):void {
This line is a standard function heading. Because the argument is a ListEvent, it makes me think that this function is probably written as an event handler. Because of the name of the function, it is most like listening to the change event on a list.
var tList:List = evt.currentTarget as List;
This line creates a reference to the list that dispatched the event, which caused this handler to be executed.
var cameraName:String = tList.selectedIndex.toString();
This line converts the selectedIndex to a string. It's a bit odd to convert an index to a string, as opposed to some value. But the reason they do that looks to be on the next line..
camera = Camera.getCamera(cameraName);
This uses that camera variable (defined back in line 1) and actually gets an instance of the camera. It uses the "cameraName" which makes me think that the list that dispatched this change event contains a list of cameras available on the system.
textArea.text = ObjectUtil.toString(camera);
This converts the camera object to a string and displays it in a text area. Normally you wouldn't try to do this as it provides no valuable data. A default object will display strings as [Object object] or something similar. Perhaps the camera object has a custom string function; I don't have experience with that. Normally, you'd want to access properties of the object to get useful information, not try this on the object itself.
}
This line is the end of the function. The open bracket was in the 2nd line of code in the function definition.