Skip to main content
Inspiring
May 31, 2011
Question

have a problem with interaction.

  • May 31, 2011
  • 2 replies
  • 1063 views

This is the class that works the dragonwork.fla which only uses this class and nothing else.
package 
{
//**************************** BRINGING IN THE NECESSARY HELP FILES **************************
import flash.display.*; // for placing items on the stage
import flash.text.*;
import flash.events.*;  // for watching out for events that happen during play
import flash.net.URLRequest; // for setting up an actual outside connection
import flash.events.IOErrorEvent; // for checking to see if errors occur
import rebarr.*;
public class DragonWork extends MovieClip
{
  private var thisdragon:Dragon = new Dragon("thedragon.xml","thisdragon");
  private var theage:Number;
  private var thetext:TextField = new TextField();
  private var textlook:TextFormat = new TextFormat();
  public function DragonWork()
  {
   addChild(thisdragon);thisdragon.x = 200; thisdragon.y = 250;
   textlook.font = "Helvetica"; textlook.size = 24; textlook.bold = true;
   addChild(thetext); thetext.x = 100; thetext.y = 100; thetext.width = 250; thetext.height = 30;
   theage = thisdragon.myAge; trace("age of the dragon is ", theage);
   thetext.text = thisdragon.growth; thetext.setTextFormat(textlook);
  
  }

}

}


This is inside the rebarr directory.

package  rebarr //**************** START OF CLASS DEFINITION *************************************
{
//**************************** BRINGING IN THE NECESSARY HELP FILES **************************
import flash.display.*; // for placing items on the stage
import flash.events.*;  // for watching out for events that happen during play
import flash.net.URLRequest; // for setting up an actual outside connection
import flash.events.IOErrorEvent; // for checking to see if errors occur
//**************************** SETTING UP THE PARTS OF THE CLASS *****************************
public class Dragon extends MovieClip
{
  private var thisDragon:XMLClass = new XMLClass("thedragon.xml", "this dragon");// holder for the main information about the dragon
  private var myName:String = new String;//holder for the name of the dragon
  private var myColor:String = new String;//holder for the color of the dragon
  private var myRider:String = new String;//holder for the name of the rider
  private var myGender:String = new String;//holder for the gender of the dragon
  public var myAge:Number = new Number;//holder for the age of the dragon
  private var myFile:String = new String;//holder for the file of the dragon picture
  private var fed:Boolean = false;//holder for whether dragon has been fed or not
  private var oil:Boolean = false;//holder for whether dragon has been oiled or not
  private var love:Boolean = false;//holder for whether dragon has been loved or not
  private var fly:Boolean = false;//holder for whether dragon can fly or not
  private var dragonPic:Loader = new Loader();//holder for picture of dragon
  public var growth:String;
  private var mySize:String;
  //************************ PUTTING THE CLASS TOGETHER  CONSTRUCTOR ***********************
  public function Dragon(filename:String, elementname:String)
  {
   //thisDragon = new XMLCLass(filename,elementname);
   thisDragon.addEventListener(XMLClass.ALL_DONE, init);
  }
  //************************************* FUNCTION TO PUT THE DRAGON TOGETHER ************************************************
  private function init(e:Event)
  {
   myName = thisDragon.xmlData.dragon.myname; trace("thisDragon myName is ", myName);//places the dragons name into its holder
   myGender = thisDragon.xmlData.dragon.mygender; trace("thisDragon myGender is ", myGender);//places the dragon gender into its holder
   myColor = thisDragon.xmlData.dragon.mycolor; trace("thisDragon myColor is ", myColor);//places the dragon color into its holder
   myRider = thisDragon.xmlData.dragon.myrider; trace("thisDragon myRider is ", myRider);//places the dragon owner name into its holder
   myAge = Number(thisDragon.xmlData.dragon.myage); trace("thisDragon myAge is ", myAge);//places the age of the dragon into its holder
   findgrowth(myAge);
   size(growth);
   dragonPic.load(new URLRequest(myFile)); trace(myFile); //places the picture of the dragon into its holder
   addChild(dragonPic); dragonPic.x = 0; dragonPic.y = 0;//places the dragon picture on the stage
   this.mouseChildren = false;// makes this class not have any mouse events
  }
  private function findgrowth(theAge:Number)
  {
    if(theAge >= 720)
    {
     growth = "Mature";
    }
    if(theAge > 365 && theAge < 720)
    {
     growth = "Year";
    }
    if(theAge > 270 && theAge < 365)
    {
     growth = "9";
    }
    if(theAge >180 && theAge < 270)
    {
     growth = "6";
    }
    if(theAge > 90 && theAge < 180)
    {
     growth = "3";
    }
    if(theAge < 90)
    {
     growth = "New";
    }
  }
  private function size(thesize:String)
  {
   trace("inside size function myAge is ", myAge);
   if(thesize == "Mature")//checking that age is equal to or greater than 400 days
   {
    myFile = "images/dragons/" + myColor + "Mature.gif"; //setting dragon picture for mature dragon
    trace("myFile is ", myFile); // for error checking
   }
   if(thesize == "Year")// checking that age is greater than a year but less than 400 days
   {
    myFile = "images/dragons/" + myColor + "Year.gif";// setting dragon picture for year dragon
    trace("myFile is ", myFile); // for error checking
   }
   if(thesize == "9")// checking that age is greater than 270 days but less than a year
   {
    myFile = "images/dragons/" + myColor + "9.gif";// setting dragon picture for nine months dragon
    trace("myFile is ", myFile); // for error checking
   }
   if(thesize == "6")// checking that dragon age is greater than 180 days and less than 270 days
   {
    myFile = "images/dragons/" + myColor + "6.gif";// setting dragon picture for 6 months dragon
    trace("myFile is ", myFile); // for error checking
   }
   if(thesize == "3")// checking that age is greater than 120 days and less than 180 days
   {
    myFile = "images/dragons/" + myColor + "3.gif";// setting dragon picture for 3 months dragon
    trace("myFile is ", myFile); // for error checking
   }
   if(thesize == "New")// checking that age is less than 120 days
   {
    myFile = "images/dragons/" + myColor + "New.gif";// setting dragon picture for new born dragon
    trace("myFile is ", myFile); // for error checking
   }
  }


}

}


