AS3: Textfield active state - apache-flex

I would like to create some text on the stage that does not respond to mouse activity. I've found that after adding a piece of text to a class I am using to draw, it covers up some of the mousing area of my class and disables the user from being able to roll over the other graphical elements...
Is there something like:
var t:TextField = new TextField();
t.text = "hello";
t.active = false; //i know that this is wrong, but I haven't found an equivalent method
Thanks,
jml

Use this
t.selectable = false;

Related

TabNavigator not showing children

I have a TabNavigator component which is not showing its children when adding them at runtime.
Is there a way around this please?
I'm doing the following at the moment:
var tabNavigator:TabNavigator = new TabNavigator();
// etc.
parentHBoxContainer.addChild(tabNavigator);
// etc.
// Custom component with nothing special in it
// Trigger on a button click (Add)
var myComponent:MyComponent = new MyComponent();
var nextTabIndex:int = tabNavigator.getChildren().length;
tabNavigator.addChild(myComponent);
// I see some text flashing but the current tab stays the same
// No text is added
tabNavigator.validateNow();
tabNavigator.selectedIndex = nextTabIndex;
Sorry guys for not giving enough details. The tabNavigator is inside an HBox container, I didn't add the code because I thought it would be irrelevant.
I replaced SimpleComponent by MyComponent in the code above. MyComponent is just another HBox with a textfield and a label.
Thanks.
Try to put this:
tabNavigator.addChild(myComponent);
tabNavigator.selectedChild = myComponent;
instead of this:
var nextTabIndex:int = tabNavigator.getChildren().length;
tabNavigator.addChild(myComponent);
// I see some text flashing but the current tab stays the same
// No text is added
tabNavigator.validateNow();
tabNavigator.selectedIndex = nextTabIndex;

Add Child immediately in Flex 3

I'm having a problem similar to FLEX: dialog not display immediately . Code follows:
private function saveBitmap(event:ContextMenuEvent):void
{
loadingScreen.visible = true;
loadingScreen.appLoadingText.text = "Preparing bitmap...";
addChild(loadingScreen);
validateNow();
var bmpd:BitmapData = new BitmapData(canv.width, canv.height);
bmpd.draw(canv);
var fr:FileReference = new FileReference();
fr.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, removeLoadingScreen);
fr.addEventListener(Event.CANCEL, removeLoadingScreen);
var png:PNGEncoder = new PNGEncoder();
var iba:ByteArray = png.encode(bmpd);
fr.save(iba, "export.png");
}
Basically, bmpd.draw and/or png.encode are dog slow, so I'd like to have a nice "please hold while we prepare your png" dialog to appear. I can't use callLater() because of the FileReference.
And just for good measure, the loading screen appears at the same time the save dialog appears from the call to fr.save().
Any ideas?
Cheers!
You're adding the child in this function. Are you doing any other work to the loadingScreen, such as sizing it? Or positioning it? Most commonly this is done in updateDisplayList(). What container are you using?
Are you sure the Z-order of your two children is correct? You can swap children with the swapChildren method

How to do something after effect animation ends in Flex?

I'm a beginner in Flex so there must be more elegant way of doing this.
//move effect
private var m:Move = new Move();
//this function creates labels with some text and starts move effect on them
public function moveText(i:int):void {
var myLabel:Label = new Label();
myLabel.text = "some text";
m.target = myLabel;
...
m.play();
}
Method moveText is called in a loop so I guess that labels don't get "garbage collected".
What I want to do is to remove Labels created in moveText method after play animation ends.
Another way of doing this is maybe creating some kind of "pool" of labels which I would use to move arround text. I don't know how would I return labels in to "pool".
The question is how to do something after effect animation ends?
You can listen to the EffectEnd event.
Check out here
Look at the effectEnd event in the Effect class. You can put a handler in there that does your garbage collection.

How do i know that which button is clicked in flex?

for (iss = 0; iss < listOfProductIds2.length; iss++)
{
// Alert.show(listOfProductIds2[iss]);
var productMain:VBox=new VBox();
var p1:HBox=new HBox();
var l1:Label=new Label();
var b1:Button=new Button();
var spacer:Spacer=new Spacer();
spacer.width=300;
b1.label="Remove";
b1.setConstraintValue("id","");
b1.addEventListener(MouseEvent.CLICK,removeProduct);
l1.text="Product "+iss;
p1.setActualSize(500,500);
p1.addChild(l1);
p1.addChild(spacer);
p1.addChild(b1);
productMain.addChild(p1);
}
function removeProduct(event:MouseEvent):void
{
// How do i know which button is clicked
}
Use event.currentTarget (instead of event.target) because event.target might be the Label component or some styling component within the button, but currentTarget is assured to be the object with which the listener was registered.
To get a handle to the button that was clicked you can just cast the currentTarget to a button.
function removeProduct(event:MouseEvent):void
{
var b1:Button = Button(event.currentTarget);
}
The method setConstraintValue is for setting layout constraints, not setting id. The id property is used by mxml for creating variable names for objects. You can get/set id as you would get/set any other property (say width) - but neither have I seen anyone doing that nor do I see any need to do that in the first place.
event.target should point to the Button you clicked on, shouldn't it ? However you should probably give ids to the buttons to be able to differenciate them (since you create them dynamically.)
Look at event.target.
If ids are assigned dynamically as in the example given b1.id = "button_" + listOfProductIds2[iss]
Then the function that processes the click event would look at the currenttarget, and what I usually do is do a string replace on the part of the id that you know is not dynamic like "button_" with "", which leaves you with the name of the product.

