Flash AS2 to AS3

A place for tutorials on how to get the most out of Flash

Flash AS2 to AS3

Postby Ivan-Aedler » Sun Mar 03, 2013 12:26 pm

Hello all,

Well, I was trying to find here a topic about that and I havent succeed.
I wanted to know the right steps to convert some games to AS3.
People say that we have to refactor everything but I do know some things can be just converted directly:

E.g.
AS2: _root.Test=10;
AS3: -> (root as movieclip).Test = new int(10);

AS2: _root.mc1.mc2.gotoAndPlay(1);
AS3: (root as movieclip).mc1.mc2.gotoAndPlay(1);

AS2: key.IsDown
AS3: eventHandlers with a function.

My doubts are about:
1. Whats the best way for me to include a list of functions? I have, say, 100 functions in '_root' level.
I was creating external AS files lately, but I import them all from _root (#include file1.AS, file2.AS), so everything is based on _root yet.
How to do that in AS3? Do I have to create classes? Is there a 'root' class I can include functions for everyone? If so, is that the same classname as the main project?
I know that its good practise to create each class for each 'set' (say, a class for enemies, a class for powerups, a class for screen mechanics). But I think that, in the beginning, and in a small game, it shouldnt hurt putting them in the main class.

Its important for me to keep some code in timelines (I'm not saying those code in the instances. I dont recommend using those). Which other possible things I need to take in account when converting code inside them in order to search for variables along other movieclips?

2. Where is _global? Is there a 'global' variable instead? Thats because I use global properties like _global.LEVEL.item1,2,3,4,5, _global.GAMECONFIG.

Thank you in advance!
I really want to train myself to convert AS2 games to As3 (refactoring) the easiest (and wise) way possible, even if its still a lot of work.
User avatar
Ivan-Aedler
 
Joined: Fri Jun 03, 2011 6:34 am
Location: GMT -3

Re: Flash AS2 to AS3

Postby OwnerOfSuccuby » Sun Mar 03, 2013 4:21 pm

It is possible to wright on AS3 as you do on AS2. I do not like millions of classes and etc - so i make it like in old way.

If you need global variables - all functions that are on the root layer are globals and you always can use it like (root as MovieClip).variable or function

The same to the all functions and vars by the way that are in some MovieClip Object:

Just as example you can look some my projects there - all of them are on AS3 (i even try to rewrite LoK game on it - and i do but as understand poeple are not interesting in it :mrgreen: ;) )

Just as example how to work with variables you can look our game TFT - i post its fla in the topic Tit fight tournaments or some thing like that. There even is some kind of girls DB there based on MovieClip :mrgreen:

It is better to ask a questions with some examples - it will be simpler to answer it, becouse for me it is a little hard to understand some times what do you need ;) - but understand code and what to do with it is simpler :mrgreen:

By the way to make some thing on AS3 takes more time then make it on as2 i think.
OwnerOfSuccuby
 
Joined: Fri Jun 11, 2010 9:33 pm

Re: Flash AS2 to AS3

Postby Ivan-Aedler » Sun Mar 03, 2013 11:48 pm

Thank you for your response, OwnerOfSuccuby.
I'm glad I'm no javaman :lol: They tend to use classes everywhere, maybe because of RUP classes and business pressure. I really like to optimze things (I'm from the C era) then making fewer classes, provided you understand it and maintenance can be done without hassle, so far so good ;)


OwnerOfSuccuby Wrote:If you need global variables - all functions that are on the root layer are globals and you always can use it like (root as MovieClip).variable or function

So it works as global, either variables or functions? Neat!
(we hope no skilled AS3 programmer thinks we are blasfemating, lol)

OwnerOfSuccuby Wrote:Just as example how to work with variables you can look our game TFT - i post its fla in the topic Tit fight tournaments or some thing like that. There even is some kind of girls DB there based on MovieClip :mrgreen:

I'll check, thanks, great man!

OwnerOfSuccuby Wrote:It is better to ask a questions with some examples - it will be simpler to answer it, becouse for me it is a little hard to understand some times what do you need ;) - but understand code and what to do with it is simpler :mrgreen:

I'll give some examples here, let me just go away of this place (there are no Flash here).

OwnerOfSuccuby Wrote:By the way to make some thing on AS3 takes more time then make it on as2 i think.

