How to skew a Textfield in Actionscript 3.0? - actionscript-3

How can I skew a Textfield in order to set a ascending space for each line.
Following image should illustrate my intention:

Got it:
adobe
public class TextBlock_createTextLineExample extends Sprite {
public function TextBlock_createTextLineExample():void {
var str:String = "I am a TextElement, created from a String and assigned " +
"to the content property of a TextBlock. The createTextLine() method " +
"then created these lines, 300 pixels wide, for display." ;
var fontDescription:FontDescription = new FontDescription("Arial");
var format:ElementFormat = new ElementFormat(fontDescription);
format.fontSize = 16;
var textElement:TextElement = new TextElement(str, format);
var textBlock:TextBlock = new TextBlock();
textBlock.content = textElement;
createLines(textBlock);
}
private function createLines(textBlock:TextBlock):void
{
var lineWidth:Number = 300;
var xPos:Number = 15.0;
var yPos:Number = 20.0;
var textLine:TextLine = textBlock.createTextLine (null, lineWidth);
while (textLine)
{
//increase textLine every Line 10 px to get an offset
textLine.x += 10
textLine.y = yPos;
yPos += textLine.height + 2;
addChild (textLine);
textLine = textBlock.createTextLine (textLine, lineWidth);
}
}
}
}

private static const _DEG2RAD:Number = Math.PI/180;
public static function skew(target:DisplayObject, skewXDegree:Number, skewYDegree:Number):void
{
var m:Matrix = target.transform.matrix.clone();
m.b = Math.tan(skewYDegree*_DEG2RAD);
m.c = Math.tan(skewXDegree*_DEG2RAD);
target.transform.matrix = m;
}
public static function skewY(target:DisplayObject, skewDegree:Number):void
{
var m:Matrix = target.transform.matrix.clone();
m.b = Math.tan(skewDegree*_DEG2RAD);
target.transform.matrix = m;
}
public static function skewX(target:DisplayObject, skewDegree:Number):void
{
var m:Matrix = target.transform.matrix.clone();
m.c = Math.tan(skewDegree*_DEG2RAD);
target.transform.matrix = m;
}

Related

Remove Children Of a Different Class As3

