text to object actionscript 3.0 - actionscript-3

I'm creating a keyboard for my logographic writing script and I'm trying to implement a back button. The program records the last object coloured in the textfield LastType. I'm trying to use the content of LastType as the name of the object I want to colour white (thus hiding it from sight). However I can't get it to recognise Test as the name of a property/object. How can I do this?
Back_btn.addEventListener(MouseEvent.CLICK, BackClick);
function BackClick(event:MouseEvent):void{
var variable = textField1.text;
var es:Number = variable;
es = 0xFFFFFF;
var Test:Object = LastType[text]
var colorTransform:ColorTransform = Test.transform.colorTransform;
colorTransform.color = es;
Test.transform.colorTransform = colorTransform;
}
Thanks in advance!
This solution from Organis worked;
var Test:DisplayObject = getChildByName(LastType.text);
Many Thanks! :-)

Related

Update text field into movie clip from main timeline code

I'm working in a project for basketball. I have an issue, all my code works great if all my components are in the main timeline.
But as soon as I convert the text fields into a movie clip so I can animate and apply alpha value, all stops working.
what am I doing wrong ? The only solution that I could think of is writing the result of my countdown into the text field in the movie clip, but it didn't work as well.
this is my code.
function onTimer ( ev:TimerEvent ) : void {
timeRemaining--;
if (timeRemaining < 0) {
timeRemaining = 0;
loseGame();
}
else
showTime.text = formatTimeRemaining ();
var miReloj:MovieClip;
var titulo_txt:TextField = new TextField();
titulo_txt.text = formatTimeRemaining ();
addChild(miReloj);
miReloj.addChild(titulo_txt);
// miReloj.addChild(showTime1.text);
//miReloj.showTime1.text = formatTimeRemaining ();
}
There's never a value being assigned to miReloj, that's why it's null
var miReloj:MovieClip; // no assignment here
var titulo_txt:TextField = new TextField();
titulo_txt.text = formatTimeRemaining ();
addChild(miReloj); // miReloj is still null here
miReloj.addChild(titulo_txt); // cannot call method on null
But as soon as I convert the text fields into a movie clip
That's impossible. From your code it looks like what you actually want to do is to add the TextField object to a container. There's no need to use a MovieClip for that, simply create a Sprite object:
var container:Sprite = new Sprite(); // create container
var title:TextField = new TextField();
title.text = formatTimeRemaining();
addChild(container); // add container
container.addChild(title); // add title to container
Always use English for all your programming. Don't mix other languages into it. It becomes a pain to read your code and decreases the number of people that can help you if you have problems with your code.

Can anyone help me change this code from AS2 to AS3

This will be a simple clock. I've been using AS2 since I learned Flash in early 2000s. It's time to move on.
for (a=1; a<60; a++) {
duplicateMovieClip("dot0", "dot"+a, 10+a);
_root["dot"+a]._rotation = a*6;
_root["dot"+a].gotoAndStop(1);
}
Adobe published ActionScript 2.0 Migration that helped me tremendously back in the day.
Some specifics related to your code:
Properties aren't prefixed with underscores, ex _rotation is now rotation, and _root is now root.
root is not global, it is a property of display objects, and it is null if the display object is not in the display list.
duplicateMovieClip does not exist anymore. You should export your symbol to a class and use new operator and addChild() to create an instance and add it to the display, ex:
var dot:Dot = new Dot();
addChild(dot);
Display objects created in code are not automatically given a name and assigned to a property on its parent when added to the display. You can set the name and use getChildByName on its parent. Example:
var dot:Dot = new Dot();
dot.name = "dot" + i;
addChild(dot);
var n:int = 10;
var dot10:Dot = getChildByName("dot" + n) as Dot;
But this is a bit cumbersome, so in most cases it makes more sense to just store your display objects in your own array and reference them by index:
var dots:Array = [];
var dot:Dot = new Dot();
addChild(dot);
dots.push(dot);
var firstDot:Dot = dots[0];
That should get you started.
Thanks to help here and elsewhere, this is what works for me:
var i:Number = 1;
var dots:Array = [];
for (i=0; i<60; i++) {
var dot:Dot = new Dot;
addChild(dot);
dots.push(dot);
dot.x=683;
dot.y=436;
dot.rotation = i*6;
dot.gotoAndStop(1);
}
The clock I'm making don't have hands but the dots change color for the hours minutes and seconds. Thanks everybody who helped

Loading images using the URLRequest

