Moving textfield with mouse - actionscript-3

This is the code I have for creating a text box when clicking a button:
TextBoxButton.addEventListener(MouseEvent.CLICK, addTextBox);
function addTextBox(e:MouseEvent):void
{
myFont = new Font2();
typeText:TextField = new TextField();
typeText.defaultTextFormat = myFormat;
ttypeTextext.x = 540;
typeTexteText.width = 175;
ttypeTextText.type = TextFieldType.INPUT;
typeTexteText.text = "Your text here";
//Adding to the stage.
newLayer.addChild(typeableText);
}
My question is: how do I modify this code so that the user can drag and drop the text to wherever he/she wants on the stage using the mouse?
EDIT: Some further code:
TextBoxButton.addEventListener(MouseEvent.CLICK, addTextBox);
function addTextBox(e:MouseEvent):void
{
var myFont = new Font2();
var typeableText:TextField = new TextField();
typeableText.defaultTextFormat = myFormat;
typeableText.antiAliasType = AntiAliasType.ADVANCED;
typeableText.embedFonts = true;
typeableText.x = 540;
typeableText.y = 400;
typeableText.width = 175;
typeableText.height = 200;
typeableText.wordWrap = true;
typeableText.type = TextFieldType.INPUT;
typeableText.text = "Your text here";
interfaceLayer.buttonMode = true;
//Adding to the stage.
appLayer.addChild(typeableText);
if (typeableText.stage)
{
SideBarText.removeChild(TextBoxButton);
}
}
appLayer.addEventListener(MouseEvent.MOUSE_DOWN, dragText);
appLayer.addEventListener(MouseEvent.MOUSE_UP, stopDragText);
function dragText(e:MouseEvent):void
{
appLayer.startDrag();
}
function stopDragText(e:MouseEvent):void
{
appLayer.stopDrag();
}

Related

Volume control after loop as3

I have a problem with my code. The code works great with one exception, if I have pressed the mute-button and the animation loops, it's always back to volume = 1. How can it loop without always going back to volume = 1?
var mySound:Sound = new Sound();
var songURL:URLRequest = new URLRequest("Lyd/jetpass.mp3");
var channel1:SoundChannel = new SoundChannel();
var volumeAdjust:SoundTransform = new SoundTransform();
mute1.addEventListener(MouseEvent.CLICK, muteLyd);
volumeAdjust.volume = 1;
mySound.load(songURL);
channel1.soundTransform = volumeAdjust;
channel1 = mySound.play();
function muteLyd(e:MouseEvent):void {
if(volumeAdjust.volume == 1){
volumeAdjust.volume = 0;
}
else {
volumeAdjust.volume = 1;
}
channel1.soundTransform = volumeAdjust;
}
Just found out this code solves the problem:
var mySound:Sound = new Sound();
var songURL:URLRequest = new URLRequest("Lyd/jetpass.mp3");
var channel1:SoundChannel = new SoundChannel();
mute1.addEventListener(MouseEvent.CLICK, muteLyd);
unmute1.addEventListener(MouseEvent.CLICK, unMuteLyd);
mySound.load(songURL);
channel1 = mySound.play();
function muteLyd(evt:MouseEvent){
SoundMixer.soundTransform = new SoundTransform(0);
unmute1.visible = true;
mute1.visible = false;
}
function unMuteLyd (evt:MouseEvent){
SoundMixer.soundTransform = new SoundTransform(1);
unmute1.visible = false;
mute1.visible = true;
}

Child not added, when check index it shows -1

