Maybe you guys could help me out, I have a problem with my AS3 code which probably requires to have a look at my files. I have posted my problem elsewhere before but I couldn’t get help in the sense that it solved the problem.
So, I’m programming a jumpnrun game and it happens (not every time though!) that I get null-object reference errors when my player character should normally be added to stage at the beginning of the game (level 1). Flash debugger tells me that the problem is the mcCollbox movieclip (obviously a rectangle that I use for collision detection and handling) which I have put in every labeled animation frame of my “avatar” class–aside the regular character animations. As I said, sometimes it works perfectly fine and there are no errors at all… Thats why I don’t understand how this mc can on the other hand be nonexistent. Not complete loading processes can’t be the problem I think because I’m using a loader. I also tried to get the offending parts under control using if-statements but that didn’t do much. I’m starting to think that this is no coding problem but some weird flash glitch.
Two observations I made:
- When I trace out all the frame labels of Avatar’s timeline, the first one (labeled “idle”) exists two times for some reason and one has 0 children (should be 3 or something) (maybe the program tries to go to a frame that does not exist and therefore has no collision box and thats causing the errors?)
- When I only have the collbox in the animation frames and there are no actual “animations” on the character the code seems to work almost without error --> Every time I change the visibility value of mcCollbox in the code and execute I still get errors. But beginning with the second execution they don’t come back.
As soon as the animation MCs are back in the game all goes bad again… But I don’t think there’s anything wrong with these animations because they don’t have any code references (instance names etc) in them–they are just regular “movie”-clips–and after all the program does work sometimes.
The error message is always the same. It’s a #1009.
To me this whole code conflict doesn’t make sense. But I’m actually not an expert when it comes to programming so… I really hope someone has a clue whats going on here. Should I re-do my Avatar.fla from scratch??
I uploaded my files on MS OneDrive if you want to take a look:
http://1drv.ms/1dfCmJX
public function Avatar():void
{[INDENT]this.gotoAndStop(0);
var i:int;
var obj:DisplayObject;
for ( i = 0; i < this.numChildren; i++ )
{[/INDENT]
[INDENT=2] obj = this.getChildAt(i);
if ( obj is AvatarCollbox )
{
[/INDENT]
[INDENT=3] obj.visible = false; //changing this under the aforementioned circumstances and only once results in a failed execution
[/INDENT]
[INDENT=2] }[/INDENT]
[INDENT]}
[/INDENT]
[INDENT]this.gotoAndStop( "avatarIdle" );
addEventListener( Event.ENTER_FRAME, eFrame );
[/INDENT]
}
function eFrame( e:Event ):void
{[INDENT]updatePosition();
[/INDENT]
}
function updatePosition():void
{[INDENT] var mcCollbox:DisplayObject;
for ( var i:int = 0; i < this.numChildren; i++ )
{[/INDENT]
[INDENT=2] var obj:DisplayObject = this.getChildAt( i );
if ( obj is AvatarCollbox )
{
[/INDENT]
[INDENT=3] mcCollbox = obj;
break;
[/INDENT]
[INDENT=2] }[/INDENT]
[INDENT] }
[/INDENT]
[INDENT]var collisionTolerance:int = 5;
// the following line causes the error: mcCollbox is said to be null (which can't be since
// I put the collbox-mc in EVERY frame of Avatar.swf's timeline...)
var rectBoundsAvatar:Rectangle = mcCollbox.getBounds( mcCollbox );
var iAvatarOldLeft:int = iOldPositionX + rectBoundsAvatar.left + collisionTolerance;
var iAvatarOldRight:int = iOldPositionX + rectBoundsAvatar.right - collisionTolerance;
var iAvatarOldTop:int = iOldPositionY + rectBoundsAvatar.top + collisionTolerance;
var iAvatarOldBottom:int = iOldPositionY + rectBoundsAvatar.bottom - collisionTolerance;
var iAvatarNewLeft:int = iNewPositionX + rectBoundsAvatar.left;
var iAvatarNewRight:int = iNewPositionX + rectBoundsAvatar.right;
var iAvatarNewTop:int = iNewPositionY + rectBoundsAvatar.top;
var iAvatarNewBottom:int = iNewPositionY + rectBoundsAvatar.bottom;
// ...[/INDENT]
}