A Flash Developer Resource Site

Results 1 to 16 of 16

Thread: [Help Please] addChild();

  1. #1
    Member
    Join Date
    Aug 2007
    Posts
    53

    [Help Please] addChild();

    The code below generates an error any idea?
    thanks

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at pang/checkForheroHits()


    PHP Code:
    public function checkForheroHits(event:Event) {
                
                    for (var 
    ballNum:int=balls.length-1;ballNum>=0;ballNum--) {
                        if (
    myhero.hitTestObject(balls[ballNum])) {
                            
    //myhero.heroHit();
                            //protectedhero();
                            //balls[ballNum].width=50;
                            
                            //removeball(balls[ballNum]);
                            //ball = new Ball()
                            
    var hisnumint=ballNum+4;
                            
                            
    ball[hisnum]=new Ball();
                            
    stage.addChild(ball[hisnum]);
                            
                        }
      }



  2. #2
    Member
    Join Date
    Oct 2009
    Location
    Ontario
    Posts
    98
    Is the checkForheroHits method sitting in an ActionScript class?

    You need to provide it with a reference to the Stage, either by having this class be the main Document Class, or by passing a reference to the Stage in the constructor.

    If you don't have a "base" class, I would just cheat a little and send a reference to the stage.

    From the main timeline:

    Code:
    var myClass = new MyClass(this.stage);
    this.addChild(myClass);
    And then from your custom class:

    Code:
    import flash.display.Stage;
    var stageReference:Stage;
    public function MyClass(stageReference:Stage) {
         this.stageReference = stageReference;
    }
    ...
    public function checkForheroHits(event:Event) {
         ...
         this.stageReference.addChild(ball[hisnum]);
         ...
    }
    Give that a try. =)
    Last edited by Nidht; 10-15-2009 at 05:49 PM.

  3. #3
    Member
    Join Date
    Aug 2007
    Posts
    53
    Thank you for your reply

    It is part of the main base class

    and I have a class called ball

  4. #4
    Member
    Join Date
    Oct 2009
    Location
    Ontario
    Posts
    98
    I've been at the office too long. It's not the stage reference that Flash is complaining about, it's the reference to "balls".

    Can you show me where this is being declared above the constructor?

  5. #5
    Member
    Join Date
    Aug 2007
    Posts
    53
    yes sir:
    PHP Code:
    public class mygame extends MovieClip {
    private var 
    balls:Array;

    private var 
    ball1:Ball;
            private var 
    ball2:Ball;
            private var 
    ball3:Ball;
            private var 
    ball4:Ball;
            private var 
    ball5:Ball;
            private var 
    ball6:Ball;
            private var 
    ball:Ball;




    public function 
    startmygame() {

    balls = new Array();
    balls=[ball1,ball2];


  6. #6
    Member
    Join Date
    Aug 2007
    Posts
    53
    I cahanged the code a little bit:

    ReferenceError: Error #1069: Property 4 not found on Ball and there is no default value.
    at pang/checkForheroHits()



    PHP Code:
    public function checkForheroHits(event:Event) {
                
                    for (
    ballNum=balls.length-1;ballNum>=0;ballNum--) {
                        if (
    myhero.hitTestObject(balls[ballNum])) {
                            
    //myhero.heroHit();
                            
    protectedhero();
                            
    //balls[ballNum].width=50;
                            
                            //removeball(balls[ballNum]);
                            
    ball = new Ball()
                            var 
    hisnumint=ballNum+4;
                            
                            
    //ball[hisnum]=new Ball();
                            //stage.addChild(ball[hisnum]);
                            
                            
    stage.addChild(ball[hisnum]);
                            
    ball[hisnum].x=150;
                            
    ball[hisnum].y=150;
    }
    }


  7. #7
    Member
    Join Date
    Oct 2009
    Location
    Ontario
    Posts
    98
    It looks like it's looking for the 4th parameter for your Ball constructor. Either supply all 4, or give the 4th one a default value. =)

  8. #8
    Member
    Join Date
    Aug 2007
    Posts
    53
    the Ball constructor has no parameter

    what i am doing is a calculation

    PHP Code:
    if ball0 was hit add ball0+=ball4
    if ball1 was hit add ball1+4=ball5
    thanks

  9. #9
    Member
    Join Date
    Oct 2009
    Location
    Ontario
    Posts
    98
    Are you still receiving this error message? Have you made any progress on this?

  10. #10
    Member
    Join Date
    Aug 2007
    Posts
    53
    Thank you


    I am still receiving error message: I am almost the point of quiting learning AS3

    I even made it very simple:



    PHP Code:
    public function checkForheroHits(event:Event) {
                
                    for (
    ballNum=balls.length-1;ballNum>=0;ballNum--) {
                        if(
    balls[ballNum] !=null){
                        if (
    myhero.hitTestObject(balls[ballNum])) {
                            if(
    balls[ballNum].parent!=null){
                            
    hasshield=true;
                            
    trace(balls[ballNum].name);
                            
    balls[ballNum].deleteballx();
                            
                            
    removeEventListener(Event.ENTER_FRAME,checkForheroHits);
                            
                            }
                            
                        }
                            
                            
                        
                    }
                    
                }
                
                
    addEventListener(Event.ENTER_FRAME,protecthero);
                } 

    my balls array :

    at the top I have my

    var balls: Array;
    balls = new Array();

    balls=[ball1,ball2,ball3,ball4,ball5,ball6];

    it seems that if I do not have all the balls added at the bigining of the game

    ie

    no intialized they will be ignored even if i initialize them latter

    PHP Code:
    public function startgame() {

    ball1 = new Ball();
                
    ball1.name="ball1";
                
    addChild(ball1);
                
    //balls.push(ball1);

                //balls.push(ball1);
                
    ball1.50;
                
    ball1.350;
                
    ball1.gotoAndStop(1);
                
                
    ball2 = new Ball();
                
    //balls.push(ball2);
                
    ball2.name="ball2";
                
    addChild(ball2);
                
    ball2.150;
                
    ball2.50;
                
    ball2.gotoAndStop(2);
                } 
    if i latter add ball3 ,ball5, they get ignored:



    PHP Code:
    public function checkForharpoonHits(event:Event) {
                
                
                    if(
    weapon!=null){
                
                        if (
    weapon.hitTestObject(ball1) ) {
                            
    trace("was hit");
                        
                            
                            
    addChild(ball5);
                            
    ball5.x=ball1.x+50;
                            
    ball5.y=ball1.y+50;
                            
                                
    weapon.deleteharpoon();
                            
                            
    removeEventListener(Event.ENTER_FRAME,checkForharpoonHits);
                              }
                              
                            }
                            
                            
                            
            }
                        
                    
                
                } 
    I attached all the files of the games-thanks
    Attached Files Attached Files
    Last edited by sami_rima; 10-16-2009 at 10:15 PM.

  11. #11
    Member
    Join Date
    Oct 2009
    Location
    Ontario
    Posts
    98
    Don't quit just yet. You're learning really quick and your game is coming along well!

    The issue is that you're sometimes adding balls that don't exist to the Stage in the checkForharpoonhits function:

    Code:
    public function checkForharpoonHits(event:Event) {
    			if (weapon != null) {
    				if (weapon.hitTestObject(ball1)) {
    					trace("was hit");
    					addChild(ball5);
    ...
    In your startPang function, you never initialize the the ball5 object, which is why you get an error when you try to add it.

    Instead of manually instantiating multiple Ball objects, why not run a loop to declare several at once?

    Code:
    var numberOfBalls = 5;
    
    // create balls and add them to the stage
    balls = new Array();
    for (var i=0; i<numberOfBalls; i++) {
    	ball = new Ball();
    	ball.name = "ball" + (i + 1);
    	ball.x = (i + 1) * 50 + Math.random() * 100;
    	ball.y = (i + 1) * 25 + Math.random() * 100;
    	
    	if (Math.random() * 100 > 50)
    		ball.gotoAndStop(2);
    	
    	balls.push(ball);
    	
    	this.addChild(ball);
    }
    This code replaces the chunk of code in startPang that manually instantiates those balls. You can change how many Ball objects are instantiated with the numberOfBalls variable, which is a temporary way to do things. Later on, you should make the Ball objects spawn more dynamically, which I'm sure you were planning.

    You'll see that I randomly shift the ball's position around using the current index of the loop, an arbitrary number (50 and 25 for x and y, respectively), and some random math. You can change how this works and tweak the values to your liking.

    I also run a check so that 50% of the time, the ball becomes a shield when instantiated.

    All of these Ball objects sit in the balls array for later use.

    This is the new checkForharpoonHits:

    Code:
    // check for collisions
    public function checkForharpoonHits(event:Event) {
    	if (weapon != null) {
    		for (var i=0; i<balls.length; i++) {
    			if (weapon.hitTestObject(balls[i])) {
    				if (balls[i] != null)
    					balls[i].deleteballx();
    			}
    		}
    	}
    }
    You'll notice that I loop through the balls array and test each Ball instance individually. This is not the best way to do this, because it can become quite CPU-intensive as this loop will run EVERY TIME A FRAME IS HIT. A better solution should be researched for later on, but for now, it works. And the best part is you don't have to do any of the hit tests manually. It loops through all the Ball objects and checks for you (whereas before you were only checking for ball1).

    I'm getting error messages when the balls are deleted. I think it's complaining about the parent.removeChild call in the deleteballx function, but I didn't look too much into it.

    This should put you on the right track, hopefully. =)

  12. #12
    Member
    Join Date
    Aug 2007
    Posts
    53
    Thank you sir

    I will try .

  13. #13
    Member
    Join Date
    Aug 2007
    Posts
    53
    Thank you:

    now the array works to add balls automatically -through the function

    addball();

    and my only issue is removing the balls that were hit


    PHP Code:
    public function checkForheroHits(event:Event) {
        
            for (var 
    ballNum:int=0;ballNum<balls.length;ballNum++){
                if( 
    balls[ballNum]!=null){
      if (
    myhero.hitTestObject(balls[ballNum])){
          
          
         
    balls[ballNum].ballHit();  
        
    //removeChild( balls[ballNum]);
    //balls[ballNum].deleteballx();
    //MovieClip(balls[ballNum].parent).removeChild(balls[ballNum])

    //removeChild(getChildByName("ball"+ballNum));
    //balls[ballNum].parent.removeChild (balls[ballNum]);



    removeEventListener(Event.ENTER_FRAME,checkForheroHits);
     }
    //stage.removeChild( balls[ballNum]);
    //

     // i--; //adjust i down since we altered balls array.  want to revisit that index.
        
      
    }
     
    }
            
            
            } 

    in the main I have:


    PHP Code:
    // take a ball from the array
            
    public function removeball(ball:Ball) {
                for(var 
    k in balls) {
                    if (
    balls[k] == ball) {
                        
    balls.splice(k,1);
                        
    //break;
                    
    }
                }
            } 

    in the Ball class I have:

    PHP Code:
    // ball hit, show remove it
            
    public function ballHit() {
                
    removeEventListener(Event.ENTER_FRAME,moveball);
                
    MovieClip(parent).removeball(this);
                
            } 
    and Also I have

    PHP Code:
    // remove from screen and remove events
            
    public function deleteballx() {
                
    parent.removeChild(this);
                
    removeEventListener(Event.ENTER_FRAME,moveball);
                
            } 
    if I use :

    PHP Code:
    balls[ballNum].deleteballx(); 
    I get :Error #1009: Cannot access a property or method of a null object reference.
    at Ball/deleteballx()

    if I use:

    PHP Code:
    balls[ballNum].ballHit(); 
    I get:

    Error #1034: Type Coercion failed: cannot convert flash.display::Stage@353df99 to flash.display.MovieClip.
    at Ball/ballHit()

    if I use:

  14. #14
    Flash Intermediate XenElement's Avatar
    Join Date
    Sep 2008
    Location
    At my computer
    Posts
    196
    There's a list of things to try every time you get Error #1009: Cannot access a property or method of a null object reference.

    I believe that the error above, is basically flash saying Ahhhh! I can't delete that! It doesn't exist!

    Well, usually flash can have something, you're just not telling it to look in the right place. If you get that error, always try stage.removeChild(myMovie). Using the stage property sometimes works, for whatever reason. also, you could try whatever movieclip the balls are in, or to make things even easier, make a holder that holds all of the balls.

    Hope that helps a bit,
    XenElement
    In the process of designing a quirky little game engine called gulp. Check out it's progress below: @ my blog.

    ---

    Check out my blog at XenElement.com

  15. #15
    Member
    Join Date
    Aug 2007
    Posts
    53
    I've tried that without luck same error message

    thanks

  16. #16
    Member
    Join Date
    Oct 2009
    Location
    Ontario
    Posts
    98
    sami_rima, check out this post by the great senocular on accessing the Stage element:
    http://kirupa.com/forum/showthread.php?p=1952513

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