I am not sure why i addedChild but when i check it's index it still shows -1.
What i am trying to do is:
1.)Loop a URLRequest and load bitmap pictures.
2.)Put them in individual _contentHolder
3.)Put everything in a viewport
4.)Check the index of the image when its clicked
5.)Display just the picture that is clicked(With a black background) (Picture viewer)
6.)When the picture or background is clicked again, it closes the "picture viewer"(The single picture with black background), and just display the list from before.
At the moment i can upload the pictures in a loop, then add them into a viewport, but i can not managed to get the index of the image and reload it.
Thanks for your time!
Code:
public var _contentHolder:Sprite = new Sprite;
public var _contentHolder1:Sprite;
public var loadedArray:Array = new Array;
public var blackBox:Sprite = new Sprite();
private var somedata:Array;
protected var Holder:Listing9 = new Listing9;
public var viewport:Viewport = new Viewport();
public var scroller:TouchScroller = new TouchScroller();
var my_url:Array = somedata;
function loadImage():void
{
somedata = SearchVectorTest.lists;
for (var i:int = 5; i < somedata.length; i++)
{
if (somedata[i])
{
var loader:Loader = new Loader();
loader.load(new URLRequest("http://www.rentaid.info/rent/" + somedata[i]));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
}
}
}
function onImageLoaded(e:Event):void
{
loadedArray.push(e.currentTarget.loader.content as Bitmap);
for (var i:int = 0; i < loadedArray.length; i++)
{
var currentY1:int = 200;
e.currentTarget.loader.content.height = 200;
e.currentTarget.loader.content.y += currentY1;
currentY1 += e.currentTarget.loader.content.height + 300;
_contentHolder.mouseChildren = false; // ignore children mouseEvents
_contentHolder.mouseEnabled = true; // enable mouse on the object - normally set to true by default
_contentHolder.useHandCursor = true; // add hand cursor on mouse over
_contentHolder.buttonMode = true;
_contentHolder.addChild(loadedArray[i]);
}
var viewport:Viewport = new Viewport();
viewport.y = 0;
viewport.addChild(_contentHolder);
var scroller:TouchScroller = new TouchScroller();
scroller.width = 300;
scroller.height = 265;
scroller.x = 10;
scroller.y = 100;
scroller.viewport = viewport;
addChild(scroller);
_contentHolder.addEventListener(MouseEvent.CLICK, gotoscene);
}
loadImage();
public function gotoscene(e:MouseEvent):void
{
var searchString = loadedArray;
var index:Number;
index = searchString.indexOf(e.target);
trace(index);
trace(_contentHolder);
trace(_contentHolder.parent);
blackBox.graphics.beginFill(0x000000);
blackBox.graphics.drawRect(-1, -1, stage.width, stage.height);
blackBox.alpha = 0.7;
addChild(blackBox);
Holder.height = 300;
Holder.width = stage.width;
Holder.x = 0;
Holder.y = 100;
trace(blackBox);
trace(blackBox.parent);
addChild(Holder);
}
function gotoscene1(e:MouseEvent):void
{
removeChild(Holder);
removeChild(blackBox);
}
The indexOf you are using strict equality (===). e.target is of type Sprite and your array is filled with Bitmaps.
The strict equality will fail because the objects are not of the same type.
You need to use items of the same type to make the comparison succeed.

distortion in textField

I have a bitmap data which draw a text field. after scaling, text are distortion.
I using following code:
// tf is text Field and bm is bitmap.
var tf:TextField = new TextField();
tf.text = "Hello world";
var bd:BitmapData = new BitmapData(200, 200, true, 0x00ff00);
bd.draw(tf);
var bm:Bitmap = new Bitmap(bd);
addChild(bm);
bm.scaleX = 2;
bm.scaleY = 2;
Please guide me.
You should use transform matrix to draw an upscaled text field (or any other vector graphics object) onto a BitmapData.
var mat:Matrix=new Matrix();
mat.scale(2.0,2.0);
bd.draw(tf,mat);
First, increase the font size and then convert and scale as bitmap like so,
//---- Text format ----
var textFormat:TextFormat = new TextFormat();
textFormat.font = "Arial";
//textFormat.bold = true;
textFormat.size = 40;
//---------------------------------------------
//
var tf:TextField = new TextField();
tf.text = "Hello world";
/*tf.antiAliasType = AntiAliasType.ADVANCED;
tf.gridFitType = GridFitType.PIXEL;
tf.thickness = 0;
tf.sharpness = 0;*/
tf.setTextFormat(textFormat);
//
//
var bd:BitmapData = new BitmapData(200,200,true,0x00ff00);
bd.draw(tf);
var bm:Bitmap = new Bitmap(bd);
bm.smoothing = true;
addChild(bm);
bm.scaleX = 2;
bm.scaleY = 2;
Best luck.

AS3 Set Combobox background color?

