PDA

View Full Version : AS2 to AS3 migration - several Questions



lsarcher
February 7th, 2009, 07:55 AM
i am graphics designer & also programming php. I stopped working with flash as flash Player 5 or 6 came out, and now that i started to do flash again Actionscript is giving me a headache. I am trying to get through the Actionscript Essentials Book but i need to experiment on the side to understand it - or probably just to keep my mind sane...

Now i found a nice script in AS2 which i try to migrate to AS3 without much success. I managed to change the random() & define the variables.. but still many questions:

- mc._x or mc._alpha become mc.x and mc.alpha?
- _xmouse+1 now gets mouseX+1?
- var o = this.attachMovie("part", "p"+i, i); gives me error 1060: attachMovie is no longer supported. But the solution goes over my head. How do i need to implement that?

i have attached the samplecode if someone wants to take a look. and i hope the question wasn't too stupid :-)




var n = 15;
var vf = 3; //Stir factor
var af = 0.9; //Air fric
var bf = 0.1; //Bounce
var ks = 0.0; //Spring
var g = 0.0 //Gravity

var sw = 1000;
var sh = 500;
for(var i=0;i<n;i++){
var o = this.attachMovie("part", "p"+i, i);
o._x = Math.random()*400;
o._y = Math.random()*150;
o._alpha = Math.random()*200;
var xscale= Math.random()*100;
var yscale= xscale+Math.random()*20;
o._yscale = yscale;
o._xscale = xscale;
o._rotation = Math.random()*360;
o.x0 = o._x;
o.y0 = o._y;
o.vx = 0;
o.vy = 0;
}
var mpx = mouseX+Math.random()*1;
var mpy = mouseY+Math.random()*1;
this.onMouseMove = function(){
var dx = mouseX - mpx;
var dy = mouseY - mpy;
for(i=0;i<n;i++){
o = this["p"+i];
var ox = mouseX - o._x;
var oy = mouseY - o._y;
var oa = Math.sqrt(ox*ox+oy*oy);
if(oa != 0){
o.vx += dx*vf/oa;
o.vy += dy*vf/oa;
}
}
mpx = mouseX;
mpy = mouseY;
}
this.onEnterFrame = function(){
for(i=0;i<n;i++){
o = this["p"+i];
o.vx -= (o._x - o.x0)*ks;
o.vy -= (o._y - o.y0)*ks;

o.vx *= af;
o.vy *= af;
o.vy += g;
o._x += o.vx;
o._y += o.vy;
if(o._x>sw){
o.vx *= -bf;
o._x = sw;
}
if(o._y>sh){
o.vy *= -bf;
o._y = sh;
}
if(o._x<0){
o.vx *= -bf;
o._x = 0;
}
if(o._y<0){
o.vy *= -bf;
o._y = 0;
}
}
}
thx
kami

Alber Kidd
February 7th, 2009, 08:09 AM
- mc._x or mc._alpha become mc.x and mc.alpha?
- _xmouse+1 now gets mouseX+1?
- var o = this.attachMovie("part", "p"+i, i)


First two are correct, the last one is now addChild();

I suggest that you buy this book, this will help you get your head around the language

http://www.amazon.co.uk/Essential-ActionScript-3-0-Colin-Moock/dp/0596526946/ref=sr_1_2?ie=UTF8&s=books&qid=1234011790&sr=1-2

also you could join Lynda.com for a couple of months as there are loads of good AS3 video tutorials on there for practical examples.

It is a fairly steep learning curve I am just starting to get reasonably competent after about a year. Ask enough questions on here and you will get there.

Cheers,


Ak

senocular
February 7th, 2009, 10:49 AM
This might help too:
http://www.senocular.com/flash/tutorials/as3withflashcs3/

it covers a lot of the changes including movie clip properties (_x, _alpha, etc) and working with the display list over attachMovie

gtg236s
February 7th, 2009, 11:11 AM
var o = this.attachMovie("part", "p"+i, i);This one is probably the hardest one for people to understand especially for people without Object Oriented Programing experience. I don't really know your experience so I'll assume not much in AS3 and OOP.

So in AS2 attachMovie function was used to bring a movieclip that you had 'linked' in the library onto the stage of the swf. In order to do this you had to define a linkage name to the movieclip inside the library.