recently started to learn ActionScript 3 and already have questions.
question remains the same: I'm uploading a picture using the object Loader.load (URLRequest). Loaded and displayed a picture normally. But it is impossible to read the values of attributes of the image height and width, instead issued zero. That is, do this:
var loader:Loader=new Loader();
var urlR:URLRequest=new URLRequest("Image.jpg");
public function main()
{
loader.load(urlR);
var h:Number = loader.height;// here instead of the width of the image of h is set to 0
// And if you do like this:
DrawText(loader.height.toString(10), 50, 50); // Function which draws the text as defined below
// 256 is displayed, as necessary
}
private function DrawText(text:String, x:int, y:int):void
{
var txt:TextField = new TextField();
txt.autoSize = TextFieldAutoSize.LEFT;
txt.background = true;
txt.border = true;
txt.backgroundColor = 0xff000000;
var tFor:TextFormat = new TextFormat();
tFor.font = "Charlemagne Std";
tFor.color = 0xff00ff00;
tFor.size = 20;
txt.x = x;
txt.y = y;
txt.text = text;
txt.setTextFormat(tFor);
addChild(txt);
}
Maybe attribute values must be obtained through the special features, but in the book K.Muka "ActionScript 3 for fash" says that it is necessary to do so. Please help me to solve this. Thanks in advance.
Well it's simple.
Flash is focused on the Internet, hence such problems.
If you wrote loader.load (urlR); it does not mean loaded. Accordingly, prior to the event confirming the end of loading, in loadare Null
if, instead of certain functions would be more code that would perhaps tripped your approach.
Yeah plus still highly dependent on the size of the file that you read.
Well, in general it all lyrics. Listen event on loader.contentLoaderInfo.addEventListener (Event.INIT, _onEvent), onEvent and read properties.
You need to wait for your image to load to be able to get values out of it.
Attach an eventListener to your URLLoader.
var urlR:URLRequest = new URLRequest("Image.jpg");
loader.load(urlR);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
function loader_complete(e:Event): void {
// Here you can get the height and width values etc.
var target_mc:Loader = evt.currentTarget.loader as Loader;
// target_mc.height , target_mc.width
}
Loader

AS3 multiple textfields made easy

I am working on a Results page for my game as well as upgrade page and looking for an easy way to do many textfields. I have a format for my text that takes care of font, colour, and size, but looking for an easy way to do the width and height of textfields to increase all at the same time.
I have been informed about a "with" keyword that may work but do not understand how to implement this within my program and essentially want to shorten my results class if possible.
Thank you,
The best way would be to create a custom function for generating textfield.
The example can be found in the livedocs itself.
So something like the following should suffice :
private function createCustomTextField(x:Number, y:Number, width:Number, height:Number):TextField {
var result:TextField = new TextField();
result.x = x;
result.y = y;
result.width = width;
result.height = height;
return result;
}
You may also set a default value to each attribute in the function.
private function createCustomTextField ( x:Number= <Default Value>, ...
Use it to add a textfield inside the container form.
var container:Sprite = new Sprite(); // New form container
container.addChild(createCustomTextField (20,20,50,50)); // Text Filed 1
container.addChild(createCustomTextField (20,50,50,50)); // Text Filed 2
addChild(container); // Add to current class
You may want to modify the function to accept a name so that each variable can be accessed later.
As far as I am aware, you can't use a "with" keyword to target multiple objects. Here's the documentation for it: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/statements.html#with
What I've done in the past is just make an array of all the targets, and then write a loop to apply properties to each:
var textFormat:TextFormat = generateMyCustomTextFormat();
var textField1:TextField = new TextField();
var textField2:TextField = new TextField();
//...
var textField3:TextField = new TextField();
var targets:Array = [textField1, textField2, textField3];
for(var i:int=0; i<targets.length; i++)
{
targets[i].defaultTextFormat = textFormat;
targets[i].width = 250;
//...
}

insert string info into addressing a movieclip in as3?

I am trying to dynamically address different instances of the same movieclip by passing a string into the movieclip address, but apparently I don't know what I'm doing. I'm trying something like below:
var vsTargetName:String;
vsTargetName = "instance50";
vsTargetName + vsThumb.thumbHighlight.visible = true;
Is something like this possible? What am I doing wrong/what do I need to do to make that work?
You could use getChildByName.
For example:
var vsTargetName:String = "instance50";
//container is a parent of vsTarget
var vsTarget:MovieClip = container.getChildByName(vsTargetName);
vsTarget.thumbHighlight.visible = true;
You can interchange object notation and array notation for DisplayObjects.
So:
var vsTargetName:String = "instance50";
this["instance50"] === this.instance50 === this[vsTargetName]
Hope that helps. You can use different combinations to select what you need:
var num:String = 50;
this["instance" + num]
The code above is very useful for loops when the MovieClip names are numbered.
In AS2 you had to call the eval() to convert a string into their relative stage object. Not sure how you would do it in AS3, I'm new to it.
var myID = "someObjectID";
var myObject = eval("someParent." + myID);
myObject._visible = false;