添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
May 16, 2012 May 16, 2012

I am more than a little frustrated.  I'm using Flash Professional CS5.5, attempting to publish ActionScript 3.0 for FlashPlayer 10.2

My code begins with:

import flash.events.MouseEvent;

import flash.events.Event;

import flash.events.KeyboardEvent;

import flash.utils.Timer;

import flash.events.TimerEvent;

Then much later I have:

teamFourCar_mc.addEventListener(Event.ENTER_FRAME, trackPosition);

function trackPosition (event:Event)

{

and I keep getting a compiler error:

Scene 1, Layer 'actions', Frame 1, Line 434 1046: Type was not found or was not a compile-time constant: Event.

I have imported the class "Events".  I have successfully used the same construction in other scripts... in fact, virtually the same construction is included in Code Snippets as Fade In and Fade Out.  Adding ":void" after (event:Event) makes no difference.  I have Googled - to no avail - and spent the better part of a day reading through the on-line help files, but the solution is still eluding me.

It's probably so simple I'm going to smack myself in the forehead and mutter "Duh!" when I find a solution...

If you can do anything to hasten that event (no pun intended) I (with the possible exception of my forehead) will be very grateful.

TIA

Terry

Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more May 16, 2012 May 16, 2012
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more May 16, 2012 May 16, 2012
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more May 17, 2012 May 17, 2012
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more May 17, 2012 May 17, 2012

There is nothing wrong with the lines of code as you have them written, so the problem lies elsewhere.  If you haven't doublechecked, make sure your publish settings are set for AS3.  Otherwise, there could be an error in some other line that is leading up to line 434

Thank you for confirming that... I was beginning to doubt my memory.

I checked again, and I am attempting to publish AS3 for FlashPlayer 10.2.  Can you - or anyone - give me a hint as to what kind of error I'm looking for?  If it helps, there are 3 identical compiler errors, 2 for a function triggered by ENTER_FRAME and 1 triggered by COMPLETE (from loader.info).  When, in desperation, I changed this last example from (event:Event) to (event:Loader.info) [which may not even exist] the compiler error disappeared.

Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more May 17, 2012 May 17, 2012

Theorizing what the issue could be is about as useful as a mechanic guessing what's wrong with your car from 30 feet away.

We need to see under the hood. We need full error messages, line numbers, the code on those exact lines and especially a few lines of code above and below the problem lines along with the lines that call those functions.

When you encounter something like you're saying my initial guess is it's always the code that that either called the function or it's a syntax error above that line of code.

Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more May 17, 2012 May 17, 2012

Sinious:

The entire code of the main movie clip is at http://pastebin.com/JYfLUhh1

A sample of one of the four "problem" movie clips is at http://pastebin.com/gQGDyngx

There is nothing wrong with the "problem" clip.  The problems are in the main movie clip:

Scene 1, Layer 'actions', Frame 1, Line 427 1046: Type was not found or was not a compile-time constant: Event.
Scene 1, Layer 'actions', Frame 1, Line 612 1046: Type was not found or was not a compile-time constant: Event.
Scene 1, Layer 'actions', Frame 1, Line 1819 1046: Type was not found or was not a compile-time constant: Event.

There are the only three occurrences of a function with (event:Event).  Two are ENTER_FRAME (427 & 1819) The other is COMPLETE from Loader.info

Auto format reported a syntax error "near 'if(movingCar.currentFrame == (dieRoll * 24) + movingCarPosition)' " but I can't detect what, if anything, is wrong with it.

I'm not an experienced developer... in fact, the last programming I did before taking up Flash three months ago was with QuickBasic under DOS 6.2... therefore, please try not to roll your eyes too much when you see how inelegant the code is.

I have a working version, but it's over 7,000 lines and a memory hog.  I'm trying to make it more elegant and more efficient.  BTW, the three (event:Event) functions are in the working version in more or less the same places, with the same calling code.

I hope you can figure it out.

Thank you in advance

Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more May 17, 2012 May 17, 2012
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more May 17, 2012 May 17, 2012
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more May 18, 2012 May 18, 2012

You're defining functions inside other functions. That's going to confuse the heck out of you really quick. AS3 doesn't need you to nest those functions for any optimizing purposes, it just makes things confusing. Unfortunately it also won't stop you either.

If you put them at the same level all the functions will work just fine, there's no real reason you needed to nest dozens of functions inside of each other.

So I recommend you go into the function prepateToRoll() which has tons of nested functions and take them all out. Also the functions nested inside the nested functions, (like function step_1() { function unstep_1() { } } ), get them all out of each other and place them at the same level as prepareToRoll().

Once you do that you'll find that your app may still have issues but it will work as well as it did before. You may also find this solves your problem. You'll see nesting those functions was entirely unnecessary. Your errors will be a lot easier to fix and your code will be a lot easier to read.

edit:

A last note on all those redundant step_#, unstep_# functions.. It looks like the only difference between them is the number of the instance you're affecting. You could simply track which step you're on using a simple variable:

var _currentStep:int = 1;

Or, you can read the 'instance name' of the step button being pressed (I see you have btn_1, btn_2, etc). Then read that number in the function and use it to do what appears to be the same thing. That will remove GOBS of code you have there. A method that could read the button name might look like this:

Pastebin Example

Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more May 18, 2012 May 18, 2012

Holy crap.

At least you didn't laugh!

If you put them at the same level all the functions will work just fine, there's no real reason you needed to nest dozens of functions inside of each other.

So it doesn't matter where the function is declared (I'm assuming "declared" == "defined")? I could, for example, declare all the functions at the top of the code, then just call them at the appropriate time?

