A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Trouble converting AS2 to AS3

  1. #1
    Junior Member
    Join Date
    Jun 2007
    Posts
    28

    Trouble converting AS2 to AS3

    I have a very simple preloader im trying to use and i need to make it AS3 because it involves the FLVPlayback component.

    Here is the AS2 code:

    Code:
    stop();
    loadingBar._xscale = 1;
    var loadingCall:Number = setInterval(preloadSite, 50);
    function preloadSite():Void {
    var siteLoaded:Number = _root.getBytesLoaded();
    var siteTotal:Number = _root.getBytesTotal();
    var percentage:Number = Math.round(siteLoaded/siteTotal*100);
    loadingBar._xscale = percentage;
    percentClip.percentDisplay.text = percentage + "%";
    percentClip._x = loadingBar._x + loadingBar._width;
    bytesDisplay.text = "loaded " + siteLoaded + " of " + siteTotal + " bytes";
    if (siteLoaded >= siteTotal) {
    clearInterval(loadingCall);
    gotoAndStop(5);
    }
    }
    I found a converter online and this is how the code turned out:

    Code:
    package  { 
    	stop();
    	import flash.utils.clearInterval;
    	import flash.utils.setInterval;
    	loadingBar.scaleX = 1;
    	var loadingCall:Number = setInterval(preloadSite, 50);
    	function preloadSite():void {
    	public var siteLoaded:Number = _root.getBytesLoaded();
    	public var siteTotal:Number = _root.getBytesTotal();
    	public var percentage:Number = Math.round(siteLoaded/siteTotal*100);
    	loadingBar.scaleX = percentage;
    	percentClip.percentDisplay.text = percentage + "%";
    	percentClip.x = loadingBar.x + loadingBar.width;
    	bytesDisplay.text = "loaded " + siteLoaded + " of " + siteTotal + " bytes";
    	if (siteLoaded >= siteTotal) {
    	clearInterval(loadingCall);
    	gotoAndStop(5);
    	}
    	}
    	
    }
    but I get the following errors:

    Code:
    1037: Packages cannot be nested
    1114: The public attribute can only be used inside a package
    Can anyone help?

  2. #2
    Junior Member
    Join Date
    Jun 2007
    Posts
    18
    Code:
    package  { 
    	stop();
    	import flash.utils.clearInterval;
    	import flash.utils.setInterval;
    	loadingBar.scaleX = 1;
    	var loadingCall:Number = setInterval(preloadSite, 50);
    	function preloadSite():void {
    	public var siteLoaded:Number = _root.getBytesLoaded();
    	public var siteTotal:Number = _root.getBytesTotal();
    	public var percentage:Number = Math.round(siteLoaded/siteTotal*100);
    	loadingBar.scaleX = percentage;
    	percentClip.percentDisplay.text = percentage + "%";
    	percentClip.x = loadingBar.x + loadingBar.width;
    	bytesDisplay.text = "loaded " + siteLoaded + " of " + siteTotal + " bytes";
    	if (siteLoaded >= siteTotal) {
    	clearInterval(loadingCall);
    	gotoAndStop(5);
    	}
    	}
    	
    }
    There a a few things inherently wrong with this conversion, so lets look at the error messages:

    Code:
    1037: Packages cannot be nested
    This error suggests that the code is either in the actions layer in a movie timeline, which makes it already part of the package the doc class is part of, or the code is internal to another package.
    Typically the package is used like a container for a class or a group of classes, like this:
    Code:
    package 
    {
    	import flash.display.Sprite;
    	import flash.text.TextField;
    	
    	/*
    	 * This is a very simple class that only does one thing:
    	 * It creates a textfield and adds it to the display list.
    	 * The purpose of this class is to show the required imports (above)
    	 * and procedure needed (below) to create a field using OOD in AS3.
    	 * It also provides a base class for other classes.
    	 */
    	
    	public class TextExample extends Sprite
    	{
    		public function TextExample ()
    		{
    			var field:TextField=new TextField  ;
    			addChild (field);
    		}
    	}
    }
    It is very likely that the converter that was used was designed around converting AS2 classes to AS3.

    the second error:
    Code:
    1114: The public attribute can only be used inside a package
    really just reiterates the first. The package key word was ignored by the compiler, and then the use of 'public' for a variable, which is used for determining the accessibility of a variable outside of a particular class, threw the error.

    If you are using this code inside a fla on the actions layer of the timeline, then remove the package and its braces, then remove the word public from the variables used.

    I would suggest using a doc class similar to the following:
    Code:
    package 
    {
    	import flash.display.*;
    	import flash.text.*;
    	import flash.net.URLRequest;
    	import flash.events.*;
    
    	/*
    	 * This class creates and utilizes listeners for multiple
    	 * download progress related events when loading an image
    	 */
    
    	public class LoaderExample extends Sprite
    	{
    		public function LoaderExample ()
    		{
    			//Create the loader and add it to the display list
    			var loader:Loader=new Loader  ;
    			addChild (loader);
    
    			//Add the event handlers to check for progress
    			loader.contentLoaderInfo.addEventListener (Event.OPEN,handleOpen);
    			loader.contentLoaderInfo.addEventListener (ProgressEvent.PROGRESS,handleProgress);
    			loader.contentLoaderInfo.addEventListener (Event.COMPLETE,handleComplete);
    
    			//Load the external image
    			loader.load (new URLRequest("image.jpg"));
    
    		}
    		//handler for the open
    		private function handleOpen (event:Event):void
    		{
    			trace ("open");
    		}
    		//handler for the progress
    		private function handleProgress (event:ProgressEvent):void
    		{
    			var percent:Number=event.bytesLoaded / event.bytesTotal * 100;
    			trace ("progress, percent = " + percent);
    		}
    		//handler for the completion
    		private function handleComplete (event:Event):void
    		{
    			trace ("complete");
    		}
    	}
    }
    Granted this example only loads an image, but it may help you out. If you are using Flash CS3 you would save it as LoaderExample.as in the same folder as the .fla and then in the property inspector of the fla with nothing selected enter LoaderExample into the document class field.

    I hope I did not give you TMFI on this. If you want more information on custom classes in AS3 check out Actionscript 3.0 Cookbook Ch 2
    Good Luck!

  3. #3

    "packages cannot be nested"

    I entered the example you gave an put it on frame 1 of my flash file and got, "packages cannot be nested"". what am I missing?

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