Skip to main content
March 27, 2012
Answered

can I create a class and package for my inframe AS and access it global

  • March 27, 2012
  • 2 replies
  • 4698 views

Ls,

I made an app using only inframe actionscript in a .fla;

transfrerring variables from frame 1 to frame 2 is unreliable.

Some sources say the persist over frames but i found that not to be reliable.

Arrays seem to be filled with the right values. Normal int vars declared in frame 1 fail some time.

You should have used classes i hear you say,  and right you are.

But I didnt.

Now im using either reloading, re-initing and/or the sharedobject.

Anyhoe..

Question:

If I declare a class for my main .fla file.,

then declare, instantiate and load the globals i want in this main class,

will I be able to acces these variables in my main .fla timeline script?

If so, I could slowely reconstruct my .fla into smaller objects/classes and make it a real app.

Just starting from scratch is. welll..i dont even want to think about that.

Highest regards,

Mac

This topic has been closed for replies.
Correct answer sinious

Dear Sinious,

I feel faint at the concept of disputing anybody in here who gives solid advice.

Still...  I have seen it happen.

I understand the danger of global vars and the mess of having to many lines of code.

But I think it might have something to do with the amount of code/vars.

It sounds extremely noob I know. But read below.

I moved some general data vectors and init routines to the startframe as a way of spreading the startup time before actual level start.

These variables just didnt survive in the second frame and the whole thing stopped working.

(not errors but erratic behaviour of the app).

Also:

There was this basic function that was left unchanged from the start and worked fine.

It got shifted down a lot by adding more and more code above it.

At a certain point it failed at runtime, also not in error but in wrong behaviour of the code.

I then traced it and it showed 0, yet I inititalized it different and didnt touch it in any other function.

The declaration of the vars used was just above the function.

Then all i did was move the var declaration to the top of the framescript and it worked again.

It was not changed or used anywhere else.

This happend more and more when the code got longer.

Especially with vars declared in the first frame. So I just re-reinitialise them at the beginning now.

I ended up moving every declaration to the top.

Now i have to scroll a lot though.

Its a bit of a twilightzone thing but I worked around it and left it at that.

I'm sure it has a reasonable explanation wich, when i learn it, will make me stick my head in the toilet bowl and flush it.

Highest regards,

Mac


Oh, I know exactly your pain. I used AS2 a LOT longer than I should have. I had some apps exceeding 10,000 lines in a single class file and had flash say my file exceeded the max size and I couldn't even export. I also saw it mysteriously lose references to objects or "something else" adjusted or nulled my variables.

But that was AS2 and a long time ago...

I have never seen AS3 have this issue. You have a ton of code so I would completely expect, having known flash for over a decade, that it's failing. Flash isn't perfect, but AS3 losing variable data between frames seems very unlikely. I'm only suggesting that when you get to this much code, it's easy to overlook something that may be resetting your variables. If you trace()d them and they're not "undefined", they do exist, and apparently have been set to 0. That leads me to believe, somewhere, in that vast wilderness of code, "something else" is affecting those variables and it's not flash.

I've had many years of blaming flash under my belt. In AS2 it actually was the case sometimes. In AS3? I've always found the error to be my own..

2 replies

sinious
Legend
March 29, 2012

If you're using vectors for everything and you're simply repositioning them but not rotating/resizing/alphaing them then definitely turn cacheAsBitmap on every single MovieClip or Sprite you can.

e.g.

hud_background.cacheAsBitmap = true;

You will use more memory and if you are targeting a mobile device, just forget about this.

Vectors (and now 3D) are the hardest thing to render in flash. If it continuously needs to re-render the vectors it will cost you in performance. Things that do not change much like a HUD are good targets for the performance bump of cacheAsBitmap. Even objects that only transform (rotate/resize/change alpha) every now and then but largely stay the same for periods of time will gain the benefits of cacheAsBitmap.

The Vector class I speak of is merely to force something to always be an array of 'this type'. I just wanted to make it clear that it doesn't have anything to do with vector drawn graphics. It's a programmatic term in as3 that is just a higher performance array because you guarantee to flash that it will not change 'type' from holding nothing but, say, Sprites to holding MovieClips. You'll error if you try to assign a MovieClip. It's just a little less overhead for Flash.

As for my name, I've used the name ominous for about 20 years in games. Too many people were taking it so I went with sinister when it wasn't available. Now both names are constantly taken so sinious is just sinister and ominous combined. It's a silly name haha, but it's almost always available.. I'd give your best enemy NPC a badass name, not this "forum available" concoction

March 29, 2012

Man we should start a blog

My bad, I meant useing the vector array variant. Not the gfx.

Never thought of making a vector with my animation clips in there.