Yes, for sure, but nothing better than copy/paste (one we understand our code well).
The good thing about AS3: we have '_currentLabel' function, we can control FPS rate, we can have thousands of animated objects without too much lag (due to the way pools of graphics are cloned in memory), and we can use more components, even interesting ones like MIDI support and MP3 property changes, like tempo/pitch/loop inside the music!
Last edited by Ivan-Aedler on Mon Mar 04, 2013 2:28 am, edited 2 times in total.
User avatar
Ivan-Aedler
 
Joined: Fri Jun 03, 2011 6:34 am
Location: GMT -3

Example code

Postby Ivan-Aedler » Mon Mar 04, 2013 2:05 am

Here is code inside a character MC (timeline - frame 1) that will need conversion to AS3:
Code: Select All Code
//FRAME 1
onEnterframe = function()
{
   if (!obj.notice) {
      if (!_root.Character.hitTest(obj))  //to be used in the future to check other variables
      {
         if (obj.isShowingButton) {
            obj.isShowingButton = false;
            _root.interactiveButton_remove();
         }
      }

      else if (obj.canInteract) //if Character collide
      {
         if (!obj.isShowingButton) {
            _root.interactiveButton_show();
            _root.interactiveButton_add();
            obj.isShowingButton = true;
         }
         if (_global.FLAGS.interactiveButtonPressed || _root.Character._state == "backside") {
            obj.notice = true;
            if (_type == "key") {
               if (_root.CharacterHasKey == "yes")
                  obj.CharacterHasKey = true;
            }
         }
      }
   }
}
}

User avatar
Ivan-Aedler
 
Joined: Fri Jun 03, 2011 6:34 am
Location: GMT -3

Re: Flash AS2 to AS3

Postby BlueLight » Mon Mar 04, 2013 2:30 am

You make classes when you need to. That being said, my parents have told me about some projects they looked at by a sub division of their company that had so many classes in the program that it took 2 hours to go thought the names

But OO programming is one of the best inventions of the digital world. The other one i can think of is programming languages humans can read and understand reasonable well.
User avatar
BlueLight
Gangs n' Whores Developer
 
Joined: Sat Jun 04, 2011 8:23 am

Re: Example code

Postby OwnerOfSuccuby » Mon Mar 04, 2013 9:12 am

Ivan-Aedler Wrote:Here is code inside a character MC (timeline - frame 1) that will need conversion to AS3:
Code: Select All Code
//FRAME 1
onEnterframe = function()
{
   if (!obj.notice) {
      if (!_root.Character.hitTest(obj))  //to be used in the future to check other variables
      {
         if (obj.isShowingButton) {
            obj.isShowingButton = false;
            _root.interactiveButton_remove();
         }
      }

      else if (obj.canInteract) //if Character collide
      {
         if (!obj.isShowingButton) {
            _root.interactiveButton_show();
            _root.interactiveButton_add();
            obj.isShowingButton = true;
         }
         if (_global.FLAGS.interactiveButtonPressed || _root.Character._state == "backside") {
            obj.notice = true;
            if (_type == "key") {
               if (_root.CharacterHasKey == "yes")
                  obj.CharacterHasKey = true;
            }
         }
      }
   }
}
}



If you enter frame - all code on this frame will execute - what is "onEnterframe = function()" ?



-----------------

Hit test now changed on hit object - so it will look like:

Code: Select All Code
  if (this.hitTestObject((root as MovieClip).Player1.body)){(root as MovieClip).CHF=4;
    //trace("din din");
    if ((root as MovieClip).StarShipFirstStagemoveUp==1) {
     (root as MovieClip).StarShipFirstStagemoveUp=2;


If i did not make a misake it will look some kind of :

Code: Select All Code
   if (!obj.notice) {
      if (!(root as MovieClip).Character. hitTestObject (obj))  //to be used in the future to check other variables
      {
         if (obj.isShowingButton) {
            obj.isShowingButton = false;
           (root as MovieClip).interactiveButton_remove();
         }
      }

      else if (obj.canInteract) //if Character collide
      {
         if (!obj.isShowingButton) {
           (root as MovieClip).interactiveButton_show();
           (root as MovieClip).interactiveButton_add();
            obj.isShowingButton = true;
         }
         if ((root as MovieClip).FLAGS.interactiveButtonPressed || (root as MovieClip).Character._state == "backside") {
            obj.notice = true;
            if (_type == "key") {
               if ((root as MovieClip).CharacterHasKey == "yes")
                  obj.CharacterHasKey = true;
            }
         }
      }
   }
}


onEnterframe = function() some example:

Code: Select All Code
if (!this.hasEventListener(Event.ENTER_FRAME)){this.addEventListener(Event.ENTER_FRAME,fMyTimer);}


What you may need examples:
Code: Select All Code
if ((parent as MovieClip).hitobjGoNextEpisode!=undefined){(parent as MovieClip).hitobjGoNextEpisode.fGoNextEpisode();}


This need to be write on the first loaded frame (to make flash focus on it):

Code: Select All Code
stage.focus=stage;


You can change quality like this: stage.quality = "medium";
OwnerOfSuccuby
 
Joined: Fri Jun 11, 2010 9:33 pm

Re: Example code

Postby Ivan-Aedler » Tue Mar 05, 2013 11:18 am

Thanks for the directions, OwnerOfSuccuby ;)
I just have some questions.
OwnerOfSuccuby Wrote:If you enter frame - all code on this frame will execute - what is "onEnterframe = function()" ?

OnEnterFrame is a 'timeline' version of the 'on-object' (instance code) named 'onClipEvent(enterFrame'). As you know, instance codes are a mess, and were discontinued in AS3 (for God sake).

But onEnterFrame is so important because it is like a timer, which check a given code in each frame without us copying and pasting the same code in all frames.
It does basically one thing: it executes code in each frame of the movieclip, even if the frame is stopped (with this.stop())!
So if you have three frames, 'walking', 'stop', 'fight', and you have a onEnterFrame=function() in frame 1 to, say, check if someone is coming,
it will execute in each state (walking, stop, fight), as its omnipresent.
You can even put a 'stop'() in the frame1, and make onEnterFrame check something, then put the play pointer to frame2 or any frame you want. onEnterFrame will be like a maestro, putting the play pointer (the current frame it's playing) in every place you choose.

Once casted, you can stop onEnterFrame:
1) coding a line with 'delete onEnterFrame' in a given frame in a case that, say, the player died. It will delete itself forever.
2) coding a FLAG inside onEnterFrame, like 'if (active), do that'. So, if active is false, its like onEnterFrame is empty. This is better because you can reactivate it in the future.

OwnerOfSuccuby Wrote: onEnterframe = function() some example:
Code: Select All Code
if (!this.hasEventListener(Event.ENTER_FRAME)){this.addEventListener(Event.ENTER_FRAME,fMyTimer);}

Ok its a line that checks if the listener is set, if not, it creates onenterframe for me?
This one should be put once, because if in a loop, it could cut some CPU speed. But I know its good practise, because if I just put 'this.addEventListener(Event.ENTER_FRAME,fMyTimer);', in a case it already exits, it can throw an error, doesnt it?

And how about 'fMyTimer'? Is it the function I want to repeat on each frame?
Say, I create this:
Code: Select All Code
  fMyTimer= function()
    {
       if (!obj.notice) {
          if (!(root as MovieClip).Character.hitTestObject(obj))  //to be used in the future to check other variables
          {
             if (obj.isShowingButton) {
                obj.isShowingButton = false;
                (root as MovieClip).interactiveButton_remove();
             }
          }
 }

Then I do that below to create onEnterFrame for it?
Code: Select All Code
if (!this.hasEventListener(Event.ENTER_FRAME)){this.addEventListener(Event.ENTER_FRAME,fMyTimer)};


OwnerOfSuccuby Wrote:What you may need examples:
Code: Select All Code
if ((parent as MovieClip).hitobjGoNextEpisode!=undefined){(parent as MovieClip).hitobjGoNextEpisode.fGoNextEpisode();}

But what this one actually does? Does it check if the object exists, then, if so, it executes a given function?

OwnerOfSuccuby Wrote:This need to be write on the first loaded frame (to make flash focus on it):
Code: Select All Code
stage.focus=stage;

In this case, the first frame of SCENE1 (the root timeline)?
So if I play level 1, 2, 3, 4, in each 'header' of the current level, I put stage.focus=stage ONCE?

Thank you, really ;) I think I can cope with that.
User avatar
Ivan-Aedler
 