I have added many objects to the stage (in the Achievements.as class) to create an achievements board. I want to remove all of the children from the main.as class when someone presses a back button is there a way of doing this. Also, is there a way of creating a custom textField class where each textField.selectable = false without having to assign it to each one individually.
public static var texts:Vector.<TextField> = new Vector.<TextField>();
public static var titleTxt:Vector.<TextField> = new Vector.<TextField>();
public static var descripTxt:Vector.<TextField> = new Vector.<TextField>();
public static var rewardTxt:Vector.<TextField> = new Vector.<TextField>();
public static var achCoins:Vector.<MovieClip> = new Vector.<MovieClip>();
for (var j:int = 0; j < 30; j++)
{
achCoins[j].x = 240;
achCoins[j].y = 45 + j * 70;
addChild(achCoins[j]);
descripTxt[j].x = 0;
descripTxt[j].y = 30 + 70 * j;
//descripTxt[j].antiAliasType = AntiAliasType.ADVANCED;
descripTxt[j].defaultTextFormat = tf2;
descripTxt[j].text = descriptext[j];
descripTxt[j].embedFonts = true;
addChild(descripTxt[j]);
titleTxt[j].x = 0;
titleTxt[j].y = j * 70;
//titleTxt[j].antiAliasType = AntiAliasType.ADVANCED;
titleTxt[j].defaultTextFormat = tf3;
titleTxt[j].text = titletext[j];
titleTxt[j].embedFonts = true;
addChild(titleTxt[j]);
rewardTxt[j].x = 200;
rewardTxt[j].y = 30 + j * 70;
rewardTxt[j].text = reward[j].toString();
rewardTxt[j].setTextFormat(tf1);
rewardTxt[j].embedFonts = true;
rewardTxt[j].textColor = 0x000000;
addChild(rewardTxt[j]);
texts[j].text = Main.achievement[j] + "/" + totnum[j];
texts[j].setTextFormat(tf1);
texts[j].x = 200;
texts[j].y = j * 70;
texts[j].embedFonts = true;
addChild(texts[j]);
}
I then remove the children using this code
for (var k:int = 0; k < 30; k++)
{
removeChild(achievementback.descripTxt[k]);
removeChild(achievementback.titleTxt[k]);
removeChild(achievementback.rewardTxt[k]);
removeChild(achievementback.texts[k]);
}
I get the error
1119: Access of possibly undefined property descripTxt through a
reference with static type Achievements.
for (var i:int = 0; i < 30; i++)
{
texts.push(new TextField());
titleTxt.push(new TextField());
rewardTxt.push(new TextField());
descripTxt.push(new TextField());
achCoins.push(new coinSmall());
{
In Achievements.as Class
public function RemoveTextboxes(){
for (var k:int = 0; k < 30; k++)
{
removeChild(descripTxt[k]);
removeChild(titleTxt[k]);
removeChild(rewardTxt[k]);
removeChild(texts[k]);
}
}
In Main.as Class
achievementback.RemoveTextBoxes()
Here is my whole achievements class
package
{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.display.Shape;
import flash.display.Graphics;
import flash.text.AntiAliasType;
public class Achievements extends MovieClip
{
public var backRect:Shape = new Shape();
public var coinsmall:MovieClip = new coinSmall();
public var titleText:MyTextField = new MyTextField();
public var descripText:MyTextField = new MyTextField();
public var reward_txt:MyTextField = new MyTextField();
public static var texts:Vector.<TextField> = new Vector.<TextField>();
public static var titleTxt:Vector.<TextField> = new Vector.<TextField>();
public static var descripTxt:Vector.<TextField> = new Vector.<TextField>();
public static var rewardTxt:Vector.<TextField> = new Vector.<TextField>();
public static var achCoins:Vector.<MovieClip> = new Vector.<MovieClip>();
public var tf1:TextFormat = new TextFormat();
public var tf2:TextFormat = new TextFormat();
public var tf3:TextFormat = new TextFormat();
//Achievement Variables
public var descriptext:Array = new Array();
public static var acharr:Array = new Array();
public var titletext:Array = new Array();
public var reward:Array = new Array(25,50,250,500,200,2000,10,100,500,10,25,50,10,25,100,25,25,50,50,100,500,500,5,5,5,50,10,50,100,250);
public function Achievements()
{
//Defining Objects
//Set Up Text Formats
//Text Format 1
tf1.font = "Myriad pro";
tf1.size = 20;
tf1.color = 0x660000;
tf1.align = "right";
//Text Format 2
tf2.size = 16;
tf2.color = 0xB8461D;
tf2.font = "Myriad Pro";
tf2.align = "center";
//Text Format 3
tf3.size = 23;
tf3.color = 0x660000;
tf3.font = "Myriad Pro";
tf3.align = "center";
//Text Box: Fraction Completed
//Text Box: Reward Amount For Each Achievement
//Text Box: Description of Achievement
for (var i:int = 0; i < 30; i++)
{
texts.push(new TextField());
titleTxt.push(new TextField());
rewardTxt.push(new TextField());
descripTxt.push(new TextField());
achCoins.push(new coinSmall());
backRect.graphics.beginFill(0x86B46D, 0.7);
backRect.graphics.drawRoundRect(0, i*70, 300, 60, 10, 10);
backRect.graphics.endFill();
addChild(backRect);
}
//change the text in a loop;
var acharr:Array = new Array();
var totnum:Array = new Array(50,100,500,1000,5,10,10,100,500,25,50,100,1,1,3,1,50,100,50,100,2,5,5,5,5,20,10,50,100,250);
for (var j:int = 0; j < 30; j++)
{
achCoins[j].x = 240;
achCoins[j].y = 45 + j * 70;
addChild(achCoins[j]);
descripTxt[j].width = 225;
descripTxt[j].height = 30;
descripTxt[j].x = 0;
descripTxt[j].y = 30 + 70 * j;
//descripTxt[j].antiAliasType = AntiAliasType.ADVANCED;
descripTxt[j].defaultTextFormat = tf2;
descripTxt[j].text = descriptext[j];
descripTxt[j].embedFonts = true;
addChild(descripTxt[j]);
titleTxt[j].width = 225;
titleTxt[j].height = 30;
titleTxt[j].x = 0;
titleTxt[j].y = j * 70;
//titleTxt[j].antiAliasType = AntiAliasType.ADVANCED;
titleTxt[j].defaultTextFormat = tf3;
titleTxt[j].text = titletext[j];
titleTxt[j].embedFonts = true;
addChild(titleTxt[j]);
rewardTxt[j].width = 100;
rewardTxt[j].height = 30;
rewardTxt[j].selectable = false;
rewardTxt[j].x = 200;
rewardTxt[j].y = 30 + j * 70;
rewardTxt[j].text = reward[j].toString();
rewardTxt[j].setTextFormat(tf1);
rewardTxt[j].embedFonts = true;
rewardTxt[j].textColor = 0x000000;
addChild(rewardTxt[j]);
texts[j].text = acharr[j] + "/" + totnum[j];
texts[j].setTextFormat(tf1);
texts[j].width = 100;
texts[j].height = 30;
texts[j].selectable = false;
texts[j].x = 200;
texts[j].y = j * 70;
texts[j].embedFonts = true;
addChild(texts[j]);
}
}
public function RemoveTextboxes(){
for (var k:int = 0; k < 30; k++)
{
removeChild(descripTxt[k]);
removeChild(titleTxt[k]);
removeChild(rewardTxt[k]);
removeChild(texts[k]);
}
}}
}
The arrays descriptext:Array acharr:Array titletext:Array All have the correct content in them but it is a lot of text so I took it out to post this
Here is the code I am using to remove the children from Main.as
function achievementExit():void
{
removeChild(back1);
removeChild(coin_sm);
removeChild(coinAmt);
removeChild(completed);
removeChild(achTit);
removeChild(achback);
achievementback.RemoveTextBoxes()
removeChild(achievementback)
removeChild(scrollPane);
}
I had to slightly modify the code and comment out where you set the text in the boxes to get this to run (since I'm missing some code) But What seemed to be my issue is that the textboxes were getting removed but your green shape was staying on the stage. Added a vector to hold all those and added those to the remove function.
package {
import flash.display.MovieClip;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.display.Shape;
import flash.display.Graphics;
import flash.text.AntiAliasType;
public class Achievements extends MovieClip
{
public var coinsmall:MovieClip = new MovieClip();
public var titleText:TextField = new TextField();
public var descripText:TextField = new TextField();
public var reward_txt:TextField = new TextField();
public static var texts:Vector.<TextField> = new Vector.<TextField>();
public static var titleTxt:Vector.<TextField> = new Vector.<TextField>();
public static var descripTxt:Vector.<TextField> = new Vector.<TextField>();
public static var rewardTxt:Vector.<TextField> = new Vector.<TextField>();
public static var achCoins:Vector.<MovieClip> = new Vector.<MovieClip>();
public static var graphix:Vector.<Shape> = new Vector.<Shape>();
public var tf1:TextFormat = new TextFormat();
public var tf2:TextFormat = new TextFormat();
public var tf3:TextFormat = new TextFormat();
//Achievement Variables
public var descriptext:Array = new Array();
public static var acharr:Array = new Array();
public var titletext:Array = new Array();
public var reward:Array = new Array(25,50,250,500,200,2000,10,100,500,10,25,50,10,25,100,25,25,50,50,100,500,500,5,5,5,50,10,50,100,250);
public function Achievements()
{
//Defining Objects
//Set Up Text Formats
//Text Format 1
tf1.font = "Myriad pro";
tf1.size = 20;
tf1.color = 0x660000;
tf1.align = "right";
//Text Format 2
tf2.size = 16;
tf2.color = 0xB8461D;
tf2.font = "Myriad Pro";
tf2.align = "center";
//Text Format 3
tf3.size = 23;
tf3.color = 0x660000;
tf3.font = "Myriad Pro";
tf3.align = "center";
for (var i:int = 0; i < 30; i++)
{
descriptext.push("THIS IS " + i);
texts.push(new TextField());
titleTxt.push(new TextField());
rewardTxt.push(new TextField());
descripTxt.push(new TextField());
achCoins.push(new MovieClip());
var backRect:Shape = new Shape();
backRect.graphics.beginFill(0x86B46D, 0.7);
backRect.graphics.drawRoundRect(0, i*70, 300, 60, 10, 10);
backRect.graphics.endFill();
graphix.push(backRect);
addChild(backRect);
}
//change the text in a loop;
var acharr:Array = new Array();
var totnum:Array = new Array(50,100,500,1000,5,10,10,100,500,25,50,100,1,1,3,1,50,100,50,100,2,5,5,5,5,20,10,50,100,250);
for (var j:int = 0; j < 30; j++)
{
achCoins[j].x = 240;
achCoins[j].y = 45 + j * 70;
addChild(achCoins[j]);
descripTxt[j].width = 225;
descripTxt[j].height = 30;
descripTxt[j].x = 0;
descripTxt[j].y = 30 + 70 * j;
//descripTxt[j].antiAliasType = AntiAliasType.ADVANCED;
descripTxt[j].defaultTextFormat = tf2;
//descripTxt[j].text = descriptext[j];
descripTxt[j].embedFonts = true;
addChild(descripTxt[j]);
titleTxt[j].width = 225;
titleTxt[j].height = 30;
titleTxt[j].x = 0;
titleTxt[j].y = j * 70;
//titleTxt[j].antiAliasType = AntiAliasType.ADVANCED;
titleTxt[j].defaultTextFormat = tf3;
//titleTxt[j].text = titletext[j];
titleTxt[j].embedFonts = true;
addChild(titleTxt[j]);
rewardTxt[j].width = 100;
rewardTxt[j].height = 30;
rewardTxt[j].selectable = false;
rewardTxt[j].x = 200;
rewardTxt[j].y = 30 + j * 70;
//rewardTxt[j].text = reward[j].toString();
rewardTxt[j].setTextFormat(tf1);
rewardTxt[j].embedFonts = true;
rewardTxt[j].textColor = 0x000000;
addChild(rewardTxt[j]);
//texts[j].text = acharr[j] + "/" + totnum[j];
texts[j].setTextFormat(tf1);
texts[j].width = 100;
texts[j].height = 30;
texts[j].selectable = false;
texts[j].x = 200;
texts[j].y = j * 70;
texts[j].embedFonts = true;
addChild(texts[j]);
}
}
public function RemoveTextboxes(){
trace("removing")
for (var k:int = 0; k < 30; k++)
{
removeChild(descripTxt[k]);
removeChild(titleTxt[k]);
removeChild(rewardTxt[k]);
removeChild(texts[k]);
removeChild(graphix[k]);
}
}
}
}
I also had to change your custom classes back to MovieClip and TextBox since I didnt have source for those.
It should be:
for (var k:int = 0; k < 30; k++)
{
achievementback.removeChild(achievementback.descripTxt[k]);
achievementback.removeChild(achievementback.titleTxt[k]);
achievementback.removeChild(achievementback.rewardTxt[k]);
achievementback.removeChild(achievementback.texts[k]);
}
Update
Are you even adding those DisplayObjects to your vector arrays?

How to get indexof a Bitmap image?

I am having a hard time to get an index of a bitmap image. I am not sure how should i suppose to do it.
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
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
{
BitmapArray.push(loadedArray);
var index:Number;
index = BitmapArray.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);
}

AS3 Flash CS6 access of undefined properties? Help will be much obliged

Properties such as score1; score2, lives1, lives2 and STAGE seem to be undefined properties. I dont see why? Please help...
e.g.
C:\Users\PC\Desktop\Whack My Mole - Android\Game.as, Line 429 1120: Access of undefined property score1.
C:\Users\PC\Desktop\Whack My Mole - Android\Game.as, Line 430 1120: Access of undefined property score1.
package {
import flash.display.Stage;
import flash.events.TouchEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.filters.DropShadowFilter;
import flash.text.AntiAliasType;
import flash.text.Font;
public class Game{
//Global Declarations
private var STAGE:Stage;
'Graphics'
private var Title:Title_mc;
private var Score_lbl:Label_Score_txt;
private var Lives_lbl:Label_Lives_txt;
private var holes:MoleHoles_mc;
private var Lives_txt:TextField;
private var Score_txt:TextField;
private var Shadow:DropShadowFilter;
private var Pause_btn:Button_Pause;
'Game Properties'
private var SLEEP:Timer;
private var countdown:Timer;
private var countdownComplete:Timer;
private var countdownDelay:Timer;
private var count:TextField;
private var count_inc:int;
private var Paused:Boolean;
private var moles:int;
public static var mole_spawn_delay:int = 1000;
public static var mole_death_delay:int = 1000;
public static var molePlacable:Boolean = true;
private var unavailableHole:int;
public static var molesName:String;
private var lvl:int = 51;
private var score1:Anim_Score1_mc;
private var score2:Anim_Score2_mc;
private var lives1:Anim_Lives1_mc;
private var lives2:Anim_Lives2_mc;
'Player Properties'
public static var Score:int = 0;
public static var Lives:int = 3;
public static var incrementer:int = 1;
public function Game(STAGE:Stage) {
this.STAGE = STAGE;
//Enable Touch Events
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
//Instantiate Objects
Shadow = new DropShadowFilter();
//Handle new frames
STAGE.addEventListener(Event.ENTER_FRAME, handle_ENTER_FRAME);
//Draw Graphics
'Menu Title'
Title = new Title_mc();
Title.x = 250.5;
Title.y = 62.35;
STAGE.addChild(Title);
'Content'
holes = new MoleHoles_mc();
holes.x = 0;
holes.y = 123.9;
STAGE.addChild(holes);
'Score Label'
Score_lbl = new Label_Score_txt();
Score_lbl.x = 19.65;
Score_lbl.y = 146.75;
STAGE.addChild(Score_lbl);
'Lives Label'
Lives_lbl = new Label_Lives_txt();
Lives_lbl.x = 358.25;
Lives_lbl.y = 141.05;
STAGE.addChild(Lives_lbl);
'Score Value'
Score_txt = new TextField();
Score_txt.x = 19.65;
Score_txt.y = 209.95;
Score_txt.width = 100;
Score_txt.defaultTextFormat = new TextFormat("Balloonist", 40, 0xFFFFFF);
Score_txt.selectable = false;
Score_txt.text = String(Score);
STAGE.addChild(Score_txt);
'Lives Value'
Lives_txt = new TextField();
Lives_txt.x = 410;
Lives_txt.y = 204.70;
Lives_txt.defaultTextFormat = new TextFormat("Balloonist", 40, 0xFFFFFF);
Lives_txt.selectable = false;
Lives_txt.text = String(Lives);
STAGE.addChild(Lives_txt);
'Pause Button'
Pause_btn = new Button_Pause();
Pause_btn.x = 22.40;
Pause_btn.y = 772.85;
STAGE.addChild(Pause_btn);
Pause_btn.buttonMode = true;
Pause_btn.addEventListener(TouchEvent.TOUCH_BEGIN, handle_Pause);
//Start Countdown
countDown();
}
//Pause/resume game
private function handle_Pause(e:TouchEvent):void
{
//...
}
//Sleep Method
private function sleep(seconds:int):void
{
SLEEP = new Timer(seconds, 1); // 1 second
SLEEP.addEventListener(TimerEvent.TIMER_COMPLETE, sleep_end);
SLEEP.start();
STAGE.frameRate = 0;
}
//Sleep Method Complete
private function sleep_end(e:TimerEvent):void
{
SLEEP.removeEventListener(TimerEvent.TIMER_COMPLETE , sleep_end);
STAGE.frameRate = 24;
}
//Count Down Timer
private function countDown():void
{
Paused = true;
count_inc = 5;
count = new TextField();
count.x = 213.9;
count.y = 158.05;
count.height = 150;
count.width = 150;
count.defaultTextFormat = new TextFormat("Balloonist", 150, 0xFFFFFF);
count.filters = [Shadow];
count.antiAliasType = AntiAliasType.ADVANCED;
count.sharpness = 400;
count.text = String(count_inc);
STAGE.addChild(count);
countdownComplete = new Timer(5000, 1);
countdownComplete.addEventListener(TimerEvent.TIMER, coutdown_Complete);
countdownDelay = new Timer(100);
countdownDelay.addEventListener(TimerEvent.TIMER, countDown_Tick);
countdown = new Timer(1000);
countdown.addEventListener(TimerEvent.TIMER, countDown_end);
countdownComplete.start();
countdownDelay.start();
countdown.start();
}
//Handle countdown tick
private function countDown_Tick(e:TimerEvent):void
{
if(count_inc <= 0)
{
countdown.stop();
countdown.removeEventListener(TimerEvent.TIMER, countDown_end);
}else {
countdownDelay.delay = 100;
}
}
//Handle countown complete
private function countDown_end(e:TimerEvent):void
{
if(count_inc <= 0)
{
countdownDelay.stop();
countdownDelay.removeEventListener(TimerEvent.TIMER, countDown_Tick);
}else{
count_inc -= 1;
count.text = String(count_inc);
}
}
//Countdown cleanup
private function coutdown_Complete(e:TimerEvent):void
{
STAGE.removeChild(count);
Paused = false;
}
//Main Game Loop
private function handle_ENTER_FRAME(e:Event):void
{
//Update game stuff
if(!Paused)
{
if(molePlacable)
{
sleep(mole_spawn_delay);
newMole();
}
Score_txt.text = String(Score);
Lives_txt.text = String(Lives);
}
//Clear stage & display game over interface
if(Lives <= 0)
{
STAGE.removeEventListener(Event.ENTER_FRAME, handle_ENTER_FRAME);
STAGE.removeChild(Title);
STAGE.removeChild(holes);
STAGE.removeChild(Score_lbl);
STAGE.removeChild(Lives_lbl);
STAGE.removeChild(Pause_btn);
STAGE.removeChild(Score_txt);
STAGE.removeChild(Lives_txt);
var gameOver:GameOver = new GameOver(STAGE);
}
//Update mole stats
if(moles > 50)
{
lvl = 71;
}
//Dissallow score to go below 0
if(Score < 0)
{
Score = 0;
}
}
//Create new Mole
private function newMole():void
{
'Specify mole hole'
var rnd = Math.ceil(Math.random()*11);
'Ensure mole does not spawn from preceding hole'
while(rnd == unavailableHole)
{
rnd = Math.ceil(Math.random()*11);
}
var X:int;
var Y:int;
switch(rnd)
{
case 0:
X = -14.75;
Y = 293.45;
break;
case 1:
X = 109.25;
Y = 291.35;
break;
case 2:
X = 223.75;
Y = 291.35;
break;
case 3:
X = 337.2;
Y = 291.35;
break;
case 4:
X = 0;
Y = 430;
break;
case 5:
X = 118.7;
Y = 430;
break;
case 6:
X = 226.9;
Y = 430;
break;
case 7:
X = 342.45;
Y = 430
break;
case 8:
X = 0;
Y = 561.35
break;
case 9:
X = 112.4;
Y = 561.35;
break;
case 10:
X = 229;
Y = 561.35;
break;
case 11:
X = 339.3;
Y = 561.35;
break;
}
'Specify molde to add'
rnd = lvl * Math.random();
if(rnd <=40)
{
//Default + 10*incrementer
var mole:Mole_Default_mc = new Mole_Default_mc(STAGE, X, Y);
}else if(rnd <=42){
//Crazy - 5*incrementer
var mole2:Mole_Crazy_mc = new Mole_Crazy_mc(STAGE, X, Y);
}else if(rnd <=43){
//Crazy2 - 10*inrementer
var mole3:Mole_Crazy2_mc = new Mole_Crazy2_mc(STAGE, X, Y);
}else if(rnd <45){
//Lady + 1 life
var mole4:Mole_Lady_mc= new Mole_Lady_mc(STAGE, X, Y);
}else if(rnd <46){
//Ninja - 2*inrementer
var mole5:Mole_Ninja_mc = new Mole_Ninja_mc(STAGE, X, Y);
}else if(rnd <47){
//Zombie + 5 * lives
var mole6:Mole_Zombie_mc = new Mole_Zombie_mc(STAGE, X, Y);
}else if(rnd <48){
//reaper - Lives
var mole7:Mole_Reaper_mc = new Mole_Reaper_mc(STAGE, X, Y);
}else if(rnd <49){
//Snob + 250
var mole8:Mole_Snob_mc = new Mole_Snob_mc(STAGE, X, Y);
}else if(rnd <52){
//Angel + 3 lives
var mole9:Mole_Angel_mc = new Mole_Angel_mc(STAGE, X, Y);
}else if(rnd <54){
//Demon - 3 lives
var mole10:Mole_Demon_mc = new Mole_Demon_mc(STAGE, X, Y);
}else if(rnd <55){
//Creature - 3+incrementer
var creature:Mole_Creature_mc = new Mole_Creature_mc(STAGE, X, Y);
}else if(rnd <56){
//Cyber + 50+incrementer
var cyber:Mole_Cyber_mc = new Mole_Cyber_mc(STAGE, X, Y);
}else if(rnd <57){
//Grumpy + 5
var grumpy:Mole_Grumpy_mc = new Mole_Grumpy_mc(STAGE, X, Y);
}else if(rnd <58){
//Hippie Lives+3 Score+100
var hippie:Mole_Hippie_mc = new Mole_Hippie_mc(STAGE, X, Y);
}else if(rnd<59){
//Hyper 30*incrementer
var hyper:Mole_Hyper_mc = new Mole_Hyper_mc(STAGE, X, Y);
}else if(rnd<60){
//Love timer-100
var love:Mole_Love_mc = new Mole_Love_mc(STAGE, X, Y);
}else if(rnd<61){
//LoveZombie - 20*Lives
var loveZombie:Mole_LoveZombie_mc = new Mole_LoveZombie_mc(STAGE, X, Y);
}else if(rnd<70){
//Sleepy Timer+100
var sleepy:Mole_Sleepy_mc = new Mole_Sleepy_mc(STAGE, X, Y);
}else if(rnd<71){
//Warrior + (10*incrementer)*2
var warrior:Mole_Warrior_mc = new Mole_Warrior_mc(STAGE, X, Y);
}
//Update mole stats
moles += 1;
if(mole_spawn_delay > 20 && mole_death_delay > 20)
{
mole_spawn_delay -= 10;
mole_death_delay -= 5;
}
//Update incrementer
if(moles > 100)
{
incrementer = 50;
}else if(moles > 80)
{
incrementer = 40;
}else if(moles > 60)
{
incrementer = 30;
}else if(moles > 20)
{
incrementer = 20;
}else if(moles > 10)
{
incrementer = 10;
}
}
//Animation
public static function animate(type:int)
{
if(type == 0)
{
score1 = new Anim_Score1_mc();
score1.x = 40;
score1.y = 250.6;
STAGE.addChild(score1);
var anim_timer:Timer = new Timer(2000, 1);
anim_timer.addEventListener(TimerEvent.TIMER, remove_score1);
anim_timer.start();
}else if(type == 1)
{
score2 = new Anim_Score2_mc();
score2.x = 32;
score2.y = 248.6;
STAGE.addChild(score2);
var anim_timer2:Timer = new Timer(2000, 1);
anim_timer2.addEventListener(TimerEvent.TIMER, remove_score2);
anim_timer2.start();
}else if(type == 2)
{
lives1 = new Anim_Lives1_mc();
lives1.x = 430.9;
lives1.y = 237.95;
STAGE.addChild(lives1);
var anim_timer3:Timer = new Timer(2000, 1);
anim_timer3.addEventListener(TimerEvent.TIMER, remove_lives1);
anim_timer3.start();
}else{
lives2 = new Anim_Lives2_mc();
lives2.x = 430.9;
lives2.y = 237.95;
STAGE.addChild(lives2);
var anim_timer4:Timer = new Timer(2000, 1);
anim_timer4.addEventListener(TimerEvent.TIMER, remove_lives1);
anim_timer4.start();
}
}
//Handle remove_score1
private function remove_score1(e:TimerEvent):void
{
STAGE.removeChild(score1);
}
//Handle remove_score2
private function remove_score2(e:TimerEvent):void
{
STAGE.removeChild(score2);
}
//Handle remove_lives1
private function remove_lives1(e:TimerEvent):void
{
STAGE.removeChild(lives1);
}
//Handle remove_lives2
private function remove_lives2(e:TimerEvent):void
{
STAGE.removeChild(lives2);
}
}
}
Replace:
public static function animate(type:int)
with
public function animate(type:int)
Or if you want to have that method static add:
private static var _instance:Game = null;
in Game Class definition and change animate function:
public static function animate(type:int):void{
if(_instance!=null) _instance.hidden_animate(type);
}
protected function hidden_animate(type:int){
// PLACE HERE CODE FROM YOUR ORIGINAL ANIMATE FUNCTION
}
in constructor function add:
_instance = this;
If you often use static methods (like in this code) read more bout design patterns like singleton :)

How to get dynamic TextInput field values in Actionscript 3

Hi creating dynamic TextInput fields in a function. Need to get the values of those fields in another function. Can anyone throw some light on this.
for(var i:int=0;i<answers.length;i++)
{
txtbox = new spark.components.TextInput();
var lblBox:spark.components.Label = new spark.components.Label();
lblBox.id = "lbl"+i.toString();
lblBox.text = String(answersLabel.getItemAt(i) );
lblBox.width = 10
lblBox.x = xPos-15;
lblBox.y = yPos;
QuestionAnswer.addElement(lblBox);
txtbox.id = "text"+i.toString();
txtbox.x = xPos;
txtbox.y = yPos;
QuestionAnswer.addElement(txtbox);
xPos += 200;
}
var txt:TextField;
var i:uint;
var ary:Array = new Array();
function txtCreation ():void {
for( i=0;i<5;i++)
{
txt = new TextField();
txt.text = "txt"+i;
addChild(txt);
txt.x = 50 + txt.width *i;
txt.y = 20;
ary.push(txt);
}
}
txtCreation();
for(i=0;i<ary.length;i++)
{
trace("array values : " +ary[i].text);
}
Just look at the text variable for the textfield.
var textFromField : String = myInputText.text;

doubt regarding carrying data in custom events using actionscript

I am working on actionscript to generate a SWF dynamically using JSON data coming from an HTTP request. I receive the data on creationComplete and try to generate a tree like structure. I don’t create the whole tree at the same time. I create 2 levels, level 1 and level 2. My goal is to attach custom events on the panels which represent tree nodes. When users click the panels, it dispatches custom events and try to generate the next level. So, it goes like this :
On creation complete -> get JSON-> create top tow levels -> click on level 2-> create the level 2 and level 3 -> click on level 3-> create level 3 and 4. …and so on and so on. I am attaching my code with this email. Please take a look at it and if you have any hints on how you would do this if you need to paint a tree having total level number = “n” where n = 0 to 100
Should I carry the data around in CustomPageClickEvent class.
[code]
import com.iwobanas.effects.*;
import flash.events.MouseEvent;
import flash.filters.BitmapFilterQuality;
import flash.filters.BitmapFilterType;
import flash.filters.GradientGlowFilter;
import mx.controls.Alert;
private var roundedMask:Sprite;
private var panel:NewPanel;
public var oldPanelIds:Array = new Array();
public var pages:Array = new Array();
public var delPages:Array = new Array();
public function DrawPlaybook(pos:Number,title:String,chld:Object):void {
panel = new NewPanel(chld);
panel.title = title;
panel.name=title;
panel.width = 100;
panel.height = 80;
panel.x=pos+5;
panel.y=40;
var gradientGlow:GradientGlowFilter = new GradientGlowFilter();
gradientGlow.distance = 0;
gradientGlow.angle = 45;
gradientGlow.colors = [0xFFFFF0, 0xFFFFFF];
gradientGlow.alphas = [0, 1];
gradientGlow.ratios = [0, 255];
gradientGlow.blurX = 10;
gradientGlow.blurY = 10;
gradientGlow.strength = 2;
gradientGlow.quality = BitmapFilterQuality.HIGH;
gradientGlow.type = BitmapFilterType.OUTER;
panel.filters =[gradientGlow];
this.rawChildren.addChild(panel);
pages.push(panel);
panel.addEventListener(MouseEvent.CLICK,
function(e:MouseEvent){onClickHandler(e,title,chld)});
this.addEventListener(CustomPageClickEvent.PANEL_CLICKED,
function(e:CustomPageClickEvent){onCustomPanelClicked(e,title)});
}
public function onClickHandler(e:MouseEvent,title:String,chld:Object):void {
for each(var stp1:NewPanel in pages){
if(stp1.title==title){
var eventObj:CustomPageClickEvent = new CustomPageClickEvent("panelClicked");
eventObj.panelClicked = stp1;
dispatchEvent(eventObj);
}
}
}
private function onCustomPanelClicked(e:CustomPageClickEvent,title:String):void {
Alert.show("onCustomPanelClicked" + title);
var panel:NewPanel;
for each(var stp:NewPanel in pages){
startAnimation(e,stp);
}
if(title == e.panelClicked.title){
panel = new NewPanel(null);
panel.title = title;
panel.name=title;
panel.width = 150;
panel.height = 80;
panel.x=100;
panel.y=40;
this.rawChildren.addChild(panel);
var slideRight:SlideRight = new SlideRight();
slideRight.target=panel;
slideRight.duration=750;
slideRight.showTarget=true;
slideRight.play();
var jsonData = this.map.getValue(title);
var posX:Number = 50;
var posY:Number = 175;
for each ( var pnl:NewPanel in pages){
pages.pop();
}
for each ( var stp1:Object in jsonData.children){
panel = new NewPanel(null);
panel.title = stp1.text;
panel.name=stp1.id;
panel.width = 100;
panel.id=stp1.id;
panel.height = 80;
panel.x = posX;
panel.y=posY;
posX+=150;
var s:String="hi" + stp1.text;
panel.addEventListener(MouseEvent.CLICK,
function(e:MouseEvent){onChildClick(e,s);});
this.addEventListener(CustomPageClickEvent.PANEL_CLICKED,
function(e:CustomPageClickEvent){onCustomPnlClicked(e)});
this.rawChildren.addChild(panel);
pages.push(panel);
this.addEventListener(CustomPageClickEvent.PANEL_CLICKED,
function(e:CustomPageClickEvent){onCustomPanelClicked(e,title)});
var slide:SlideUp = new SlideUp();
slide.target=panel;
slide.duration=1500;
slide.showTarget=false;
slide.play();
}
}
}
public function onChildClick(e:MouseEvent,s:String):void {
for each(var stp1:NewPanel in pages){
if(stp1.title==e.currentTarget.title){
var eventObj:CustomPageClickEvent = new CustomPageClickEvent("panelClicked");
eventObj.panelClicked = stp1;
dispatchEvent(eventObj);
}
}
}
private function onCustomPnlClicked(e:CustomPageClickEvent):void {
for each ( var pnl:NewPanel in pages){
pages.pop();
}
}
private function fadePanel(event:Event,panel:NewPanel):void{
panel.alpha -= .005;
if (panel.alpha <= 0){
panel.removeEventListener(Event.ENTER_FRAME,
function(e:Event){fadePanel(e,panel);});
};
panel.title="";
}
private function startAnimation(event:CustomPageClickEvent,panel:NewPanel):void{
panel.addEventListener(Event.ENTER_FRAME,
function(e:Event){fadePanel(e,panel)});
}
[/code]
Thanks in advance.
Palash
completely forgot i don't have enough rep to edit...
import com.iwobanas.effects.*;
import flash.events.MouseEvent;
import flash.filters.BitmapFilterQuality;
import flash.filters.BitmapFilterType;
import flash.filters.GradientGlowFilter;
import mx.controls.Alert;
private var roundedMask:Sprite;
private var panel:NewPanel;
public var oldPanelIds:Array = new Array();
public var pages:Array = new Array();
public var delPages:Array = new Array();
public function DrawPlaybook(pos:Number,title:String,chld:Object):void {
panel = new NewPanel(chld);
panel.title = title;
panel.name=title;
panel.width = 100;
panel.height = 80;
panel.x=pos+5;
panel.y=40;
var gradientGlow:GradientGlowFilter = new GradientGlowFilter();
gradientGlow.distance = 0;
gradientGlow.angle = 45;
gradientGlow.colors = [0xFFFFF0, 0xFFFFFF];
gradientGlow.alphas = [0, 1];
gradientGlow.ratios = [0, 255];
gradientGlow.blurX = 10;
gradientGlow.blurY = 10;
gradientGlow.strength = 2;
gradientGlow.quality = BitmapFilterQuality.HIGH;
gradientGlow.type = BitmapFilterType.OUTER;
panel.filters = [gradientGlow];
this.rawChildren.addChild(panel);
pages.push(panel);
panel.addEventListener(MouseEvent.CLICK, function(e:MouseEvent){onClickHandler(e,title,chld)});
this.addEventListener(CustomPageClickEvent.PANEL_CLICKED, function(e:CustomPageClickEvent){onCustomPanelClicked(e,title)});
}
public function onClickHandler(e:MouseEvent,title:String,chld:Object):void {
for each(var stp1:NewPanel in pages){
if(stp1.title==title){
var eventObj:CustomPageClickEvent = new CustomPageClickEvent("panelClicked");
eventObj.panelClicked = stp1;
dispatchEvent(eventObj);
}
}
}
private function onCustomPanelClicked(e:CustomPageClickEvent,title:String):void {
Alert.show("onCustomPanelClicked" + title);
var panel:NewPanel;
for each(var stp:NewPanel in pages){
startAnimation(e,stp);
}
if(title == e.panelClicked.title){
panel = new NewPanel(null);
panel.title = title;
panel.name=title;
panel.width = 150;
panel.height = 80;
panel.x=100;
panel.y=40;
this.rawChildren.addChild(panel);
var slideRight:SlideRight = new SlideRight();
slideRight.target=panel;
slideRight.duration=750;
slideRight.showTarget=true;
slideRight.play();
var jsonData = this.map.getValue(title);
var posX:Number = 50;
var posY:Number = 175;
for each ( var pnl:NewPanel in pages){
pages.pop();
}
for each ( var stp1:Object in jsonData.children){
panel = new NewPanel(null);
panel.title = stp1.text;
panel.name=stp1.id;
panel.width = 100;
panel.id=stp1.id;
panel.height = 80;
panel.x = posX;
panel.y=posY;
posX += 150;
var s:String="hi" + stp1.text;
panel.addEventListener(MouseEvent.CLICK, function(e:MouseEvent){onChildClick(e,s);});
this.addEventListener(CustomPageClickEvent.PANEL_CLICKED, function(e:CustomPageClickEvent){onCustomPnlClicked(e)});
this.rawChildren.addChild(panel);
pages.push(panel);
this.addEventListener(CustomPageClickEvent.PANEL_CLICKED, function(e:CustomPageClickEvent){onCustomPanelClicked(e,title)});
var slide:SlideUp = new SlideUp();
slide.target=panel;
slide.duration=1500;
slide.showTarget=false;
slide.play();
}
}
}
public function onChildClick(e:MouseEvent,s:String):void {
for each(var stp1:NewPanel in pages){
if(stp1.title==e.currentTarget.title){
var eventObj:CustomPageClickEvent = new CustomPageClickEvent("panelClicked");
eventObj.panelClicked = stp1;
dispatchEvent(eventObj);
}
}
}
private function onCustomPnlClicked(e:CustomPageClickEvent):void {
for each ( var pnl:NewPanel in pages){
pages.pop();
}
}
private function fadePanel(event:Event,panel:NewPanel):void{
panel.alpha -= .005;
if (panel.alpha <= 0){
panel.removeEventListener(Event.ENTER_FRAME,
function(e:Event){fadePanel(e,panel);});
};
panel.title="";
}
private function startAnimation(event:CustomPageClickEvent,panel:NewPanel):void{
panel.addEventListener(Event.ENTER_FRAME,
function(e:Event){fadePanel(e,panel)});
}