# Input textbox data to display in a dynamic textbox

**URL:** <https://forum.kirupa.com/t/input-textbox-data-to-display-in-a-dynamic-textbox/261607>\
**Category:** flash\
**Created:** [May 29, 2008, 8:03am UTC](https://forum.kirupa.com/t/input-textbox-data-to-display-in-a-dynamic-textbox/261607 "2008-05-29T08:03:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![punkyfee](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@punkyfee](https://forum.kirupa.com/u/punkyfee)\
**Post date:** [May 29, 2008, 8:03am UTC](https://forum.kirupa.com/t/input-textbox-data-to-display-in-a-dynamic-textbox/261607/1 "2008-05-29T08:03:54Z")

</div>

Hi, I am trying to create a virtual pet. I want the user to be able to enter the name they would like to give to their pet, then click a button which will store the name and move to the next frame and for this data to be displayed in various stages throughout the game.

I am completely new to actionscript 3 and struggling a bit :).  
My input textbox has the instance name myTextBox  
Dynamic textbox has the instance name petName

In the first time line I have other variables/constants declared so I thought I could declare var yourname:String; which brought the error Error #2007: Parameter text must be non-null.

and I also tried var yourname:TextField which brought the error 1067: Implicit coercion of a value of type flash.text:TextField to an unrelated type String.

My coding on the button stage is

myTextBox.text = yourname;

enterButton.addEventListener(MouseEvent.CLICK, enterSlide);  
//skip slide function  
function enterSlide (evt) {  
gotoAndPlay(“instructions”);

```
}

```

On the other frame where the dynamic text box is i have  
addEventListener(Event.ENTER\_FRAME, petNameDisplay);

function petNameDisplay (event:Event){  
petName.text = String (+yourname);  
}

Also declared somewhere on the dynamic text stage i have petName.text = yourname

I would really appreciate any help and any 101 on declaring variables for text boxes.  
Thanks for your time

Fiona

Below is all my code

Frame One  
const LIFEEXP:uint = 10;  
const EXPLODE:uint = 10;  
const HEARTATTACK:uint = 10;  
const BARHEIGHT:uint = 185;  
const TIMEMSGDISPLAYED:uint = 4

var age:Number; // pet’s age  
var lifeExpectancy:Number; // pet’s life expectancy  
var timeSinceAged:uint; // time since pet aged  
var fullness:Number; // current fullness  
var timeSinceFed:uint; // time since it was fed  
var mood:Number; // current mood  
var timeSincePlay:uint; // time since played with  
var causeOfDeath:String;  
import flash.utils.Timer;  
var gameTimer:Timer = new Timer(1000,0);  
var fullnessMsg:String;  
var moodMsg:String;  
var timeDisplayed:Number;  
var yourname:String;

skipbutton.addEventListener(MouseEvent.CLICK, skipSlide);  
//skip slide function  
function skipSlide (evt) {  
gotoAndPlay(20);  
}  
Frame Two (where the input box is )

myTextBox.text = yourname;

enterButton.addEventListener(MouseEvent.CLICK, enterSlide);  
//skip slide function  
function enterSlide (evt) {  
gotoAndPlay(“instructions”);

```
}

```

Frame Three (where the dynamic text box is)  
// pause the playback head  
stop();

initialise();

// event listeners for help  
helpBtn.addEventListener(MouseEvent.CLICK, showHideHelp);  
help.addEventListener(MouseEvent.CLICK, showHideHelp);

// other event listeners  
feedButton.addEventListener(MouseEvent.CLICK, feedPet);  
play2Button.addEventListener(MouseEvent.CLICK, playPet);  
gameTimer.addEventListener(TimerEvent.TIMER, updateGame);

//background buttons  
but1.addEventListener(MouseEvent.CLICK, changeBG);  
but2.addEventListener(MouseEvent.CLICK, changeBG);  
but3.addEventListener(MouseEvent.CLICK, changeBG);  
addEventListener(Event.ENTER\_FRAME, petNameDisplay);

function petNameDisplay (event:Event){  
petName.text = yourname  
}

function changeBG ( evt ) {

```
var myButton:String = evt.target.name;

switch (myButton) {
    case "but1" : bg.gotoAndStop(2);
        break;
    case "but2" : bg.gotoAndStop(3);
        break;
    case "but3" : bg.gotoAndStop(4);
        break;
    } // end switch

```

} // end function

function showHideHelp (evt)  
{  
help.visible = !(help.visible);

```
if (gameTimer.running) 
    {
    gameTimer.stop();
    } 
else 
    {
    gameTimer.start();
    }

```

}

function feedPet ( evt )  
{  
fullness += 0.3;  
if (fullness \>= EXPLODE) {  
gameTimer.stop();  
causeOfDeath =“overeating”;  
// go to death screen  
gotoAndPlay(“death”);  
}  
else  
{  
updateFullness(fullness);  
timeSinceFed = 0;  
}  
}

function playPet ( evt )  
{

```
mood += 0.4;
if (mood &gt;= HEARTATTACK) {
gameTimer.stop();
causeOfDeath ="Heart Attack";
// go to death screen
gotoAndPlay("death");        
} 
else 
{
    updateMood(mood);
    timeSincePlay = 0;

}

```

}

var speed:uint = 5;  
square1.addEventListener(MouseEvent.CLICK, clickHandler);

function clickHandler (evt)  
{  
if (square1.x \< (stage.stageWidth - speed))  
{  
// move the square  
square1.x += speed;  
}  
}

function updateAge (myAge:Number)  
{  
// show age with no decimal places, and as a string  
ageDisplay.text = int(myAge).toString();  
}

function updateMessage () {  
var myMsg:String = fullnessMsg + " " + moodMsg;  
if (!(messageTxt.visible)) {  
var myRandStatus:Number = Math.random();  
if (myRandStatus \>= 0.5) {  
messageTxt.text = myMsg;  
messageTxt.visible = true;  
}  
} else {  
timeDisplayed ++;  
if (timeDisplayed \>= TIMEMSGDISPLAYED) {  
timeDisplayed = 0;  
messageTxt.visible = false;  
}  
}  
}

function updateFullness (myFullness:Number)

{  
// find appropriate fullnessMsg  
switch (int(myFullness)) {  
case 0: fullnessMsg = “I’m starving! Feed me now!!”;  
break;  
case 1:  
case 2: fullnessMsg = “ohh so hungry…!”;  
break;  
case 3: fullnessMsg = “I’m a bit peckish!.”;  
break;  
case 4:  
case 5:  
case 6: fullnessMsg = “Mmm I’m quite content food wise.”;  
break;  
case 7: fullnessMsg = “Woo just a little full!”;  
break;  
case 8: fullnessMsg = “No I am positively stuffed!.”;  
break;  
case 9:  
case 10: fullnessMsg = “Blah, I am so full I might explode :(!”;  
break;  
}

// update the feedback fullness display  
var myBarHeight:Number;  
myBarHeight = (BARHEIGHT / EXPLODE) \* myFullness;  
fullnessDisplay.height = myBarHeight;  
}

function updateMood (myMood:Number)  
{  
// find appropriate fullnessMsg  
switch (int(myMood))  
{  
case 0: moodMsg = “I’m also so so so lonely”;  
break;  
case 1:  
case 2: moodMsg = “And I feel very alone”;  
break;  
case 3: moodMsg = “And I’m a little bit sad.”;  
break;  
case 4:  
case 5:  
case 6: moodMsg = “I’m a happy little alien :)”;  
break;  
case 7: moodMsg = "You are wearing me out ";  
break;  
case 8: moodMsg = “Please let me rest”;  
break;  
case 9:  
case 10: moodMsg = “Also Leave me alone, I need rest!”;  
break;  
}  
// update the feedback fullness display  
var myBarHeight:Number;  
myBarHeight = (BARHEIGHT / HEARTATTACK) \* myMood;  
moodDisplay.height = myBarHeight;  
}

function updateGame (evt) {  
// add 1 to timeSinceAged, timeSinceFed and timeSincePlay  
timeSinceAged++;  
timeSinceFed++;  
timeSincePlay++;  
// check if time to age  
if (timeSinceAged \>= 10) {  
// add 0.3 to age  
age += 0.3;  
// reset timeSinceAged  
timeSinceAged = 0;  
// check if died  
if (age \> lifeExpectancy) {  
// it’s died!  
gameTimer.stop();  
causeOfDeath = “Old Age”;  
gotoAndPlay(“death”);  
}  
else  
{  
//not dead, update display  
updateAge(age);  
}  
}  
if (timeSinceFed \>= 5)  
{  
fullness -= 0.5;  
timeSinceFed = 0;  
// check if starved  
if (fullness \<= 0)  
{  
// died  
gameTimer.stop();  
causeOfDeath = “starvation”;  
gotoAndPlay(“death”);  
}  
else  
{  
// not dead, adjust lifeExpectancy and update display  
if (fullness \<= 2.0) {  
lifeExpectancy -= 0.05;  
} else if (fullness \<= 3.0) {  
lifeExpectancy -= 0.02;  
} else if ((fullness \> 4.0) && (fullness \<= 4.5)) {  
lifeExpectancy += 0.02;  
} else if (fullness \<= 5.4) {  
lifeExpectancy += 0.05;  
} else if (fullness \<= 5.9) {  
lifeExpectancy += 0.02;  
} else if ((fullness \> 6.9) && (fullness \<= 7.9)) {  
lifeExpectancy -= 0.02;  
} else if (fullness \> 7.9) {  
lifeExpectancy -= 0.05;  
}

```
    }
if (timeSincePlay &gt;= 5) 
    {
    mood -= 0.5;
    timeSincePlay = 0;
    // check if lonely
    if (mood &lt;= 0) 
    {
        // died
        gameTimer.stop();
        causeOfDeath = "Loneliness";
        gotoAndPlay("death");
    }
    else 
    {
        // not dead, adjust lifeExpectancy and update display
        if (mood &lt;= 2.0) {
            lifeExpectancy -= 0.05;
        } else if (mood &lt;= 3.0) {
            lifeExpectancy -= 0.02;
        } else if ((mood &gt; 4.0) && (mood &lt;= 4.5)) {
            lifeExpectancy += 0.02;
        } else if (mood &lt;= 5.4) {
            lifeExpectancy += 0.05;
        } else if (mood &lt;= 5.9) {
            lifeExpectancy += 0.02;
        } else if ((mood &gt; 6.9) && (mood &lt;= 7.9)) {
            lifeExpectancy -= 0.02;
        } else if (mood &gt; 7.9) {
            lifeExpectancy -= 0.05;
    }
    }
        
        // check we haven’t reached the life expectancy limit
        if (lifeExpectancy &gt; LIFEEXP) 
        {
            lifeExpectancy = LIFEEXP;
        }

        // check we haven’t reached the life expectancy limit
        if (lifeExpectancy &gt; LIFEEXP) 
        {
            lifeExpectancy = LIFEEXP;            
        }
        
        updateFullness(fullness);
        updateMood(mood);
    } // end check if lonely
} // end check if need to update mood

updateMessage();

```

}

function initialise ( )  
{  
fullnessMsg = “”;  
moodMsg = “”;

```
// set help sprite to invisible
help.visible = false;
// set the help display movieclip to invisible
age = 0.0;
lifeExpectancy = 7.0;
timeSinceAged = 0;
fullness = 5.0;
timeSinceFed = 0;
mood = 6.5;
timeSincePlay = 0;
timeDisplayed = 0;
// display the initial age, fullness and mood on 
// the feedback elements

// display the initial age, fullness and mood on displays
updateAge(age);
// show age with no decimal places, and as a string
ageDisplay.text = int(age).toString();
petName.text = yourname;

// some code here
updateAge(age);
updateFullness(fullness);
updateMood(mood);
    // display the initial age, fullness and mood on displays
updateAge(age);
updateFullness(fullness);
updateMood(mood);
gameTimer.start();

```

}

---

<div class="post-metadata">

**Author:** ![djheru](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/djheru/32/3190_2.png) [@djheru](https://forum.kirupa.com/u/djheru)\
**Post date:** [May 29, 2008, 2:11pm UTC](https://forum.kirupa.com/t/input-textbox-data-to-display-in-a-dynamic-textbox/261607/2 "2008-05-29T14:11:14Z")

</div>

I just picked up the oreilly book on AS3, and a virtual pet application is the example they use throughout.

---

<div class="post-metadata">

**Author:** ![punkyfee](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@punkyfee](https://forum.kirupa.com/u/punkyfee)\
**Post date:** [May 30, 2008, 6:58am UTC](https://forum.kirupa.com/t/input-textbox-data-to-display-in-a-dynamic-textbox/261607/3 "2008-05-30T06:58:56Z")

</div>

> [@djheru;2332998](#):
>
> I just picked up the oreilly book on AS3, and a virtual pet application is the example they use throughout.

Thanks 🙂 Yes I saw it on google and had a look through it. I didn’t understand the naming bit… all i need to know is just how to get text that typed into an input text box and display this text in a dynamic text box (which is on a different frame). I am not too sure of what variables to declare or how to go about it.

---

<div class="post-metadata">

**Author:** ![DiamondDog](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@DiamondDog](https://forum.kirupa.com/u/DiamondDog)\
**Post date:** [May 30, 2008, 7:51am UTC](https://forum.kirupa.com/t/input-textbox-data-to-display-in-a-dynamic-textbox/261607/4 "2008-05-30T07:51:26Z")

</div>

I’m a noob. This seemed like a useful thing for me to play around with and see if I could get it working.

I don’t know if this is the best way of doing this but it seems to work.

I have two frames.  
On the first frame I have an input text field called myTextBox, and a button called myPlayButton.

I also have this code:

```auto
stop();

var enteredText:String = "";

myPlayButton.addEventListener(MouseEvent.MOUSE_DOWN,moveon);

function moveon(e:MouseEvent)
{
    enteredText = myTextBox.text;    
    gotoAndStop(2);
}

```

On the second frame I have a dynamic text field called petName and this code:

```auto
stop();
petName.text = enteredText;

```

Originally I tried doing it without the ‘enteredText’ variable. On Frame 2 I tried

```auto
petName.text = myTextBox.text

```

but that didn’t work for some reason.

Anyway, hope this works for you.

---

<div class="post-metadata">

**Author:** ![punkyfee](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@punkyfee](https://forum.kirupa.com/u/punkyfee)\
**Post date:** [May 30, 2008, 8:22am UTC](https://forum.kirupa.com/t/input-textbox-data-to-display-in-a-dynamic-textbox/261607/5 "2008-05-30T08:22:02Z")

</div>

Thanks for getting me a step further Diamond! I tried your code and no errors this time (Yay!!) unfortunately the text I enter still isn’t being displayed.

---

<div class="post-metadata">

**Author:** ![punkyfee](https://avatars.discourse-cdn.com/v4/letter/p/779978/32.png) [@punkyfee](https://forum.kirupa.com/u/punkyfee)\
**Post date:** [May 30, 2008, 8:31am UTC](https://forum.kirupa.com/t/input-textbox-data-to-display-in-a-dynamic-textbox/261607/6 "2008-05-30T08:31:09Z")

</div>

Wow Diamond you are a star!! It all works now thanks to your code. Very much appreciated!  
Thank sooo much!!!