Can an overlapping sibling prevent an Event?

Below is the code for a simple Flex actionscript project. A sprite is partially covering a hyperlink. What's happening is that when you hover over the sprite, if you're also hovering over the hyperlink, the hyperlink is activated. I want to prevent that. I want the hyperlink to be activated only when the mouse hovers over it -- but not when the mouse hovers over the sprite which covers it.
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TextEvent;
import flash.text.Font;
import flash.text.StyleSheet;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class SpriteHyperlinkTest extends Sprite
{
private var style : StyleSheet = new StyleSheet();
public function SpriteHyperlinkTest()
{
createOutputTextField();
}
public var output_txt : TextField;
private function createOutputTextField() : void {
// set styles
var hover : Object = new Object();
hover.fontWeight = "bold";
hover.color = "#0000FF";
var link : Object = new Object();
link.fontWeight = "bold";
link.textDecoration = "underline";
link.color = "#555555";
var active : Object = new Object();
active.fontWeight = "bold";
active.color = "#FF0000";
var visited : Object = new Object();
visited.fontWeight = "bold";
visited.color = "#cc0099";
visited.textDecoration = "underline";
style.setStyle("a:link", link);
style.setStyle("a:hover", hover);
style.setStyle("a:active", active);
style.setStyle(".visited", visited);
output_txt = new TextField();
output_txt.backgroundColor = 0xFFFFFF;
output_txt.background = true;
//output_txt.embedFonts = true;
output_txt.wordWrap = true;
output_txt.multiline = true;
output_txt.name = "output_txt";
output_txt.x = 100;
output_txt.y = 100;
output_txt.width = 300;
output_txt.height = 200;
output_txt.htmlText = "<b>sample <a href='http://www.google.com'>hyperlink text</a></b>";
addChild(output_txt);
var mySprite:Sprite = new Sprite();
mySprite.graphics.lineStyle(.5,0x000000);
mySprite.graphics.beginFill(0xff0000, 1);
mySprite.alpha = .7;
mySprite.graphics.drawRect(100, 100, 90, 20);
mySprite.graphics.endFill();
mySprite.useHandCursor = true;
mySprite.mouseChildren = true;
mySprite.buttonMode = true;
mySprite.name = "Sprite1";
this.addChild(mySprite);
output_txt.styleSheet = style;
}
}
}
I've almost found a workaround. It is just amazing that there isn't some object that you can place on top of the textfield in order to prevent its seeing the mouse overing over a hyperlink.
Here, for the record, are some of the things I've tried.
stopPropagation () // didn't work
stopImmediatePropagation(P //didn't work
Because the textfield and the sprite are siblings on the displaylist, I think all of the event propagation stuff is irrelevant. All of that has to do with ancestors and descendants, but what we're dealing with here is neither.
I tried placing another ('cover') textfield on top of the 'target' textfield. I tried setting the cover.visible = false; but that just meant it effectively wasn't there. I tried setting its alpha to .1, but that didn't work either -- just like the covering sprite, it allowed the mouseOver to go through it, so that the hyperlink responded.
I thought about trying to use preventDefault () on the hyperlink but a) I don't know how to reference the hyperlink (it has no ID) and b) the only event that is dispatched from a hyperlink is the TextEvent, and that's when it's clicked. We're not clicking, we're hovering. So I don't know what event to cancel.
The other thing I thought to do was to a kind of a fake 'cancel'. That is, maybe I could set the textformat, or style, of the hyperlink to look like normal text while the mouse is hovering over the sprite. The hyperlink would actually be being activated, but it would look like it isn't being activated because the style would be changed. This is what worked.
But it's only a fake visual workaround...
I'd add a rollOver (or whatever event you're trying to block) event listener to the sprite on top of the hyperlink. In the rollover handler, be sure to call event.stopImmediatePropagation() and that should prevent the hyperlink underneath from receiving the event.
I've never seen that sort of behavior before. Your sample code doesn't even assign events. I would be curious to see how you are assigning the events, because that in itself could be the cause of your problem.

Resources