A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Problem with Array

  1. #1
    Developer
    Join Date
    Apr 2007
    Location
    UK
    Posts
    324

    Problem with Array

    Im making a game which relies on multidimensional arrays. But Im getting a serious problem which can be reproduced with this code (in the Flash 9 preview)

    Code:
    var test:Array = new Array()
    test.push(["a", "b"])
    test.push(["c", "d"])
    trace(test[0][0])	//	a
    trace(test[1][0])	//	c
    trace(test[0][-1])	//	undefined (perfectly acceptable)
    trace(test[-1][0])	//	throws error : "TypeError: Error #1010: A term is undefined and has no properties."
    How can i get around this error? It's annoying because I actually want it to return undefined!

  2. #2
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,358
    check for valid objects before referencing their properties or use try-catch

    http://www.kirupa.com/forum/showthre...71#post2098271

  3. #3
    Member
    Join Date
    Mar 2007
    Posts
    33
    test[0][-1] is ok because test[0] exists, though test[0][-1] is undefined.

    test[-1][0] is an error because test[-1] is undefined and you're in effect trying to access a property([0]) of an object that does'n exist. In AS3 you have to do bounds checking to avoid these errors.

  4. #4
    Developer
    Join Date
    Apr 2007
    Location
    UK
    Posts
    324
    Damn Adobe! I understand now I'll need to change my code a bit to avoid the error which will probably add a few milliseconds to each frame. Grumble!

  5. #5
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,358
    an if clause takes far less to complete than even a single millisecond. In fact, lets see if it can be timed. Working off of the following code:
    Code:
    var notDefined:Object;
    var timer:Number = getTimer();
    var count:Number = 0;
    while(true){
    	if (notDefined){}
    	count++;
    	if (getTimer() - timer >= 1000) break;
    }
    trace(count);
    ran 10 times, throwing out the highest and lowest, I get an average of 5719602 iterations for a second. Now, thats not considering the time taken for the count++ and timer code. Running that on its own (the above code without the if statement) got me an average of 6273903 iterations. That equates to ... after carrying the 1... and a 4, 3 - niner ... about, well we'll round it to roughly 75000000 - that's 75 million if conditions that can be evaluated every second, or 75 thousand every millisecond.

    I don't think you have anything to worry about.

  6. #6
    Developer
    Join Date
    Apr 2007
    Location
    UK
    Posts
    324
    Indeed right you are! I tested your code in Flash 8 and 9 and executing that code showed me that it could execute almost 10 times the amount of 'if' statements in Flash 9 compared to 8. Im quite impressed.

    Thanks for your time

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center