Joined: Fri Jun 03, 2011 6:34 am
Location: GMT -3

Re: Flash AS2 to AS3

Postby OwnerOfSuccuby » Tue Mar 05, 2013 3:57 pm

Code: Select All Code
OnEnterFrame is a 'timeline' version of the 'on-object' (instance code) named 'onClipEvent(enterFrame').


I did not know that - thank you - i always copy - paste :roll: :mrgreen:

if (!this.hasEventListener(Event.ENTER_FRAME)){this.addEventListener(Event.ENTER_FRAME,fMyTimer);}

Yes it checks and set listeners - but i do not know the anolog of your version OnEnterFrame in AS3 unfortunatly :roll: - i do not use in in as 2 so ... i make it a little different way.

I try to make it in cicle 100 times the same listeners for one event = it do not work. I mean i add the same listener 100 times that have to move object on +1 by x. It was moving the same for 1 listener and for 100. So i think that it looks it itself, but some times may be you will need to check some other events and conditions.

I do not remember why but i use this construction for main time line - on first enter frame. I try to make some kind of ultimate motion algoritms that can be the same for all game types - but it was not the good idea - and have to chech some different listeners there :mrgreen:

---

And how about 'fMyTimer'? Is it the function I want to repeat on each frame?


I better show you some example - i little do not understand how you want it to work - so i beter show it - and ask is it right or not :mrgreen: :lol:

Look in example fla and its trace - it works on each enter on frame in xxx object. Is it the same you want ?

But what this one actually does? Does it check if the object exists, then, if so, it executes a given function?


Some times it is possible that you will use object that will not exist or you forget to copy it (i forget to copy some parts of stage some times :roll: :mrgreen: ) - it wil let it work with out mistake - or for example if it is no such element in this stage but you use some universal model for it.

In this case, the first frame of SCENE1 (the root timeline)?


On the first frame of main time line. Becouse when flash loads some times it thinks that it was not selected and it is possible that it will not work on some buttons and some events. But if you start your flash with - click the button all will be good. But some times it is a bug so i always wright it even if it is no need in it :mrgreen:
OwnerOfSuccuby
 
Joined: Fri Jun 11, 2010 9:33 pm

Re: Flash AS2 to AS3

Postby Ivan-Aedler » Tue Mar 05, 2013 7:16 pm

OwnerOfSuccuby Wrote:I did not know that - thank you - i always copy - paste :roll: :mrgreen:

You can make a onEnterframe like in your example (addEventListener(Event.ENTER_FRAME,fMT);).
Its actually the same onEnterFrame but in AS3 ;)
So you already know how to use 'ominipresent' functions ;)
But in order not to repeat frame 1 all the time (in your example, when fMTy is 8, it resets itself), you can do this:

Code: Select All Code
if (!once) {
   once=true;
   var MyY:int=0;

  function fMTy(e:Event):void{
    MyY=MyY+1;
    trace(MyY); 
  }
}


So you can re-use frame1 and it will keep counting (8,9,10..20...1000.)
And you save CPU resources because you will not be recreating the function all the time ;)

OwnerOfSuccuby Wrote:Yes it checks and set listeners - but i do not know the anolog of your version OnEnterFrame in AS3 unfortunatly :roll: - i do not use in in as 2 so ... i make it a little different way.

But it is that event function! It does exactly what onEnterFrame does ;)

OwnerOfSuccuby Wrote:I try to make it in cicle 100 times the same listeners for one event = it do not work. I mean i add the same listener 100 times that have to move object on +1 by x. It was moving the same for 1 listener and for 100. So i think that it looks it itself, but some times may be you will need to check some other events and conditions.

Yes. I actually use ONE onEnterFrame (that is, I create one event) for each part I need to be checked all the time. But I can disable onEnterFrame (in this case, we can create a IF (ACTIVE) in the event function fMTy(e:event). If ACTIVE is false, t will keep running but it wont be doing anything until ACTIVE is true again.

OwnerOfSuccuby Wrote:I do not remember why but i use this construction for main time line - on first enter frame. I try to make some kind of ultimate motion algoritms that can be the same for all game types - but it was not the good idea - and have to chech some different listeners there :mrgreen:

You can use all Enter Frames in the main timeline, but you will need to instantiate / spawn movie clips to use them. I use ENterFrames inside timelines of MovieClips because it will execute only if this movie Clips exists.

OwnerOfSuccuby Wrote:Look in example fla and its trace - it works on each enter on frame in xxx object. Is it the same you want ?

Yes, thank you! Its that way. But I would rename fMTy to, say, EnemyAttack, ClothesChanger, or any function name that is more human readable ;)