This will mean that almost all of my variable will have to be global, yes? (As I understand it, a variable declared within a function, ie between { }, is only available within that function and is discarded.)

Thank you for the suggestion (and sample code).  This is exactly the kind of help I was looking for... the books and video tutorials are useful, but this addresses a specific need.

To be clear, will this change affect how much processor time or memory is involved? My guess is no, because all the same steps have to be carried out, but it makes the code much more elegant.

Thank you again: my original problem remains unsolved (for the moment) but you have taught me a great deal.  I have already thought of places in the code where I could employ the same method to reduce the volume.

Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more May 21, 2012 May 21, 2012

Often there's very few variables you need to keep in global scope. If you find you have a ton of them, that's when you make an array or object and start sticking the references and values in there.

e.g.

var settings:Object = new Object();

settings.someVar = value;

settings.someOtherVar = value;

settings.someClipReference = some_clip_mc;

or

var lotsOfObjectReferences:Array = new Array();

lotsOfObjectReferences.push(someClip 1 );

lotsOfObjectReferences.push(someClip 2 );

lotsOfObjectReferences.push(someClip 3 );

lotsOfObjectReferences.push(someClip 4 );

lotsOfObjectReferences.push(someClip 5 );

By doing that it keeps a bunch of related information contained inside a single object for easy access (and removal).

Yes you definitely want to put all these functions on a global scope. There's no optimization difference between either approach on a modern computer, but one is much easier to debug and memory friendly.

You'll be more optimal bringing every function down to global scope because you won't be re-creating a function and assigning it over and over. By creating it inside the function scope over and over like you are, you are creating a new "anonymous" function every time you assign it. That can be optimized by pointing all the clips to the same single global function. Then only one function is in memory and all the clips use it.

After you get done rewriting it feel free to post it back. Only then can I really speculate on what the errors might be as the structure and line numbers will change.

Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more May 23, 2012 May 23, 2012

Often there's very few variables you need to keep in global scope. If you find you have a ton of them, that's when you make an array or object and start sticking the references and values in there.

Define "a ton of them".  I just counted 81 global variables in ± 2,100 lines (of which probably 1,300 lines are actual code , with appropriate white-space for readability).

Does this qualify as "a ton of them"?  I'm beginning to suspect it might.  Does stuffing them into an array, or attaching them to a generic object reduce memory use? If not, how else does it help?

By doing that it keeps a bunch of related information contained inside a single object for easy access (and removal).

How do I remove an object or an array used to store variable?  I'm familiar with removeChild() for removing objects from the display list, but that's all.  It seems to me that using an array would be most useful if I wanted to cycle through the stored values in an incrementing loop, for example.  Otherwise I think using an object might be better because the names would be easier to keep straight in my memory than the array index position. (To quote an old BBS tagline, "The older I get, the better I used to be.")

Your suggestion to un-nest most of my functions worked.  The "1046: Type was not found or was not a compile-time constant: Event." compiler errors disappeared,  They were replaced by 126 other compiler errors, but I eventually solved all of those and I've reduced the size of the source code to about 25% of the original size.  I would have to say that your guess from 30 feet away was exceptionally astute.  Thank you again.

Terry

Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more May 16, 2012 May 16, 2012
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more May 16, 2012 May 16, 2012
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more May 16, 2012 May 16, 2012
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more