i have created a group in run time and then added in it two buttons and one label
addElement (myGroup )
myGroup.addElement ( button 1 )
myGroup.addElement ( label )
myGroup.addElement ( button 2 )
now when i click on one button 2 i can get event.currentTarget.
How can i change the text of label using this event.currentTarget. How can i target the label
I believe this may solve your problem.
When you create the label Object provide it with an id. so that you can access the label through out the application using this 'id'. You can change the text by using this id.text
Or you can use the group objectId. like this one
groupObjectID.getElementAt(index).text
You can see if the event.currentTarget is Label by casting it using 'as' operator
var lbl:Label = event.currentTarget as Label;
if (lbl)
{
//do rest of processing
}
Give name to your Label label.name='lblSomething'. Then you can access by
var mylabel:Label = myGroup.getChildByName(lblSomething) as Label
Try var labelStr:String = event.currentTarget.label;
Actually I do recommend you try to create those labels and buttons as either public or private objects whenever possible so that you can always refer to them using the object ID.
This is a good practice as well... just my 2 cents.
public var t_label:Label = new Label (); // t_ just stands for temporary... nothing special
myGroup.addElement (this.t_label);
Then inside ANY event handler you can write something like this to change the label text.
private function onWhateverHandler (event:Event):void
{
this.t_label.text = "whatever new string value";
}
Related
I have a form with several component: datagrid, textArea, text input...
For each component FocusIn Event is available.
var objTarget:String;
protected function memo_focusInHandler(event:FocusEvent):void
{
objTarget=event.currentTarget.id;
}
With memo_focusInHandler, I know which has focus.
My goal is to backup last focus objet, and re open Windows with focus on this object.
I try to do this:
objTarget.setfocus();
But it doesn't work. Could you help to found the best way to reach my goal.
String is not a display object, thus it can't be in focus. The representation of string on the stage is a TextField.
In as3 you can set focus to the desired target by using stage method:
stage.focus = myTarget;
Please see the corresponding documentation section: https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Stage.html#focus
There is no need (that you've shown) to work with the string id reference. It would much simpler (and slightly more efficient) to work directly with the object reference.
var objTarget:Object; // Object instead of type :String
protected function memo_focusInHandler(event:FocusEvent):void {
objTarget = event.currentTarget; //instead of the currentTarget's id property, assign the current target itself
}
Then, when you want to reset focus, you can do:
if(objTarget is TextInput || objTarget is TextArea){ //make sure it's a text input or text area first - optional but recommended if you don't like errors
objTarget.selectRange(objTarget.text.length, objTarget.text.length); //set cursor to the end
objTarget.setFocus(); //focus the text input/area
}
I found a solution:
this[objTarget].selectRange(this[objTarget].text.length, this[objTarget].text.length);
this[objTarget].setFocus();
I'm fairly new to AS3 and I want to know how would I go about having whatever is typed into a Text Input to become a variable? For example, in a text input under name if I were to type in "Jeffrey" that would become a variable and I could then use that to display "Hello, Jeffrey" or something like that with whatever the name inputted is replaced with Jeffrey.
It's fairly simple to implement this. One way you could do this is have a keyboard event running while input is being made. Then assuming you have the textfield created and ready for input, you have a variable that stores the textfield text property. So you would create your variable:
private var inputText:String;
Next add your listener:
stage.addEventListener( KeyboardEvent.KEY_DOWN, onKeyDown );
Then create the listener function, and in that function set your variable to the textfield text. In this example let's assume your textfield is called myTextField:
private function onKeyDown( e:KeyboardEvent ):void {
textInput = myTextField.text;
}
Now you have a variable that is storing the textfield's text every time a key is pressed.
Whenever you are done with the input, remove the listener from the stage:
stage.removeEventListener( KeyboardEvent.KEY_DOWN, onKeyDown );
Now there is also an alternative way. You can attach a TEXT_INPUT event to the textfield and capture the input that way, like so:
myTextField.addEventListener( TextEvent.TEXT_INPUT, onInput );
public function onInput( e:TextEvent ):void {
inputString = myTextField.text;
}
There are two parts to this:
1: Define "something that is typed into a TextInput".
The reason for this is that each thing is typed in one keystroke at a time. This is important, because for the word "Jeffrey", you have several different things that get typed into there:
J
Je
Jef
Jeff
Jeffr
Jeffre
Jeffrey
You probably don't want all of those turned into variables, but ActionScript 3.0 will more-or-less think of those as all separate inputs. Somehow you have to distinguish "Jeffrey" from all the rest. Usually this is done by making the user click a Submit button or tab off the TextInput or something, but you'll have to decide.
2: Add a dictionary variable to your class and use the input as one of its keys:
private var myDict:Dictionary = new Dictionary();
Whenever the program decides that the user has finished typing something into the TextInput (like in a Submit button's click handler), just do this:
myDict[myTextInput.text] = new Object(); // or whatever value you want
So let's say the user entered the value "Jeffrey". Then you could come back later and access what is effectively a variable by that name:
myDict[myTextInput.text] = 5; // myTextInput.text == "Jeffrey"
.
.
.
trace(myDict["Jeffrey"]); // output is "5"
trace(myDict.Jeffrey); // output is "5"
trace(myDict[myTextInput.text]); // output is "5" if myTextInput.text is the same
Effectively rolling back an earlier edit, just go ahead and use a Dictionary, not an Object. If the user types in certain things like "12345", things will just go better if you use a Dictionary.
I have a movie clip called radio and flashlight. Inside the movieclip, there is a text box with words inside. The instance name of the textbox inside radio is called 'radioText' and the instance name of the textbox inside flashlight is called 'flashlightText'. I want the color of the text to turn white when hovering over the movieclip.
The code below works for changing radioText but not flashlightText:
var containers = [radio, flashlight];
for (var i:int = 0; i<containers.length; i++) {
containers[i].addEventListener(MouseEvent.MOUSE_OVER, hOver);
}
var whiteFont:TextFormat = new TextFormat();
whiteFont.color = 0xFFFFFF;
function hOver(evt:Event):void {
evt.currentTarget.radioText.setTextFormat(whiteFont); //change radioText's color
}
what I want to do is instead of
evt.currentTarget.radioText.setTextFormat(whiteFont);
, I want to do somthing like
evt.currentTarget.(currentTarget.name + 'Text').setTextFormat(whiteFont);
but that doesn't work for obvious reasons. Is there a way to do what I want to do?
There are quite a few good ways to implement what you are asking, what you do really depends on how you are setting up your objects.
For example, your pseudo code:
evt.currentTarget.(currentTarget.name + 'Text').setTextFormat(whiteFont);
...could be implemented if you named all your child text fields identically OR if you named them accordingly to some convention (for this example I set a name for the parent and a similar name for the child). So let's assume your container has the name "container" and has a TextField child named "containerText". It would look something like this:
private function hOver(evt:MouseEvent):void {
var n:String = evt.currentTarget.name;
evt.currentTarget.getChildByName( n + "Text" ).setTextFormat( whiteFont );
}
This is now dependent on following a naming convention, which may or may not be ideal. So what might be an even better and more generic way to handle this? You can setup a class for your container object and have a TextField property there. For Example:
class myContainer extends Sprite {
public var textField:TextField;
public function myContainer():void {
textField = new TextField();
addChild(textField);
}
}
Pretty basic, but now if you added all the listeners to types of myContainer your event function could look like this:
private function hOver(evt:MouseEvent):void {
evt.currentTarget[ "textField" ].setTextFormat( whiteFont );
}
Which is probably more what you are looking for. This version allows you to grab the "textField" property of the object you applied the listener to, in this case that property would essentially be your radioText/flashlightText.
Ok so lastly, and the least favorable way to do this, assumes all you care about is that the object has a child of type TextField, it has no logical naming convention, and we are not sure what index it is at in the display list. You would have to do it something like this:
private function hOver(e:MouseEvent):void {
var i:int;
var displayObj:DisplayObjectContainer = e.currentTarget as DisplayObjectContainer;
for ( ; i < displayObj.numChildren; i++ ) {
var obj:* = displayObj.getChildAt( i );
if ( obj is TextField ) {
obj.setTextFormat( whiteFont );
}
}
}
That is also the slowest way of reaching your child object. This should cover all the bases, good luck!
What's wrong with?
DisplayObjectContainer(evt.currentTarget)[evt.currentTarget.name + 'Text'].setTextFormat(whiteFont);
I am making a soundboard and I'm using actionscript. I would like to have one function with the code to play a different sound depending on which button is pressed. I will also be playing from the library rather than a URL. Here is some real code mixed with pseudo for what I want to do:
var soundEffect:SoundEffect = new SoundEffect();
sound1_btn.addEventListener(MouseEvent:CLICK, buttonName, playSoundEffect); //possible?
function playSoundEffect(e:Event, buttonName):void {
soundEffect.attachSound = buttonName + ".mp3" //pseudo code
soundEffect.play();
}
The SoundEffect class is just the name I used in Linkage. I don't know the best way to change the sound that a class represents, or the best way to do this in general. Ideally I'd like to not create 50 different classes with 50 different sound variables and 50 functions. I'd rather each button had some sort of identifier and within the function I can use the button name or identifier to assign the appropriate sound.
If you are using a Button symbol, you could use a naming convention that encodes the class name in your button name.
So if your sound effect class name was sfx_jump , you would name your instance :
sfx_jump_btn
You then set your event listener like this :
sfx_jump_btn.addEventListener(MouseEvent.CLICK, clickHandler);
What you want to do in the clickHandler function is to first generate your className String based on the buttons name property. Then you get the Class Definition via using getDefinitionByName so that you can create an instance of the sound, the following code is how you do that :
public function clickHandler(e:MouseEvent):void
{
var button:SimpleButton = e.target as SimpleButton;
// use replace to clip off the _btn suffix
var className:String = button.name.replace("_btn","");
var SoundClass:Class = getDefinitionByName(className) as Class;
var newSound:Sound = new SoundClass();
newSound.play();
}
You also need to add this import :
import flash.utils.getDefinitionByName;
Yes you can do it. Since you have multiple buttons attach a property to each button like below I attached 'soundToPlay' property to sound1_btn.
sound1_btn.soundToPlay = "1.mp3";
sound1_btn.addEventListener( MouseEvent.CLICK, handleBtnClick);
function handleBtnClick( e:Event ):void{
soundEffect.attachSound = e.target.soundToPlay;
soundEffect.play();
}
#hrehman have the answer for you, but if you button class don't have the property soundToPlay you could use the name property as an ID. and get back with the currentTargetproperty of the event.
sound1_btn.name = "Sound1";
sound1_btn.addEventListener(MouseEvent:CLICK, playSoundEffect);
function playSoundEffect(e:Event):void {
soundEffect.attachSound = e.currentTarget.name + ".mp3";
soundEffect.play();
}
I use a Label component to display the length of an ArrayCollection. How do I get it to update when I add new items to the collection?
Here's the text field for the Label:
text="{model.meetingInfo.documentList.length}"
Here's the handler for adding a new item to the collection:
var attachmentProgressVO:AttachmentProgressVO = new AttachmentProgressVO();
attachmentProgressVO.fileReference = file as File;
newAttachmentList.addItem(attachmentProgressVO);
checkIfUpdate(file as File, attachmentProgressVO);
meetingInfo.docsAndAttachmentsList.addItem(attachmentProgressVO);
I tried adding these 2 lines but that didn't work:
meetingInfo.docsAndAttachmentsList.itemUpdated( attachmentProgressVO );
meetingInfo.docsAndAttachmentsList.refresh();
I also tried changing this:
public var docsAndAttachmentsList:ArrayCollection = new ArrayCollection();
to this:
private var _docsAndAttachmentsList:ArrayCollection = new ArrayCollection();
..with a getter and setter but that didn't work.
I'm not using the right approach, am I?
Generically, Binding only looks at a specific object; you can't drill down 4 objects deep to a specific property and expect binding to update values.
Changing the documentList does not change meetingInfo or Model, so binding will never be triggered. itemUpdated() and refresh() should update the list based class which displays the data; but will not affect your label displaying the count.
You need to listen on the collection for a collectionChange event and manually update the label's text in the collectionChange handler.