A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Calling a custom package

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    26

    Calling a custom package

    I am having problems calling a custom package. I have a package defined as follows:

    Code:
    package MMES {
    	/**
    	 * Global import commands
    	 */
    	import flash.display.*;
    	import flash.events.*;
    	import flash.net.*;
    	
    	/**
    	 * Common Class definition
    	 * This class contains methods used by some/all of the MMES Flash Programs
    	 * @extends MovieClip
    	 */
    	public class Common extends MovieClip {
    		/**
    		 * Constant defintions
    		 */
    		public const WEBSITE:int = 0;
    		public const SUBMIT:int = 1;
    		public const UPDATE:int = 2;
    		var configFile:Array;
    		var myLoader:URLLoader;
    		var filePath:String;
    		var myReq:URLRequest
    		var loaded:Boolean = true;
    
    		var myArrayOfLines:Array;
    		/**
    		 * Class Constructor
    		 * @param	void
    		 * @return	int
    		 */ 
    		public function Common():void {
    			filePath  =  "config/MMESCommon.cfg";
    			myReq  =  new  URLRequest(filePath);
    			myLoader  =  new  URLLoader();
    			myLoader.addEventListener(Event.COMPLETE,  fileLoaded);
    			myLoader.addEventListener(IOErrorEvent.IO_ERROR,  ioErrorHandler);
    			try {
    				myLoader.load(myReq);     
    			}      
    			catch (error:Error) {
    				loaded == false;
    			}
    			trace(loaded);
    		}
    		/** Method to return the load status
    		 * @param void
    		 * @return The Boolean load status
    		 */
    		public function loadStatus():Boolean {
    			return loaded;
    		}
    		/**
    		 * Method to calculate a numeral score
    		 * @param	correct:int
    		 * @param 	possible:int
    		 * @return	The numeric score earned
    		 */
    		public function numericScore(correct:int, possible:int):int {
    			return int((correct / possible) * 100);
    		}
    		/**
    		 * LetterScore function
    		 * @param	score:int		The numerial grade earned
    		 * @return  retval:String: 	The letter grade earned
    		 */
    		public function letterScore(score:int):String {
    			var retval = "";
    			if (score >= 90 && score <= 100) {
    				retval = "A";
    			} else if (score >= 80 && score <= 89) {
    				retval = "B";
    			} else if (score >= 70 && score <= 79) {
    				retval = "C";
    			} else if (score >= 60 && score <= 69) {
    				retval = "D";
    			} else if (score >= 50 && score <= 59) {
    				retval = "F";
    			} else {
    				retval = '--';
    			}
    			return retval;
    		}
    		/**
    		 * submitScore Function
    		 * @param	uname:String	The students name
    		 * @param	pScore:int		The students past score
    		 * @param	rid:int			The resource id
    		 * @param	lid:int			The lesson id
    		 * @param	cscore:int		The current score
    		 * @return	void
    		 */
    		public function submitScore(sid:int, rid:int, lid:int, cscore:int, pScore:int):void {
    			var postScore:URLRequest;
    			var postAddress:String
    			var postData:String = sid + "/" + rid + "/" + lid + "/" + cscore;
    			if (pScore == 0) {			
    				postAddress = configFile[WEBSITE] + configFile[SUBMIT] + postData;
    			} else {
    				postAddress = configFile[WEBSITE] + configFile[UPDATE] + postData;
    			}
    			trace("Post Address: " + postAddress);
    			//postScore = new URLRequest(postAddress);
    			//navigateToURL(postScore);
    		}
    		/**
    		 * Call back function for when file is loaded
    		 * @param	e:Event
    		 * @return	void
    		 */
    		public  function  fileLoaded(e:Event):void {
    			configFile = e.target.data.split(/\n/);
    			configFile[WEBSITE] = trim(configFile[WEBSITE]) + "/";
    			configFile[SUBMIT] = trim(configFile[SUBMIT]) + "/";
    			configFile[UPDATE] = trim(configFile[UPDATE]) + "/";
    		}
    		/**
    		 * Call back function for a file error
    		 * @param	e:IOErrorEvent
    		 * @return 	void
    		 */
    		public  function  ioErrorHandler(e:IOErrorEvent):void    {
    			trace("There  was  a  problem  loading  a  " + filePath + "  file:  " + e);
    			loaded = false;
    		}
    		/**
    		 * Function to trim the whitespace from a string
    		 * @param	s:String
    		 * @return  String representing s without whitespace at the right end
    		 */
    		function trim( s:String ):String
    		{
    			return s.replace( /^([\s|\t|\n]+)?(.*)([\s|\t|\n]+)?$/gm, "$2" );
    		}
    	}
    }
    The class file(Common.as) is save in the MMES directory which is a subdirectory of the root directory containing the main class file.

    I am tring to call this from a main program as follows:
    First I import the class file
    Code:
    import MMES.Common
    I then create an instance:
    Code:
    var MMES:Common = new Common();
    Lastly I try to call a function of the custom class:
    Code:
    letterGrade.text = MMES.letterScore(MMES.numericScore(correct,count));
    However, for every call to a method in the Common class I get:
    Code:
    1180: Call to possibly undefined method
    I have been researching this problem, but have been unable to get it working. Any help would be greatly appreciated.
    Thanks, Troy.

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    package {
    
            import flash.display.*;
            import flash.events.*;
            import MMES.Common;
    
            public class Main extends Sprite {
    
                    public function Main() {
                            var correct = 10;
                            var count = 10;
                            var mmes:Common = new Common();
                            letterGrade.text = mmes.letterScore(mmes.numericScore(correct,count));
                    }
            }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center