This is the xml File that gets used

<?xml version="1.0" encoding="utf-8"?>
<thedragon>
<dragon>
  <myname>Rashana</myname>
  <myage>250</myage>
  <birthday>614578930224</birthday>
  <myrider>rbert</myrider>
  <myfile>images/dragons/green6.gif</myfile>
  <mygender>female</mygender>
  <mycolor>green</mycolor>
</dragon>
</thedragon>

What I am not understanding is why the growth variable will not print anything out.  It is probably something simple that I cannot see because of the forest but I do not know for sure.  Can someone give me an idea as to what is wrong here.

This topic has been closed for replies.

2 replies

Known Participant
May 31, 2011

well what i think is happening here is that for some reason the dragon class init function is not called before you set the text and thus the growth string is null./ what you can do is set the string to lets say growth="test" and see if it prints out. also i dont think it is a good idea to build such a dependancy on a public variaple you should maybe dispatch an event when the growth is changed and the handler function then can print the growth or what ever you see fit

rebarrAuthor
Inspiring
May 31, 2011

To be honest, I didn't want to make the variable public.  I tried setting up a method inside the class so that it could be gotten from the class by a call from another class but that did not seem to work either.  I am not sure what you mena by your suggestion, could you elaborate on it a bit. Please.

Known Participant
June 1, 2011

well you do like this:

inside the dragon class where you set the growth after that you write:

dispatchEvent(new MyEvent("growth",grotwh));

then you make a new class called MyEvent like this:

import flash.events.Event;

public class MyEvent extends Event{

public var VALUE::String;

public function MyEvent(name:String,value:int){

  super(s);

  VALUE = value;

}

}

and now in your main you just set an eventListener to the dragon class instance like this

var dragoninstance:Dragon = new Dragon();

dragoninstance.addEventListener("growth",handleGrowth);

function handleGrowth(e:MyEvent){

mytext.text = e.VALUE;

}

kglad
Community Expert
Community Expert
May 31, 2011

what exactly is the problem?