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