29 Replies Latest reply on Jan 25, 2012 11:15 PM by kglad

    Creating images error.

    McbainGames11 Level 2

      Hey,

       

      I am having problem creating my ice_block.png photo appear on the screen + be munipulated. Well I can get it to come up on the screen but when I try do things like change in positions of x+y, put in array it just shows #1007 or #1009 error!

       

       

      Level Class:

       

      package

      {

                import com.greensock.*;

                import com.greensock.easing.*;

                import com.greensock.plugins.*;

       

                import flash.display.Bitmap;

                import flash.display.DisplayObject;

                import flash.display.MovieClip;

                import flash.display.Sprite;

                import flash.events.Event;

                import flash.filters.DisplacementMapFilter;

       

                TweenPlugin.activate([AutoAlphaPlugin]);

       

       

                public class Level extends Sprite

                {

                          public var enemyA:Array = [];

                          public var birdA:Array = [];

       

                          public var birdImageHolder:Sprite = new Sprite();

       

       

                          public function Level()

                          {

                                    createBird();

                                    createEnemy();

                                    this.addEventListener(Event.ENTER_FRAME, loopF);

                          }

                          public function createBird():void

                          {

                                    //Creating the bird..

                                    addChild(birdImageHolder);

                                    birdImageHolder.y = 330;

       

                                    var birdImage:Bitmap = new Assets.bird();

                                    birdImageHolder.addChild(birdImage);

                                    birdImage.y = -37;

                                    birdImage.x = -40;

       

                                    //TweenLite functions..

                                    birdImage.alpha = 0;

                                    TweenLite.to(birdImage, 4, { alpha:1, visible:1, ease:Expo.easeOut });

                          }

                           public function createEnemy():void

                          {

                                    var enemyImageHolder:MovieClip = new MovieClip();

                                    var max_enemies:int = 10;

                                    var gap:int = 100;

       

                                    for(var i:int=1; i<max_enemies; i++)

                                    {

                                              addChild(enemyImageHolder);

       

                                              var enemy:Bitmap = new Assets.iceBlock();

                                              enemyImageHolder.addChild(enemy);

                                              enemy.x = 200;

                                              enemy.y =  -30.65;

                                              enemyA.push(enemy);

                                              enemy.name = "enemyA";

                                    }

                          }

                          public function updateEnemyPosition():void

                          {

                                    var fall_limit:int = 2;

       

                                    for(var i:int; i<enemyA.length; i++)

                                    {

                                              var enemy:DisplayObject = new enemyA[i] as DisplayObject;

                                              enemy.y += fall_limit;

                                    }

                          }

                          public function updateBirdPosition():void

                          {

                                    birdImageHolder.x = mouseX;

                          }

                          public function loopF(event:Event):void

                          {

                                    updateBirdPosition();

                                    updateEnemyPosition();

                          }

                }

      }

       

       

       

      Assets Class(Basically just my library, this doesn't matter just want to show everything I have done!):

       

      package

      {

                public class Assets

                {

      //Project library..

                          [Embed(source="../images/ice_block.png")]

                          public static var iceBlock:Class

                }

      }

       

      I have tried alot of combinations to try fix this, But I guess I have missed something. So I'm here to ask the pros

      All help appreciated, Thankyou!

        • 1. Re: Creating images error.
          kglad Adobe Community Professional & MVP

          copy and paste the error and highlight the line of code mentioned in the error message after ticking "permit debugging".

          1 person found this helpful
          • 2. Re: Creating images error.
            McbainGames11 Level 2

            TypeError: Error #1007: Instantiation attempted on a non-constructor.

                      at Level/updateEnemyPosition()[C:\Users\Brendon Battye\Adobe Flash Builder 4.5\Ice Mountain\src\Level.as:68]

                      at Level/loopF()[C:\Users\Brendon Battye\Adobe Flash Builder 4.5\Ice Mountain\src\Level.as:79]

             

            Line 68 -> var enemy:DisplayObject = new enemyA[i] as DisplayObject;

            Line 79 -> updateEnemyPosition();

             

            Don't know what "Permit Debugging" is in Flash Builder, but this was just a normal debug.

            • 3. Re: Creating images error.
              kglad Adobe Community Professional & MVP

              use:

               

              var enemy:DisplayObject = enemyA[i] as DisplayObject;

              1 person found this helpful
              • 4. Re: Creating images error.
                McbainGames11 Level 2

                That fixed most of problem

                Any reason why this code I just added would show an error? I added it in the for loop in createEnemy();

                 

                enemy.x = Math.random() * stage.stageWidth - enemy.width;

                 

                TypeError: Error #1009: Cannot access a property or method of a null object reference.

                          at Level/createEnemy()[C:\Users\Brendon Battye\Adobe Flash Builder 4.5\Ice Mountain\src\Level.as:56]

                          at Level()[C:\Users\Brendon Battye\Adobe Flash Builder 4.5\Ice Mountain\src\Level.as:26]

                          at Main/playGame()[C:\Users\Brendon Battye\Adobe Flash Builder 4.5\Ice Mountain\src\Main.as:140]

                 

                Line 26 -> createEnemy();

                Line 56 -> enemy.x = Math.random() * stage.stageWidth - enemy.width;

                • 5. Re: Creating images error.
                  kglad Adobe Community Professional & MVP

                  enemy is not defined in line 56.

                  1 person found this helpful
                  • 6. Re: Creating images error.
                    McbainGames11 Level 2

                    Line 56 -> enemy.x = Math.random() * stage.stageWidth - enemy.width;

                     

                    How can I define it, I thought var enemy:Bitmap = new Assets.iceBlock(); does that.

                    • 7. Re: Creating images error.
                      kglad Adobe Community Professional & MVP

                      i don't see that line 56 in your code where is it?

                      • 8. Re: Creating images error.
                        McbainGames11 Level 2

                        I just added it which is arising another problem. I originally removed those lines so I could fix the other error so there isn't 2

                        • 9. Re: Creating images error.
                          kglad Adobe Community Professional & MVP

                          if you want help with this latest problem, copy and paste the code the contains the problematic code and bold the problematic line.

                          • 10. Re: Creating images error.
                            McbainGames11 Level 2

                                                public function createEnemy():void

                                                {

                                                          var enemyImageHolder:MovieClip = new MovieClip();

                                                          var max_enemies:int = 10;

                                                          var gap:int = 100;

                             

                                                          for(var i:int=1; i<max_enemies; i++)

                                                          {

                                                                    addChild(enemyImageHolder);

                             

                                                                    var enemy:Bitmap = new Assets.iceBlock();

                                                                    enemyImageHolder.addChild(enemy);

                            enemy.x = Math.random() * stage.stageWidth - enemy.width;

                                                                    enemy.y =  i * (gap + enemy.height) - 30.65;

                                                                    enemyA.push(enemy);

                                                                    enemy.name = "enemyA";

                                                          }

                                                }

                            • 11. Re: Creating images error.
                              kglad Adobe Community Professional & MVP

                              if the following trace is "null", you're not returning anything when using new Assets.iceBlock().

                               

                              as a matter of fact, didn't you make the same error earlier today when you were supposed to be using:

                               

                              var enemy:Bitmap=Assets.iceBlock();

                               

                               

                                 public function createEnemy():void

                                                  {

                                                            var enemyImageHolder:MovieClip = new MovieClip();

                                                            var max_enemies:int = 10;

                                                            var gap:int = 100;

                               

                                                            for(var i:int=1; i<max_enemies; i++)

                                                            {

                                                                      addChild(enemyImageHolder);

                               

                                                                      var enemy:Bitmap = new Assets.iceBlock();

                              trace(enemy);  // when you see "null" for this trace use:  var enemy:Bitmap=Assets.iceBlock();

                                                                      enemyImageHolder.addChild(enemy);

                              enemy.x = Math.random() * stage.stageWidth - enemy.width;

                                                                      enemy.y =  i * (gap + enemy.height) - 30.65;

                                                                      enemyA.push(enemy);

                                                                      enemy.name = "enemyA";

                                                            }

                                                  }

                              • 12. Re: Creating images error.
                                McbainGames11 Level 2

                                I tried all possibilities I could find, I tried changing to not be in sprite etc but it just doesn't like * stage.stageWidth - enemy.width;.

                                 

                                If I do:

                                enemy.x = Math.random();

                                 

                                It works.. but it isn't the code i want... * stage.stageWidth - enemy.width; is not being aloud for some reason

                                And my method with creating the object works fine in my Main Class so that should be ok.

                                 

                                Still can't find any problems!

                                 

                                It traced: Assets_iceBlock6

                                • 13. Re: Creating images error.
                                  kglad Adobe Community Professional & MVP

                                  if that trace wasn't null, use:

                                   

                                  enemyImageHolder.stage.stageWidth

                                   

                                  instead of

                                   

                                  stage.stageWidth

                                  1 person found this helpful
                                  • 14. Re: Creating images error.
                                    McbainGames11 Level 2

                                    Same thing happened #1009

                                    • 15. Re: Creating images error.
                                      kglad Adobe Community Professional & MVP

                                      trace?

                                      • 16. Re: Creating images error.
                                        McbainGames11 Level 2

                                        Assets_iceBlock6

                                        • 17. Re: Creating images error.
                                          kglad Adobe Community Professional & MVP

                                          replace your createEnemy() with the following and copy and paste the traces.

                                           

                                           

                                             public function createEnemy():void

                                                              {

                                                                        var enemyImageHolder:MovieClip = new MovieClip();

                                          addChild(enemyImageHolder);

                                          trace(this.stage);

                                                                        var max_enemies:int = 10;

                                                                        var gap:int = 100;

                                           

                                                                        for(var i:int=1; i<max_enemies; i++)

                                                                        {

                                                                             

                                           

                                                                                  var enemy:Bitmap = Assets.iceBlock();

                                                                                  enemyImageHolder.addChild(enemy);

                                          trace(enemy);

                                          trace(enemy.x);

                                          trace(enemy.width);

                                          trace(stage);

                                          trace(enemyImageHolder.stage)

                                          enemy.x = Math.random() * stage.stageWidth - enemy.width;

                                                                                  enemy.y =  i * (gap + enemy.height) - 30.65;

                                                                                  enemyA.push(enemy);

                                                                                  enemy.name = "enemyA";

                                                                        }

                                                              }

                                          • 18. Re: Creating images error.
                                            McbainGames11 Level 2

                                            null

                                            ArgumentError: Error #1112: Argument count mismatch on class coercion.  Expected 1, got 0.

                                                      at Level/createEnemy()[C:\Users\Brendon Battye\Adobe Flash Builder 4.5\Ice Mountain\src\Level.as:55]

                                                      at Level()[C:\Users\Brendon Battye\Adobe Flash Builder 4.5\Ice Mountain\src\Level.as:26]

                                                      at Main/playGame()[C:\Users\Brendon Battye\Adobe Flash Builder 4.5\Ice Mountain\src\Main.as:140]

                                            • 19. Re: Creating images error.
                                              kglad Adobe Community Professional & MVP

                                              your class can't get a reference to the stage.  you need to pass to your class something that's in the display list.

                                              • 20. Re: Creating images error.
                                                McbainGames11 Level 2

                                                How would I begin to do that, don't fully understand.

                                                • 21. Re: Creating images error.
                                                  kglad Adobe Community Professional & MVP

                                                  you need to pass a displaylist object to your class or use a class with a static variable to pass a reference to the stage.

                                                  • 22. Re: Creating images error.
                                                    McbainGames11 Level 2

                                                    So create a function like: public function createObjectEnemy():Sprite

                                                    and then pass it through as enemy = createObjectEnemy(); and do the things I want to it in the  for loop?

                                                    • 23. Re: Creating images error.
                                                      kglad Adobe Community Professional & MVP

                                                      no.

                                                       

                                                      from somewhere you're using something like:

                                                       

                                                      var level:Level=new Level();

                                                       

                                                      correct?  if yes, and the following trace() reveal the stage:

                                                       

                                                      trace(this.stage);

                                                      var level:Level=new Level();

                                                       

                                                      use:

                                                       

                                                      var level:Level=new Level(this.stage); // and change your Level constructor to accept this stage reference.

                                                      • 24. Re: Creating images error.
                                                        McbainGames11 Level 2

                                                        Still the same thing happening with these changes.. I must have done it wrong.

                                                         

                                                        Trace Output: [object Stage]

                                                        Level constructor name: public function Level(stage:Stage)

                                                        Error: #1009 - Null

                                                        • 25. Re: Creating images error.
                                                          kglad Adobe Community Professional & MVP

                                                          stage is a reserved word.  don't use it for a parameter name:

                                                           

                                                          public function Level(stageVar:Stage){

                                                          trace(stageVar);

                                                          .

                                                          .

                                                          }

                                                          • 26. Re: Creating images error.
                                                            McbainGames11 Level 2

                                                            Output: [object Stage]

                                                             

                                                            Now do I use that variable in the function to reference stage in the other function? Like: stageVar.stageWidth

                                                            • 27. Re: Creating images error.
                                                              kglad Adobe Community Professional & MVP

                                                              yes.

                                                               

                                                              but you'll need to make that parameter available outside your construtor in Level:

                                                               

                                                               

                                                              var _stage:Stage; // declare your variable

                                                               

                                                              public function Level(stageVar:Stage){ // in your constructory define _stage

                                                              _stage=stageVar;

                                                              • 28. Re: Creating images error.
                                                                McbainGames11 Level 2

                                                                Your very helpful, I normally work with Main Class so never encountered this until now

                                                                Thankyou!

                                                                • 29. Re: Creating images error.
                                                                  kglad Adobe Community Professional & MVP

                                                                  you're welcome.