Official Design and Development Resource of ThornyRabbit

April 16, 2009

Preventing Text Field Blocking MouseClicks

Filed under: ActionScript 3 Snippets — Tags: , , , , — thornyeternity @ 10:33 am

To make text not clickable - as it may interfere with a sprite being clickable:

textFiledCreatorName.mouseEnabled = false;

Fine if this text field is nested in the same sprite.

But put the text into another sprite overlapping the one below that has the eventListener - and it still blocks it.

Solution:

otherOverlappingSprite.mouseEnabled = false; //on the overlapping sprite AS WELL AS the text field.

Adobe TextArea component - adding and controlling.

Filed under: ActionScript 3 Snippets — Tags: , , , , , , , , — thornyeternity @ 10:31 am

Imports:

import fl.controls.*;//text area component

import flash.net.*;

import flash.text.*;

 

Load html text into TextArea component:

private function loadCaseOverviewText():void {

var urlLdr:URLLoader = new URLLoader();

urlLdr.addEventListener(Event.COMPLETE, completeTextLoadHandler);

urlLdr.dataFormat = URLLoaderDataFormat.TEXT;

urlLdr.load(new URLRequest(”text_assets/advertising_overview.html”));

}//loadCaseOverviewText

 

private function completeTextLoadHandler(event:Event):void {

var str:String = event.target.data as String;

textAreaCMP.htmlText = str;

}//completeTextLoadHandler

————

Position scrollbar back at top of TextArea component when new content is loaded or a new section is navigated to using:

textAreaCMP.verticalScrollPosition = 1;

————-

Set padding in TextArea component:

textAreaCMP.setStyle(”textPadding” , 10);

———–

Disable carat (bar) cursor from showing when rolling over textArea component:

textAreaCMP.drawFocus (false);

Text Formatting & AutoSizing

Filed under: ActionScript 3 Snippets — Tags: , , , , , — thornyeternity @ 10:27 am

private function addTextCaption():void{

if(textCaption != null){

removeChild(textCaption);

}//remove if it exists

textCaption = new TextField();

var font:Verdana = new Verdana(); //linkage name in library

var format:TextFormat = new TextFormat(); //should have separate formatting function

format.font = font.fontName;

format.color = textColorVar;

format.size = infoFontSize;

format.align = TextFormatAlign.LEFT;

 

textCaption.x = thumbnailWidth + textBorder; 

textCaption.y = imageHeight + headingHeight;

textCaption.width = stage.stageWidth - thumbnailWidth;

textCaption.height =  200; //don’t need height if autosize single or multiline text

textCaption.multiline = true;

textCaption.wordWrap = true;//must be set else cuts off

//textCaption.autoSize = TextFieldAutoSize.LEFT;

textCaption.antiAliasType = flash.text.AntiAliasType.ADVANCED;

textCaption.selectable = false;

textCaption.embedFonts = true;

addChild(textCaption);

textCaption.defaultTextFormat = format;

textCaption.text = imageCaptionArray[tempCounter];

}//addTextCaption

NOTES:

To autosize height do not specify a height but must include autosize:

textCaption.wordWrap = true;

textCaption.autoSize = TextFieldAutoSize.LEFT;

textCaption.x =  textBoxXPos; 

textCaption.y = textBoxYPos;

textCaption.width = stageWide - textBoxXPos;

//textCaption.height =  stageHigh - textBoxYPos;//not allowed with autoSize

For width autosize leave out width obviously.

Easy re-usable function to creat customisable text fields on the fly.

Filed under: ActionScript 3 Snippets — Tags: , , , , — thornyeternity @ 10:24 am

var inputAddressTextField:TextField = new TextField();

genericTextField(inputAddressTextField, 50 , 4, 200 , 20 , true, 0xcc0000 , true, 0xcc99ff , true , 0xffffff); 

//obj,x,y,width,height,bg,bgColor,border,borderColor,selectable,textColor

//NOTE: just use 0 (zero) for bg and border color if false, and for sizes if autosizing

inputAddressTextField.type = TextFieldType.INPUT; //any specifics

header.addChild(inputAddressTextField);

formatTextStyle(inputAddressTextField , 14)

private function genericTextField(whichField:TextField , xpos:Number, ypos:Number , fieldWidth:Number , fieldHeight:Number , bgBool:Boolean, bgColor:uint , borderBool:Boolean , borderColor:uint , selectableBool:Boolean , txtColor:uint){

whichField.x = xpos

whichField.y = ypos;

whichField.width = fieldWidth;

whichField.height = fieldHeight;

whichField.background = bgBool;

whichField.backgroundColor = bgColor;

whichField.border = borderBool;

whichField.borderColor = borderColor;

whichField.selectable = selectableBool;

whichField.textColor = txtColor;

}//genericTextField

 

private function formatTextStyle(whichField:TextField , txtSize:Number):void{ //Set the stylesheet property before setting the content.

var format:TextFormat = new TextFormat(); //should have separate formatting function

format.size = txtSize;

format.font = “_sans”;

format.align = TextFormatAlign.LEFT;

whichField.defaultTextFormat = format;

}//addStyleSheet

3 Text Handling Methods - Input Text, Selecting Text, Returning text

Filed under: ActionScript 3 Snippets — Tags: , , , , , — thornyeternity @ 10:21 am

Create input text field:

currField.type = TextFieldType.INPUT;

currField.selectable = true; //must be true to be inputtable

———

Select text when the field is clicked on:

currField.addEventListener(MouseEvent.CLICK, highlightText);

private function highlightText(event:MouseEvent):void{

event.target.setSelection(0, event.target.length); //start point, end point

}

——–

Return string value from function call:

currField.text = scoreLabel();

private function scoreLabel():String{

var scoreString:String = “Your score: ” + scoreGot + “/” + questionTotal;

return scoreString;

}//scoreLabel

AS3 Form Validation - email address and filled field

Filed under: ActionScript 3 Snippets — Tags: , , , , — thornyeternity @ 10:16 am

Check for a valid email address in a text input field by checking for @, fullstop and positioning of these:

private function checkEmail(whichObj:String):Boolean{

//check for @

//check for .

//check @ preceeds last dot

//check that @ is not first

//email cannot have any spaces

if(whichObj.indexOf(”@”) == -1 || whichObj.indexOf(”@”) == 0){ //not present or first

emailBool = false;

}else{

emailBool = true;//if @ exists check for dot

if(whichObj.lastIndexOf(”.”) == -1 || whichObj.lastIndexOf(”.”) == whichObj.length-1){ //not present or at very end

emailBool = false;

}else{

emailBool = true;//if both are true check @ preceeds dot

if(whichObj.lastIndexOf(”.”) < whichObj.indexOf(”@”)  || whichObj.lastIndexOf(”.”) == whichObj.indexOf(”@”)+1 ){//not before and not directly after

emailBool = false;

}else{

emailBool = true;

//lastly check it has NO spaces in it

for(var i:Number = 0 ; i < whichObj.length ; i++){ 

if(whichObj.charAt(i) == ” “){ //if the character is a space

emailBool = false;

}

}

}

}

}

trace(”emailBool: ” + emailBool);

return emailBool;

}//checkEmail

————-

Check a text input field is filled in and it has NO spaces:

private function doFormCheck(event:MouseEvent):void{

checkName(form1_mc.name_txt.text);

trace(stringBool);

}//doFormCheck

private function checkName(whichObj:String):Boolean{

//check that its not empty only AND that its not just spaces

if(whichObj != “”){

//not empty so 

stringBool = true;

for(var i:Number = 0 ; i < whichObj.length ; i++){

if(whichObj.charAt(i) != ” “){ //if the character is not a space

stringBool = true;

return stringBool;//quit checking

}else{//else it is a space

stringBool = false;

return stringBool;

}

}//for

return stringBool;

}else{

stringBool = false; //else it is empty

return stringBool;

}//if

}//checkName

Animating Dynamic text onto the screen letter by letter

Filed under: ActionScript 3 Snippets — Tags: , , , , , — thornyeternity @ 10:13 am

private function setInitialContentForCaseStudy():void{

more_mc.solution_txt.text = “”;//clear to start

solutionString = solutionsArray[0];

animationCounter = 0;

animationTimer = new Timer(100, 0); 

animationTimer.addEventListener(TimerEvent.TIMER, animateContent);

animationTimer.start();

}//setInitialContentForCaseStudy

private function animateContent(event:TimerEvent):void{

more_mc.solution_txt.appendText(solutionString.charAt(animationCounter));

animationCounter++;

if(animationCounter == solutionString.length){

animationTimer.stop();

}//if

}//animateContent

Beware of other stuff happening at same time or too much text - can be slow!

Implementing Double_Click Event with AS3

Filed under: ActionScript 3 Snippets — Tags: , , — thornyeternity @ 10:10 am

oneFront.doubleClickEnabled = true; //must be enabled for it to work

oneFront.addEventListener(MouseEvent.DOUBLE_CLICK, rotateCard);

April 14, 2009

Html tags in Xml for Flash (AS2)

Filed under: Flash — Tags: , , , — thornyeternity @ 9:00 am

Put the the following (bolded) into each xml node requiring html tags in it:

<xmlNode><![CDATA[   Here is my <br/> html text   ]]></xmlNode>

Now reads as one tag without needing to do things like &lt;b&gt; to create a B tag.

Thanks to Snowcat at FlashXML.net

April 6, 2009

StageListeners - Fluid Layout (fill class as basis to position items)

Filed under: ActionScript 3 Snippets — Tags: , , , , , — thornyeternity @ 3:03 pm

Class and how to instatiate - useful for liquid layouts:

package {

import flash.display.*

import flash.events.*;

public class StageListeners extends Sprite {

private var _mc:Sprite;

private var mcStage:Stage;

public function StageListeners(_mc:Sprite){

this._mc = _mc;

mcStage = _mc.stage;

addStageListener();

}//StageListeners

public function addStageListener() {

mcStage.scaleMode = StageScaleMode.NO_SCALE;

mcStage.align = StageAlign.TOP_LEFT;

mcStage.addEventListener(Event.RESIZE, resizeHandler);

}//addStageListener

private function resizeHandler(event:Event):void {

trace(”stageWidth: “+mcStage.stageWidth);

trace(”stageHeight: “+mcStage.stageHeight);

//aligned center

_mc.x = mcStage.stageWidth/2;

_mc.y = mcStage.stageHeight/2;

//or if aligned left

_mc.x = 0;

_mc.width = mcStage.stageWidth;

var scaleAmountX:Number = _mc.scaleX;

_mc.scaleY = scaleAmountX; //keep proportional

}//resizeHandler

}//class

}//package

//////////////////////instantiate with:

var bgStageListener:StageListeners = new StageListeners(resizeBg);

Older Posts »

Powered by WordPress