Solved

Convert AS1 to AS3

Posted on 2011-09-26
12
840 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now