Solved

Convert AS1 to AS3

Posted on 2011-09-26
12
852 Views
Last Modified: 2013-11-11
I'm having difficulty converting AS1 to AS3 (CS3).

Please provide code examples to convert to AS3?

This code is taken from a book example,FLASH MX COMPONENTS - MOST WANTED, that was written in AS1.

// start off invisible
    this._alpha=0;
    // set initial position
    this.y = Object.environment.y;
    this.yoffset = random(500);
    this.angle = Object.environment.angle;
    
    // get movin'
    this.onEnterFrame = function() {
    	// calculate radian value
    	var rad = this.angle*Math.PI/180;
    	// calculate radial position
    	this.xpos = Math.sin(rad)*x+Math.cos(rad)*this.z;
    	this.zpos = Math.sin(rad)*z-Math.cos(rad)*this.x;
    	// calculate size
    	var scale = Object.environment.fl/(Object.environment.fl+this.zpos);
    	// set scren position 
    	this._x = this.xpos*scale;
    	this._y = this.ypos*scale;
    	// set screen size
    	this._xscale = scale*100;
    	this._yscale = scale*100;
    	// one chance in 1000
    	if (!random(1000)) {
    		// time to float away
    		this.timeToExit=true;
    	}
    	if (this.timeToExit) {
    		// slowly levitate towards destruction
    		this.angle += (Object.environment.angle-this.angle)/25;
    		// acceleration
    		this.vy -= .2;
    		// adjust position by velocity
    		this.ypos += this.vy;
    		// fade out
    		this._alpha--;
    		// if all the way faded, decrement count and self-destruct
    		if (this._alpha<0) {
    			//Object.environment.dotCount--;
    			//this.removeMovieClip();
    			this._alpha+=4;
    			this.timeToExit=false;
    		}
    	} else {
    		// slowly move towards global perspective
    		this.angle += (Object.environment.angle-this.angle)/25;
    		this.vy = (Object.environment.y-this.ypos+this.yoffset)/20;
    		this.ypos += this.vy;
    		// slowly fade in
    		if (this._alpha<100) {
    			this._alpha+=4;
    		}
    	}
    };

Open in new window


// create a single instance of the dot movieclip
function makedot() {
	if (Object.environment.dotCount<Object.environment.maxDots) {
		nombre="dot"+String(depth++);
		neo = field1.attachMovie("dot", nombre, depth);
		neo.x=.5 * (random(45*2)-45);
		neo.z=.5 * (random(45*2)-45);
		nombre="dot"+String(depth++);
		neo = field2.attachMovie("dot", nombre, depth);
		neo.x=.5 * (random(65*2)-65);
		neo.z=.5 * (random(65*2)-65);
		nombre="dot"+String(depth++);
		neo = field3.attachMovie("dot", nombre, depth);
		neo.x=.5 * (random(100*2)-100);
		neo.z=.5 * (random(100*2)-100);
		nombre="dot"+String(depth++);
		//neo = field2.attachMovie("dot", nombre, depth);
		//neo.x=.85 * (random(200*2)-200);
		//neo.z=.85 * (random(200*2)-200);
		//neo.x=.75 * (random(90*2)-90);
		//neo.z=.75 * (random(90*2)-90);
		//neo.x=.75 * (random(125*2)-125);
		//neo.z=.75 * (random(125*2)-125);
		
		//neo.x=.75 * (random(125*2)-125);
		//neo.z=.75 * (random(125*2)-125);
		//neo.x=.75 * (random(65*2)-65);
		//neo.z=.75 * (random(65*2)-65);
	};
}
function makedot(fld) {
	if (Object.environment.dotCount<Object.environment.maxDots) {
		nombre="dot"+String(depth++);
		neo = fld.attachMovie("dot", nombre, depth);
		neo.x=.5 * (random(45*2)-45);
		neo.z=.5 * (random(45*2)-45);
		nombre="dot"+String(depth++);
		neo = fld.attachMovie("dot", nombre, depth);
		neo.x=.5 * (random(65*2)-65);
		neo.z=.5 * (random(65*2)-65);
		nombre="dot"+String(depth++);
		neo = fld.attachMovie("dot", nombre, depth);
		neo.x=.5 * (random(100*2)-100);
		neo.z=.5 * (random(100*2)-100);
		nombre="dot"+String(depth++);
		neo = fld.attachMovie("dot", nombre, depth);
		neo.x=.85 * (random(200*2)-200);
		neo.z=.85 * (random(200*2)-200);
		//neo.x=.75 * (random(90*2)-90);
		//neo.z=.75 * (random(90*2)-90);
		//neo.x=.75 * (random(125*2)-125);
		//neo.z=.75 * (random(125*2)-125);
		
		//neo.x=.75 * (random(125*2)-125);
		//neo.z=.75 * (random(125*2)-125);
		//neo.x=.75 * (random(65*2)-65);
		//neo.z=.75 * (random(65*2)-65);
	};
}

