|
-
Trainee coder
[RESOLVED] [HELP] 'switch' statement limit (???)
Hey everyone,
I'm in need of some quick help for something that's baffling me atm. So basically I've got my game set up so that upon completing a level a function which runs a 'switch' statement to create the next level. Something along these lines:
PHP Code:
function createLevel (lvlnum:Number):Void {
createWorld();
switch (lvlnum) {
case 1:
// stuff would happen here to create lvl 1
break;
case 2:
// stuff would happen here to create lvl 2
break;
// repeat this process until there are 20 lvl cases
}
//do general to run each time a new lvl is created (like reset scores, rest lives, etc.)
}
So what's baffling me is that all of a sudden when I tried to add the 20th case (the 20th level) I got an error in FlashDevelop telling me "This file is not a valid AS2.0 class". I tried to compile the file in Flash and it said it couldn't do it with an error of something alonmg the lines of "couldn't compile .swf... please check if the file is a read-only or if it is being used by another application".
None of that made sense, so I restarted the computer when I got back home. I still get the error in FlashDevelop of an invalid AS2 class, but now Flash compiles the game fine (without an error).. but wait there's more. The bast*rd works all the way to the 19th level, but when it hits the 20th lvl (and attempts to run the 20th case) it doesn't run the case. The 'next level' button seems to work fine (I've run some traces) but using the debugger and traces I've found not only does the switch function not run, but some variables become undefined. Now, it's possible that these variables would be losing their values because the switch statement isn't being fired properly, but there's so much random crap and errors going on I've got no idea why this could've occurred (especially considering I’m 95% sure that the only change I made to the code when this happened was adding another case/level).
So unless someone understands the error message from FlashDevelop, I'm thinking AS has a limit of how many cases one switch statement can handle; but when I delete the 20th case, I still get this error from FD!.. So yeah, I'm completely lost, I'm trying to stick to a close deadline on this game and so any help would be brilliant to hear atm.
Cheers,
Viza.
-
where is your default case with break? ,- not sure but could be your problem
-
What happens if you put the 'case 20:' code at the top of the switch statement?
-
Senior Member
Had to try it for fun, sorry it's script generated but you can test it. It shows all the cases correctly
I guess with huge switch statements there's some small character in a wrong place somewhere. Probably around level 19?
Your swf might also be corrupted so try deleting it
PHP Code:
for(var i:int = 0; i< 50;i++){ changeMap(i); } function changeMap(i:int):void{ switch(i){ case 0: trace(0) break; case 1: trace(1) break; case 2: trace(2) break; case 3: trace(3) break; case 4: trace(4) break; case 5: trace(5) break; case 6: trace(6) break; case 7: trace(7) break; case 8: trace(8) break; case 9: trace(9) break; case 10: trace(10) break; case 11: trace(11) break; case 12: trace(12) break; case 13: trace(13) break; case 14: trace(14) break; case 15: trace(15) break; case 16: trace(16) break; case 17: trace(17) break; case 18: trace(18) break; case 19: trace(19) break; case 20: trace(20) break; case 21: trace(21) break; case 22: trace(22) break; case 23: trace(23) break; case 24: trace(24) break; case 25: trace(25) break; case 26: trace(26) break; case 27: trace(27) break; case 28: trace(28) break; case 29: trace(29) break; case 30: trace(30) break; case 31: trace(31) break; case 32: trace(32) break; case 33: trace(33) break; case 34: trace(34) break; case 35: trace(35) break; case 36: trace(36) break; case 37: trace(37) break; case 38: trace(38) break; case 39: trace(39) break; case 40: trace(40) break; case 41: trace(41) break; case 42: trace(42) break; case 43: trace(43) break; case 44: trace(44) break; case 45: trace(45) break; case 46: trace(46) break; case 47: trace(47) break; case 48: trace(48) break; case 49: trace(49) break; default: trace('default'); break; } }
Last edited by hatu; 12-02-2008 at 05:38 PM.
-
Trainee coder
OK guys, firstly, I appreciate it A LOT to get some help. I'm stressing out atm, and so it's great to know I can always rely on the games forum dudes to lend a hand. 
Back to the problem at hand; here's where it starts to get funky! So, I tested the suggestions here this morning (except for render's.. I don't have a default case 'cause I don't need one. There will always be a case as defined by a level number, as the 'next level' button won't appear unless there is another level to go to. Therefore, the 'createLevel(level#)' function won't get called unless there is a level to go to), starting with dawsonk's idea. Guess what, it worked beautifully (when jumping straight to lvl 20)! However, when I tried to run from the first to last levels, the first level didn't run the general code that should be called after the switch statement (a few traces confirmed this suspicion). Sooo, as I was doing further debugging to find where the code stopped being executed properly, I got this error message all of a sudden:
At line 9 of file "C:\Documents and Settings\Demons\Local Settings\Application Data\FlashDevelop\Tools\flashide\testmovie.jsfl":
Exception thrown in native function.
Does this mean my version of FlashDevelop is corrupt??? There's nothing that should be making this code do this. Like I said earlier, it was working fine before, but with the addition of a few more lines of code it just stops working. Is the code perhaps not being compiled properly? I don't have enough knowledge (if any, lol) of what happens 'behind the scenes' so to speak of when flash compiles the .swf, so I’m not sure if Flash Develop is responsible or what? I'm going to do some more testing in the meantime, but some advice from more experienced people would be awesome.
Btw, Hatu: Yeah, I thought a limit of just 20 cases in a switch statement was bit weak for Flash. Thanks for cancelling out my first thoughts. I've gone through the code a few times and there's nothing blatantly wrong. It seems to all point towards the way the .swf is being made. After a few minutes of testing Flash gives me that "error creating Flash movie" warning, and so only when I close down flash, flashdevelop and delete the .swf will it give me 10 or so more attempts to compile a .swf before it gives me the same error. :/
Viza.
-
Hype over content...
Just some things off the top of my head,
Is the class > 32k ? That can cause a load of problems ( Comment out your broken switch code, and just add some junk code to it, eg
var test:String="nop";
Add 20 or so lines of that, see if it still breaks even when your switch code is removed from the equation ).
Are you deleting your aso files before re-publishing ?
Squize.
PS. Sorry about the lack of reply to your mail mate, give me a couple of days and hopefully 3 projects will be out of the way in one big hit and I'll be able to catch up
-
Trainee coder
Ok, I'm sooo confused right now (not to mention stressed out!).
Squize man, thanks a lot for giving some advice. I tried your suggestion thinking that it was just my .as file being too big (it was 62kb... about 40kb is the level data / switch statement), and it worked to a degree (worked by deleting the last 17 levels making the file size<32kb, but when I put the level function in the main .fla file on a frame it gave me the same problem. This cancelled out a file size issue, and points to dodgy code in the later levels).
So, I commented out each level case/code and tested a clean .swf until I started to get errors. After each successful run, I un-commented a level case/code and repeated the process. This resulted in me discovering that it ALWAYS started to get errors when all levels were uncommented (so it looked like it was the last one that's causing the problems). Just to confirm this suspicion (that it's the code for level 20) I put case/lvl 20 code at the top of the switch list and I get 2 f*cked up results:
1. The first few levels don't work properly (variables are left undefined for some bizarre reason)
2. When I skip to the levels that do work and run through those, upon reaching level 20 it creates a completely random level (objects in random places) and the quality is set to "low"
I think that warrants a massive "WTF!!!"
So yeah, I'm thinking perhaps my version of Flash could be corrupt or something. I did get a virus detected in my Flash files earlier in the year, but assumed it was ok since I my virus protection got rid of it and I haven't had any sign of damage for 6 months. I've got another computer which I can test the .fla file on to see if it is a corrupt program, but this doesn't explain why I only have issues when I have 20 cases...
I know it’s not much help unless I show you guys the level code or a working .swf, but just the switch statement itself won’t make much sense (and is huge anyway) and the game’s almost finished so I don’t want to just hand out .fla’s. If by the end of tomorrow I still get this issue I might take out the game’s main functionality and just leave the relevant level changing code to hand out to anyone who’s got the time to help me out.
That’s enough hair pulling from me tonight, but it’d be awesome if anyone else could share their thoughts on how to fix this.
Viza.
Btw Squize, that’s no problem about the late email. I’m used to people taking a long time to reply (if at all), and with this time of the year you’ve got plenty of excuses. 
.. Btw, I found an artist for the game. He’s pretty talented and gave me a good quote. I’m telling you this ‘cause if it weren’t for you recommending me to check out the FGL.com forums I would’ve never found the dude. SO THANKS, MATE!
-
Hype over content...
"I tried your suggestion thinking that it was just my .as file being too big"
I didn't mean the .as file size mate, I meant the compiled size of the class.
In as2 ( Dunno about as3 ), you can't have a class > 32768 bytes as they've used a small value ( Either a signed word or longword, can't remember ) for the branch offsets / program counter ( Again, can't quite remember ).
Basically, if your compiled class is greater than 32k then it's going to give you problems. mtasc fires up a warning about this, I'm not sure if that still happens using the official compiler.
Squize.
-
Developer
How do you find out the size of a compiled class?
-
Trainee coder
 Originally Posted by dVyper
How do you find out the size of a compiled class?
^What he said^ 
.. Btw, I'm not sure how relevant this is but the .as file in which I'm having this problem is not a 'proper' class as such. It's just normal code (as if I was coding in a frame on the main timeline) with functions and a main game loop. I don't have fancy classes to control the main engine (apart from classes for certain objects, but that isn't relevant); the main reason why I use a separate .as file is 'cause FD sh*ts all over the Flash editor.
Viza.
-
Trainee coder
-
Funkalicious
Another reason to split up functions. Not only readability, but even compiling! Somewhat weird limit, but I geuss will need to put those 500 lines on just one line, eh?
On that 16,000 symbol instances limit, does this include the actionscripted creation of bitmapdata's?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|