• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 896
  • Last Modified:

Convert AS1 to AS3

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
NKOWA
Asked:
NKOWA
  • 7
  • 5
1 Solution
 
dgofmanCommented:
We need ot get FLA file for testing
0
 
NKOWAAuthor Commented:
Please see attached file... Orbs.fla
0
 
dgofmanCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
NKOWAAuthor Commented:
Perfect! Thank you very much for converting the code!

Have a nice day!
0
 
NKOWAAuthor Commented:
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
 
NKOWAAuthor Commented:
Or post the code?
0
 
dgofmanCommented:
I will try at home I have CS3 Installed
0
 
NKOWAAuthor Commented:
Thank you, again.
0
 
dgofmanCommented:
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
 
NKOWAAuthor Commented:
Looks good thank you so much.
0
 
dgofmanCommented:
Did you able to fix some Math fucntions and get the same result as AS1?
0
 
NKOWAAuthor Commented:
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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now