As3 text field issue - actionscript-3

I'm trying to make a button(movieClip-button), that when you hover over it(MOUSE_OVER), it calls a function that displays some text. The only problem is that it doesn't work :p. Or atleast not the way i want it to work. The thing is when i hover over it the first time, nothing displays. If i then remove the mouse from the movieclip and hover over it again it works just fine. Here's my code:
private var priceString:TextField = new TextField();
private function addText(price:String):void{
var priceStringFormat = new TextFormat;
priceStringFormat.color = 0xFF0000;
priceStringFormat.font = 'TradeGothic';
priceStringFormat.size = 30;
priceString.x = 285;
priceString.y = 15;
priceString.setTextFormat(priceStringFormat);
priceString.autoSize = TextFieldAutoSize.LEFT;
priceString.text = "Upgrade Costs: " + price;
getStage.addChild(priceString);
}
I can't myself see the problem:s. Other text fields in the same format in the same class works just fine. The getStage var is holding the stage access. (It works with other text fields). Strange is also that if i try to add a movieclip instead of the textfield, it works just fine.
This is how it should look:
http://i.stack.imgur.com/5a0jf.png

setTextFormat needs to happen after you set the text property. If for whatever reason you need to do the formatting before you set the textFormat, use
priceString.defaultTextFormat = priceStringFormat

If you're saying you want to create a tooltip when you hover over a button, you should probably put the TextField into a Sprite object. Add the TextField as a child of the Sprite, and the Sprite as a child of the stage. Then, either tween the alpha value of the Sprite or toggle its visibility using Sprite.visible.
PS: for a detailed tutorial, see:
http://hub.tutsplus.com/tutorials/create-a-customizable-tooltip-in-actionscript-30--active-1793
EDIT:
Based on the image you provided, what you would need is to create a sprite with the TextField as its child in the constructor of your button, and set the sprite's visible property to false.
In your mouseover handler for the button, set the sprite's visible property to true, and in reset it in your mouseout handler.

Related

Actionscript: BlitMask stopping mouse event listeners from working, how to fix?

I have a button class which contains event listeners to trigger an animation of the button on click.
I used many instances of this class to form a list which the user can scroll through. I have implemented BlitMask, this works, however the mouse event listeners in the button class no longer work. This code
_blit = new BlitMask(_mc, _obounds.x, _bounds.y, _bounds.width, _bounds.height, false);
Is what stops the button class.
How can I get the behaviour pre blitmark?
My code which creates the bottom is
var tf:TextField = new TextField(text);
tf.x = 70;
tf.y = 20;
_btn.addChild(tf);
_btn.addEventListener(MouseEvent.CLICK, click);
click is never called.
The blitmask causes the movieclip to be nothing more than an image of clip.
bitmapMode must be set to false to correct this.
_blitMask.bitmapMode = false;
EDIT: To expand on a further issue that implementing this solution causes, turning bitmapMode on only when needed causes the bitmap to not reflect changes in the classes that changed while bitmapMode was off. So you must set
_blitMask.update(null, true);
To force a full update when you use
_blitMask.bitmapMode = true;

Allow user to add and manipulate text

I am trying to figure out how to allow the user to be able to click a button to add a text field to the stage, type in their own text and then be able to freely move the text around with the mouse to position it where they want.
I have an idea on how to be able to position the text but not sure on the best way for managing the text input itself. I'd imagine it's quite difficult to be able to allow the user to 'click and drag' to draw a textbox and type in it / position it themselves? Is there an efficient way of doing this that I just don't know yet?
You can modify the position of a DisplayObject, of which TextField is a child.
TextField myMovingTextBox = new TextField;
myMovingTextBox.x = 123;
myMovingTextBox.y = 436;
//Adding to the stage.
stage.addChild(myMovingTextBox);
You can make an instance of TextField type-able by the user via its type property.
myMovingTextBox.type = TextFieldType.INPUT;
//Additional options
//myMovingTextBox.border = true;
//myMovingTextBox.restrict = "0-9";
You can learn the current mouse position via a MouseEvent triggered by the stage.
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
...
function onMouseMove(event:MouseEvent):void {
//Mouse Pos
event.stageX;
event.stageY;
}

Actionscript event listener on rectangle containing larger TextField