OwnerOfSuccuby Wrote:Some times it is possible that you will use object that will not exist or you forget to copy it (i forget to copy some parts of stage some times :roll: :mrgreen: ) - it wil let it work with out mistake - or for example if it is no such element in this stage but you use some universal model for it.

Ok understood ;)

OwnerOfSuccuby Wrote:On the first frame of main time line. Becouse when flash loads some times it thinks that it was not selected and it is possible that it will not work on some buttons and some events. But if you start your flash with - click the button all will be good. But some times it is a bug so i always wright it even if it is no need in it :mrgreen:

Oh its because of some browsers that lose focus? Ok then ;)
Last edited by Ivan-Aedler on Tue Mar 05, 2013 8:12 pm, edited 1 time in total.
User avatar
Ivan-Aedler
 
Joined: Fri Jun 03, 2011 6:34 am
Location: GMT -3

Re: Flash AS2 to AS3

Postby OwnerOfSuccuby » Tue Mar 05, 2013 8:03 pm

Yes it is about lose focus - but i do not know is it mistake of browser player or etc.

SteelSaurus find interesting bug in our TFT project - as i understand = it start playing even though it has stop(); on the first frame.

But in my version and in browser all works - and in some version of program for playing flash it does not.

Now have some problems in flash too -I use old one - and may be they make something new in CS6 that support some thing that CS5 does not.

And when i try to make some new stuff from internet - that i find it say that - "Wtf OoS what do you want from me ? Go home - you are drunk :lol: "

I think to try CS6 soon - i hope that a lot of problems will go away.

But the one more thing that is bad - that a lot of functions do not support old players - they will not work on them. I did not upgrade i think for a year - so i even do not know what swf player do i have :roll: :mrgreen: :lol:
OwnerOfSuccuby
 
Joined: Fri Jun 11, 2010 9:33 pm

Re: Flash AS2 to AS3

Postby Ivan-Aedler » Wed Mar 06, 2013 2:11 pm

OwnerOfSuccuby Wrote:Yes it is about lose focus - but i do not know is it mistake of browser player or etc. SteelSaurus find interesting bug in our TFT project - as i understand = it start playing even though it has stop(); on the first frame. But in my version and in browser all works - and in some version of program for playing flash it does not.

I understand. Problem with Flash itself in different browsers. Its better to include the stage focus code.

OwnerOfSuccuby Wrote:Now have some problems in flash too -I use old one - and may be they make something new in CS6 that support some thing that CS5 does not. I think to try CS6 soon - i hope that a lot of problems will go away.

I've been paying for student editions/upgrades since CS3. I'm not happier with CS6 because although it saves files a lot faster, it compiles slower. In CS3, my game compiles up to 1min 20sec. In CS6, 3 minutes. However, saving in CS3 could last more than 2 days!! :o Did you experience that? Is it maybe a bug in the project settings I am unaware of?
User avatar
Ivan-Aedler
 
Joined: Fri Jun 03, 2011 6:34 am
Location: GMT -3

Re: Flash AS2 to AS3

Postby OwnerOfSuccuby » Wed Mar 06, 2013 6:15 pm

Checked it today - update to CS6 - there are some new functions that did not work in CS5.5.

The one more bad thing some functions do not work in flash only in air - so be careful when looking new stuff about flash in internet ;)

Today test all new stuff on my old project with Kry Smiles :mrgreen:

I try to understand how to get data from clipboard but unfortunatly it works only in AIR :roll:

But finally i killed Right Mouse Click Button :lol: :mrgreen:

When i try it on CS5 - it gives me mistakes =( May be some functions that work with bitmap data and byte array that i can not use work in it. (But i think it will not - Like PNGencode or some thing like that)
OwnerOfSuccuby
 
Joined: Fri Jun 11, 2010 9:33 pm


Return to Tutorials



Who is online

Users browsing this forum: No registered users