Open in new window


// register root as environment
Object.environment = this;
Object.environment.fl=500;
Object.environment.maxDots = 39
Object.environment.currentDots = 0;


// create field onto which all dots will be attached
//this.createEmptyMovieClip("field1",1);
this.field1._x=610;
this.field1._y=250;

this.createEmptyMovieClip("field2",1);
this.field2._x=610;
this.field2._y=250;

this.createEmptyMovieClip("field3",1);
this.field3._x=610;
this.field3._y=250;

// track the position of the mouse for eternity
this.onEnterFrame = function() {
	// create a new dot if not yet at maximum
	//Object.environment.cntr = 5000;
//	Object.environment.cntrLimit = 5000;
//	if(Object.environment.cntr >= Object.environment.cntrLimit)
	{
		if (Object.environment.dotCount<Object.environment.maxDots) {
			Object.environment.dotCount++;
			Object.environment.currentDots++;
			makedot(this.field1);
			makedot(this.field2);
			makedot(this.field3);
		}
		else {
			//Object.environment.dotCount--;
//			Object.environment.currentDots--;
		}
		//Object.environment.cntr=0;
		angle += (296-50)/50;
		y = (-25);
	}
	//else{
//		Object.environment.cntr=Object.environment.cntr+1;
//	};
};

stop();

Open in new window

0
Comment
Question by:NKOWA
  • 7
  • 5
12 Comments
 
LVL 29

Expert Comment

by:dgofman
ID: 36600788
We need ot get FLA file for testing
0
 
LVL 5

Author Comment

by:NKOWA
ID: 36601377
Please see attached file... Orbs.fla
0
 
LVL 29

Accepted Solution

by:
dgofman earned 500 total points
ID: 36713339
I converted to AS3 but ofcourse you need to do some adjustment in Math.
If you created program in AS1 I am sure you will able to fix some code in Dot class

Good luck! OrbsAS3.fla
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 5

Author Closing Comment

by:NKOWA
ID: 36714073
Perfect! Thank you very much for converting the code!

Have a nice day!
0
 
LVL 5

Author Comment

by:NKOWA
ID: 36714081
I only have CS3, and the file you saved is giving me an error on open. Is there any way to convert or resave the FLA to CS3?
0
 
LVL 5

Author Comment

by:NKOWA
ID: 36714084
Or post the code?
0
 
LVL 29

Expert Comment

by:dgofman
ID: 36714100
I will try at home I have CS3 Installed
0
 
LVL 5

Author Comment

by:NKOWA
ID: 36714208
Thank you, again.
0
 
LVL 29

Expert Comment

by:dgofman
ID: 36714699
I don't have CS3

So here is my changes:

1) Goto Library and right click on "dot" symbol, select "Properties..."
Enable checkboxes "Export for ActionScript", "Export in frame 1" give a Class Name "Dot"
Click OK

Now I hope you will find those sources:

Double click on Dot symbol Library preview and update code for Dot symbol

// start off invisible
this.alpha = 0;
// set initial position
var yoffset:Number = Math.random() * 500;
var fl:uint = 500;
var timeToExit:Boolean = false;
var angle:Number = 0;
var ypos:Number = 0;
var vy:Number = 0;

var _valueY:Number;
var _valueX:Number;
var _valueZ:Number;
var _valueA:Number;

function set valueY(value:Number):void{
	_valueY = value;
}
function get valueY():Number{
	return _valueY;
}