I have a rectangle on which I would like to display a label. I have tried to do this by creating a rectangle sprite, and then adding a textField to the display tree of the sprite.
The problem is that there seems to be a lot of extra blank padding surrounding the textField. Although the text fits within the box, the boundaries of the textField extend beyond the visible region of it's containing rectangle. This causes the rectangle's width and height to change also.
The issue is that I want the user to be able to drag the rectangle around the screen. I added an event listener on MOUSE_DOWN to initiate the drag. However, the user can start the drag by clicking on the area surrounding the visible rectangle, rather than only on the rectangle itself. I assume this is because the user will actually be clicking on the extra blank space coming from the the TextField and seeping out over the edges
Any ideas?
I think what you're looking for is the textField.autoSize parameter. It makes the text field bounds shrink to the size of the text (otherwise it has a default height/width regardless of the text it contains)
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
var textField:TextField = new TextField();
textField.autoSize = TextFieldAutoSize.LEFT;
textField.text = "your text"; //set this AFTER autoSize
You can also set the width of the your textField to the width of your rectangle. Or forgo the autosize property and manually set the height/width of your text box to the height/width of the rectangle, though this would truncate any text that doesn't fit.
There is always some padding on the text field. Getting the exact bounds of the actual text can be tricky (though it is possible). An easier way is to just mask your text box.
If your rectangle is drawn with the graphics class, you could do this:
var rectangle:Sprite = new Sprite();
rectangle.graphics.beginFill(0xFF0000);
rectangle.graphics.drawRect(0,0,100,100);
addChild(rectangle);
var myMask:Shape = new Shape();
myMask.graphics.copyFrom(rectangle.graphics);
rectangle.addChild(myMask);
var textField:TextField = new TextField();
textField.width = rectangle.width;
textField.height = rectangle.height;
textField.mask = myMask;
rectangle.addChild(textField);
One other option you could do, is in your event listener, check to see if the target is the text box and exit the function. (just make sure your text field's mouseEnabled property is true (default) if you use this method)
function rectangleClickHandler(e:Event):void {
if(e.target == myTextField) return;
//rest of your code
}
One other thing you could do, and may be simpler than masks etc is just make the text field not receive mouse events. This will stop any mouse events being fired from interaction with the text field, but you'll still be able to process them on your rectangle.
The important item here is the mouseEnabled flag. You leave the mouseChildren enabled on your container, but disable the mouseEnabled of both the container and the textfield.
var tf:TextField = new TextField();
tf.autoSize = TextFieldAutoSize.LEFT;
tf.text = "Testing the text mouse enabled";
var rectangle:Sprite = new Sprite();
rectangle.graphics.beginFill(0xFF0000);
rectangle.graphics.drawRect(0,0,100,100);
var container:Sprite = new Sprite();
container.addChild( rectangle );
container.addChild( tf );
addChild( container );
// IMPORTANT FLAGS HERE
tf.mouseEnabled = false;
container.mouseEnabled = false;
container.mouseChildren = true;
container.addEventListener( MouseEvent.ROLL_OVER, rollOverHandler, false, 0, true );
The event handler will only fire when other children of the container are rolled over, not the text field. Using this method you can selectively active mouse enabled components in a container object.

Changing dynamic textfield inside button in as3

I am having some problems getting this code to work properly. I want to change the text on a textfield inside a button. It works, but only for the upState. As soon as I hover or click the button, it changes back to the original name. Is there any way I can define it as anyState?
var doc:DisplayObjectContainer = m1.upState as DisplayObjectContainer;
var tf:TextField = doc.getChildAt(1) as TextField;
var boldText:TextFormat = new TextFormat();
boldText.bold = true;
tf.text = "Sterno Cleido Mastoid";
tf.setTextFormat(boldText);
Example:
http://www.testdummies.dk/dynamictext.fla
Your issue is that your code is only changing the text for the up state of the button. The other states remain unaffected.
You could simply copy and paste your code to do the same change for the over and down states - adding this code after your existing code would do just that:
doc = m1.overState as DisplayObjectContainer;
tf = doc.getChildAt(1) as TextField;
tf.text = "Neck";
tf.setTextFormat(boldText);
doc = m1.downState as DisplayObjectContainer;
tf = doc.getChildAt(1) as TextField;
tf.text = "Neck";
tf.setTextFormat(boldText);
This is an awkward way though to code a simple text change for a button. Creating a custom button class, or even making a movieClip work as a button would be much cleaner. Create a new question if you need help learning either of these things.
I would put the text field on its own layer on top of the button, so it always has the same text regardless of button state.
or, alternatively, you could copy and paste that code into each button state and then alter the code to reflect the current state. (the first solution is faster/easier though)

How set the focus of a input-TextField without having a stagereference?

I got a text-InputField:
var textfield:TextField = new TextField();
textfield.text = "";
textfield.type = TextFieldType.INPUT;
addChild(textfield);
because this textfield don't reside in the main-class, I don't have a stage-reference for doing:
textfield.stage.focus = textfield;
or
stage.focus = textfield
How can I force the textfield to display the blinking line at position zero?
You can add a static reference to the stage in your main class and set focus through it (or create a dedicaced class to get stage reference wherever you are).
Main class code :
public class MainClass extends Sprite{
public static var STAGE : Stage;
public function MainClass(){
STAGE = stage;
}
}
Textfield class code :
MainClass.STAGE.focus = myTextfield;
It's sort of a flash player bug, because if you set focus with stage.focus=textfield, textfield actually has focus (you can write something), but there is no blinking cursor. On the other hand, there is a blinking cursor, but only when you launch swf locally in player (not in browser).
To inform people that they can write something right now, you can make a cursor by yourself - just create a line, animate it and hide when someone writes a first letter.
The cursor focus does work sometimes, which is all the more frustrating. I have this situation with two almost identical apps. The one that doesn't work has an animation running. It may be a conflict.
Finally I got the solution
try this code it will work
public function SetFocus(mc:TextField):void{
addEventListener(Event.ENTER_FRAME,focus);
function focus(E:Event){
stage.focus=mc;
if(mc.name.toString()==stage.focus.name.toString()){
removeEventListener(Event.ENTER_FRAME,focus);
}
}
}
you have to name the textfield then you can use it easily