Basically i have now thrown everything overboard that i bought flash for =)))

Still its a good practice for using the spritesheets in stage3d.

I already use a verctor and framenumbers and gotoandstop to point to my npc animframes.

Now I can just sprite em all.  since im not actually using the mc for anything else than a container.

I do resize/rotate them alas in my own tween routine that makes em grow and wobble on respawn.

I did use cacheasbitmapmatrix for that but that might now not be neccesary.

Does cacheasBitmap and cacheasBitmapMatrix have the same effect on Sprites?

I did a turn chache off on the respawn tween anim and then turn cache on as soon as they were settled.

Couldnt really notice a difference.

I could predraw the tween gfx and add them as a vector sprite afcourse.....

But dude, there I go again.

Ill be busy another two weeks to do all that only to have the same thing just faster.

Hmm.... might be an alternative to stage3d though....

jeezz.. ill have to seriously think about this.

Well the main NPC starts of in the storyline as a helping hand and turns on you once you try to get away..

So this double meaning has the right ring to it

sinious
Legend
March 29, 2012

Here's an article explaining the differences between them and when to use them (in the context of mobile at least).

Quoting the article: "cacheAsBitmapMatrix property is only available for the AIR mobile runtime and projects made using the iPhone Packager."

If you're noly using the Matrix version, that's why you don't notice any bump . It's not working at all.

At any point an object becomes stable and only changes position (if you're not going mobile with this) it's a good idea to turn on cacheAsBitmap. Changing position x/y/z values is perfectly fine and will gain the full benefit of cacheAsBitmap. You will only ever not gain (and perhaps suffer) from transforming the object via resizing, rotating or changing alpha as the bitmap needs to be regenerated and cached every time you alter them.

So turn cacheAsBitmap on on when the object is only being repositioned or is at rest and not being transformed and before a transform simply turn it off, then back on when done.

Also if you're using the built-in Tween class, here's another large performance bump. Download TweenLite and use it! It's a MUCH better, hugely faster AS2/AS3 tween engine. I use it for everything. It does everything the built-in tween class does but it's much easier to use and craptons faster. Here's a performance demo showing the obvious performance gains over the built-in tween.

kglad
Community Expert
Community Expert
March 28, 2012

1.  i've never seen or heard of anyone have a variable unrelated to a displayobject defined in frame 1 fail to be defined in frame 2.

2.  if your document variables are not local to a function and they are unrelated to a displayobject, they will be available throughout your timeline just like frame 1 variables.

March 28, 2012

My dear 'old' friend,

Before going smartass on you again, ill explain why I asked this:

I really want to gradually redesign my .fla to OOP.

Me thinks, a good start would be to extract all the global vars into the main class.

Then I would slowely dare to extract pieces of the mainscript and convert them to classes one at a time.

All of this mainly because I have run out of performance improvements save porting all the gfx to stage3d.

Having it OOP-ed before would be preferred if not mandatory.

Its a lot of code though, so if I could do the mainclass trick, I might be able to still use something like ND2D without a full reconstruction.

1. Well now you have =/

Problem is not that they are undefined, just unreliable.

On testing this, vector and array values seemed to be in place.

Normally defined variables like int's are recognized (as they dont throw errors) but contain 0 (zero).

2. All the vars are declared at start of frame 1 outside any function.

I believe that it is perhaps because all inframe script is somehow related to the stage or something, wich is a displayobject.

Just guessing there (as usual).

2b. Perhaps it has to do with the size/amount of the AS in that frame.

I have discovered that once your code grows/exceeds a certain size, it will not be initialized correctly by the time a function using it is executed.

When testing(lazy) whenever I build a new function i declare the vars just above it. After a while, these vars where so far down, they started to not be initialised correctly. Only after moving them to the top of script would they work.

i.e. coded far down enough, this would trace to "test: 0".

var tester:int = 100;

showtester(tester);

function showtester(testint:int):void

{

trace("test:", testint);

}

Highest regards,

Mac

sinious
Legend
March 28, 2012

Frames do not lose variable information between them. Often people create such complicated "global access" code they don't realize that a variable is being affected by something else.

If it traces out to be "0" and not "undefined" then the variable is clearly defined, it just has been set to 0 by something else.

Here is a CS4 (downsaved) example of an int existing over 2 frames with breakpoints added so you can CTRL+SHIFT+ENTER debug test it, step into each frame and watch the variable persist just fine:

http://www.filehorde.com/o/frameIntExample.zip

OOP is great because inside the class you can mark these variables with permissions. Therefore nothing outside the class can change them without you explicitly doing it yourself. It helps to track down bugs.

That much code (thousands of lines) with global access variables (defined on the timeline) are bound to get out of control very quickly.