I have the following code which styles my comboBox drop down, but I can't seem to get a background color to work. I've used // comments to show which lines it doesn't like:
var myTextFormat:TextFormat = new TextFormat();
myTextFormat.font = "Arial";
myTextFormat.color = 0xFFFFFF;
myTextFormat.size = 10;
myTextFormat.background = true; // Doesn't like this line
myTextFormat.backgroundColor = 0xFFFFFF; // Doesn't like this line
var m = 0;
function comboBoxChange(e:Event):void {
var scriptVars:URLVariables = new URLVariables();
trace("YOU CHANGED ME!!!");
//scriptVars["InstructorName" + [m]] = myXML.Instructors.InstructorName[m];
trace(e.target.selectedItem.label);
//Change the comboBox color if Instructor selected
// Go back to plain if no Instructor
if (e.target.selectedIndex == 0){
trace("EMPTY");
e.target.transform.colorTransform = new ColorTransform;
}
else
{
// Shade of black indicates slot allocated to Instructor
my_color.color = 0x002222;
//e.target.transform.colorTransform = my_color;
e.target.parent.alpha = 1;
e.target.textField.setStyle ("textFormat",myTextFormat);
}
}
Maybe what you're looking for is the contentBackgroundColor style of ComboBox?
s|ComboBox {
contentBackgroundColor: red;
}
or
combo.setStyle('contentBackgroundColor', 'red');
This seemed to work:
e.target.textField.textField.background = true; e.target.textField.textField.backgroundColor = 0x000000;

AS3 - positon advertisment that is dynamically loaded

I'm using the code below to place an ad inside a mobile game that I'm building. My problem is that the ad appears in the top left corner of the stage and I need to be able to move it centered near the bottom. I got this code from a book and I understand about 70% of what it's doing. Thanks for any direction you can offer.
Rich
// Smaato Advertising Code for Start Screen
var request:URLRequest = new URLRequest("http://soma.smaato.com/oapi/reqAd.jsp");
var variables:URLVariables = new URLVariables();
variables.adspace = "0";
variables.pub = "0";
variables.devip = "127.0.0.1";
variables.format = "IMG";
variables.adcount = "1";
variables.response = "XML";
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(request);
function onComplete(e:Event):void
{
var data:XML = new XML(loader.data as String);
var status:String = data.*::status.toString();
if(status == "success")
{
var ad:XMLList = data.*::ads.*::ad;
var link:String = ad.*::link.toString();
var l:Loader = new Loader();
l.load(new URLRequest(link));
addChild(l);
var clickurl:String = ad.*::action.#target.toString();
l.addEventListener(MouseEvent.CLICK, onAdClick);
function onAdClick(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(clickurl);
navigateToURL(request);
}
}
}
The problem is that you are never actually positioning the image, so the default is [0,0], the top-left corner of the screen.
var request:URLRequest = new URLRequest("http://soma.smaato.com/oapi/reqAd.jsp");
var variables:URLVariables = new URLVariables();
variables.adspace = "0";
variables.pub = "0";
variables.devip = "127.0.0.1";
variables.format = "IMG";
variables.adcount = "1";
variables.response = "XML";
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete);
loader.load(request);
function onComplete(e:Event):void
{
var data:XML = new XML(loader.data as String);
var status:String = data.*::status.toString();
if(status == "success")
{
var ad:XMLList = data.*::ads.*::ad;
var link:String = ad.*::link.toString();
var l:Loader = new Loader();
l.load(new URLRequest(link));
l.contentLoaderInfo.addEventListener(Event.COMPLETE, onAdLoaded);
function onAdLoaded(e:Event):void
{
var ad:DisplayObject = e.target.content;
addChild(ad);
ad.x = (ad.stage.stageWidth - ad.width) / 2;
ad.y = (ad.stage.stageHeight - ad.height) / 2 + 50;
}
var clickurl:String = ad.*::action.#target.toString();
l.addEventListener(MouseEvent.CLICK, onAdClick);
function onAdClick(e:MouseEvent):void
{
var request:URLRequest = new URLRequest(clickurl);
navigateToURL(request);
}
}
}
That will center the loaded file. The 50 number is because if you don't want it to be in the exact center, you can use the number to adjust the y-value accordingly.
By the way, I recommend you to grab another book, becase the code you are using seems a bit old and have poor performance.
If you have any other issue regarding this, comment it and I can expand my answer.