Well for for AS3 it is almost the same. When you create a new movieclip, click on that Advanced button, or in the library right click and select Properites. You will notice now that the old Indentifier field from AS2 is blanked out, but right below that is the Class field. Go ahead and put your linkage name there in your example it would be 'part' (I would suggest capitalizing any Class names just b/c that is what most developers do)

The concept is you just created a new Class in your flash movie. Classes are like blue prints and they make Objects (which can be anything) in your program. So now you created the blue print for 'Part' now it is time to make it an object (MovieClip).

Declare a new variable and datatype it to the name of your new Class.


var o:Part = new Part();Now you just add it to the displayList by going


addChild(o);That's it. :) Hope that helps. I would highly recommend Alber Kidd's book recommendation. It goes into OOP and how to use it with AS3. That is important since Abobe really designed AS3 around OOP concepts. It also goes into correct naming conventions developers us in their programs.

This is very helpful especially if you're the designer and working with a developer on a flash app. There have been many times as a developer I wanted to reach through the screen and strangle the designer I was working with. :) Take care.
~gtg236s

lsarcher
February 8th, 2009, 06:48 AM
Pretty happy i've already bought the right book: AS Essentials & Cookbook. But it's very exhausting to get through it and i fear, even beeing on page 140 i just understood 80%...
I definately understood that animals in a zoo need to be feeded :-)

So i managed to get further. Corrected the variables and added EventListener for Mousemove and Enterframe. I also tried to change the addmovieclip, but now flash just freezes when compiling :-)
i changed the properties of the mc. to:
class: Blaetterfilm
base class: flash.display.MovieClip

I think it's because i am neither defining depth nor newName?



import flash.events.*;


var n = 5;
var vf = 3; //Stir factor
var af = 0.9; //Air fric
var bf = 0.1; //Bounce
var ks = 0.0; //Spring
var g = 0.0 //Gravity

var sw = 1000;
var sh = 500;
for(var i=0;i<n;i++){
var o:Blaetterfilm = new Blaetterfilm();
addChild(o);
// var o = this.attachMovie("part", "p"+i, i);
o.x = Math.random()*400;
o.y = Math.random()*150;
o.alpha = Math.random()*1.2;
var px_scaleX= Math.random()*0.9;
var px_scaleY= px_scaleX+Math.random()*0.2;
o.scaleY = px_scaleY;
o.scaleX = px_scaleX;
o.rotation = Math.random()*360;
o.x0 = o.x;
o.y0 = o.y;
o.vx = 0;
o.vy = 0;
}
var mpx = mouseX+Math.random()*1;
var mpy = mouseY+Math.random()*1;
this.addEventListener(MouseEvent.MOUSE_MOVE,blaett erfilm);
function blaetterfilm(evt:Event):void{

var dx = mouseX - mpx;
var dy = mouseY - mpy;
for(i=0;i<n;i++){
o = this["p"+i];
var ox = mouseX - o.x;
var oy = mouseY - o.y;
var oa = Math.sqrt(ox*ox+oy*oy);
if(oa != 0){
o.vx += dx*vf/oa;
o.vy += dy*vf/oa;
}
}
mpx = mouseX;
mpy = mouseY;
}
this.addEventListener("enterFrame",onEnterFrame);
function onEnterFrame(e:Event) {
for(i=0;i<n;i++){
o = this["p"+i];
o.vx -= (o.x - o.x0)*ks;
o.vy -= (o.y - o.y0)*ks;

o.vx *= af;
o.vy *= af;
o.vy += g;
o.x += o.vx;
o.y += o.vy;
if(o.x>sw){
o.vx *= -bf;
o.x = sw;
}
if(o.y>sh){
o.vy *= -bf;
o.y = sh;
}
if(o.x<0){
o.vx *= -bf;
o.x = 0;
}
if(o.y<0){
o.vy *= -bf;
o.y = 0;
}
}
}
and YES i have not muc knowledge in OOP and YES i want to be a designer that knows how things work... If i haven't picked up OOP till April i go back to photography :-)


Thanks

kami

lsarcher
February 9th, 2009, 01:38 PM
The attached Movies were named p1...pn. How can i manage that with the addChild() function?



var o:Blaetterfilm = new Blaetterfilm();
addChild(o);
// AS2 Code var o = this.attachMovie("part", "p"+i, i);
thx
kami