function set valueX(value:Number):void{
	_valueX = value;
}
function get valueX():Number{
	return _valueX;
}

function set valueZ(value:Number):void{
	_valueZ = value;
}
function get valueZ():Number{
	return _valueZ;
}

function set valueA(value:Number):void{
	_valueA = value;
	angle = value;
}
function get valueA():Number{
	return _valueA;
}

addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
// get movin'
function onEnterFrameHandler(event:Event) {
	// calculate radian value
	var rad:Number =  angle*Math.PI/180;
	// calculate radial position
	var xpos:Number = Math.sin(rad)*valueX+Math.cos(rad)*valueZ;
	var zpos:Number = Math.sin(rad)*valueZ-Math.cos(rad)*valueX;
	// calculate size
	var scale:Number = fl/fl+zpos;
	//trace(angle + "+"+ rad + "+"+ xpos + "+"+ zpos +"+"+ scale + "+"+ ypos);
	// set scren position 
	
	this.x = xpos*scale;
	this.y = ypos*scale;
	
	// set screen size
	//this.scaleX = scale*100;
	//this.scaleY = scale*100;

	
	// one chance in 1000
	if (!Math.random() * 1000) {
		// time to float away
		timeToExit=true;
	}
	if (timeToExit) {
		// slowly levitate towards destruction
		angle += (valueA-angle)/25;
		// acceleration
		vy -= .2;
		// adjust position by velocity
		ypos += this.vy;
		// fade out
		alpha--;
		// if all the way faded, decrement count and self-destruct
		if (alpha<0) {
			//Object.environment.dotCount--;
			//this.removeMovieClip();
			alpha+=4;
			timeToExit=false;
		}
	} else {
		// slowly move towards global perspective
		angle += (valueA-angle)/25;
		vy = (valueY-ypos+yoffset)/20;
		ypos += vy;
		// slowly fade in
		if (alpha<100) {
			alpha+=4;
		}
	}
};

Open in new window


logic Frame 1

// register root as environment
//Object.environment.fl=500;

var neoAngle:Number = 0;
var neoY:Number = 0;
var dotCount:uint = 0;
var maxDots:uint = 39;

addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
// track the position of the mouse for eternity
function onEnterFrameHandler(event:Event) {
	neoAngle += (296-50)/50;
	neoY = (-25);
	
	if (dotCount < maxDots) {
		dotCount++;
		makedot();
		makedot();
		makedot();
	}
	else {
		removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
	}
};

stop();

Open in new window


functions : Frame 1

// create a single instance of the dot movieclip
function makedot() {
	if (dotCount < maxDots) {
		var neo:Dot = new Dot();
		neo.valueA = neoAngle;
		neo.valueY = neoY;
		neo.valueX = .5 * Math.random() * (45 * 2) - 45;
		neo.valueZ = .5 * Math.random() * (45 * 2) - 45;
		addChild(neo);

		neo = new Dot();
		neo.valueA = neoAngle;
		neo.valueY = neoY;
		neo.valueX = .5 * Math.random() * (65 * 2) - 65;
		neo.valueZ  =.5 * Math.random() * (65 * 2) - 65;
		addChild(neo);

		neo = new Dot();
		neo.valueA = neoAngle;
		neo.valueY = neoY;
		neo.valueX = .5 * Math.random() * (100 * 2) - 100;
		neo.valueZ = .5 * Math.random() * (100 * 2) - 100;
		addChild(neo);
		
		neo = new Dot();
		neo.valueA = neoAngle;
		neo.valueY = neoY;
		neo.valueX = .85 * Math.random() * (200 * 2) - 200;
		neo.valueZ = .85 * Math.random() * (200 * 2) - 200;
		addChild(neo);
	};
}

Open in new window




0
 
LVL 5

Author Comment

by:NKOWA
ID: 36714895
Looks good thank you so much.
0
 
LVL 29

Expert Comment

by:dgofman
ID: 36718962
Did you able to fix some Math fucntions and get the same result as AS1?
0
 
LVL 5

Author Comment

by:NKOWA
ID: 36719195
I'm still working on it, but right now it doesn't even display the orbs.

In AS3, angle is now rotation and & z is now setChildIndex(..), and so on..

Thank you for your energy.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question