Skip navigation
Currently Being Moderated

problem with arrays

Aug 26, 2012 4:24 AM

hi , how are you,i hope you all fine

 

i have problem with arrays with objects , i want to create an fireball after clicking

 

i tried to do one fireball(only fireballobject i dont wanna move it at this time , i just want it on the stage) and it is so easy with this addChild(new fireball())  but because i always get errors if i dont check the object "if (objectname.stage){}" before adding or removing it from stage i couldn,t do that ,

now iam trying now with arrays to display one object

here

 

if (event.keyCode==Keyboard.W) {
 
  fireballindex.push(new fireball())
if(fireballindex[0].stage)
addChild(fireballindex[0])
trace("if statement works")

    }

 

i got this message " if statement works" afte clicking w but the object not on the stage??!!

 

in otherwise

it works

if (event.keyCode==Keyboard.W) {

addChild(new fireball())

}

but with errors #2025 or 1009 (i dont remembe) but iam sure , the solution with if(objectname.stage)      

 

thank you

 
Replies
  • Currently Being Moderated
    Aug 26, 2012 4:30 AM   in reply to thunderxlight11111

    Show all of the code that is relevant to the problem.  Include the complete error messages as well, but before you do, go into your Flash Publish settings and select the option to Permit Debugging so that the error message might have some additional helpful info.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 26, 2012 5:02 AM   in reply to thunderxlight11111

    If there is no problem with the code and it works very well, what is the problem?

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 26, 2012 6:07 AM   in reply to thunderxlight11111

    Before you can test if the object has a non-null stage property it needs to be added to the stage.  So based on the code you just showed, the object will never get added as a child. 

     

    You will not get the 2025 error in trying to use addChild.  You can get it trying to removeChild if it is not a child.

     

    If you trace the fireballindex[0].stage before the line where you test it, you will see that it is null, and that will cause it to fail getting in to add the child...

     

        if (event.keyCode==Keyboard.W) {

     

        fireballindex.push(new fireball())

     

        trace(fireballindex[0].stage)


        if(fireballindex[0].stage)  

            addChild(fireballindex[0]) // this needs to happen first

            trace("if statement works")

        }

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 26, 2012 7:32 AM   in reply to thunderxlight11111

    var fireballindex:Array = new Array();

    fireballindex.push(new fireball())

    addChild(fireballindex[0])

     

    does work.  You can confirm this yourself by opening a new file and testing just those lines with a fireball class object in the library.  Whatever the problem is, it lies elsewhere. Maybe you don't add the object that contains the fireball?  Maybe you are adding the fireball somewhere that it cannot be seen?

     

    You need to learn how to make better use of the trace function and test things for yourself to see how they are working or not working.  Go line by line thru the expected processing with trace statements until you do not see an expected result -  solve things as they arise in that sequence.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 26, 2012 7:34 AM   in reply to thunderxlight11111

    This lines:

     

    if(fireballindex[0].stage)

         addChild(fireballindex[0])

     

    will never work because the latest object's stage property is null. Object should be already on stage in order for this conditional to return true.

     

    Code should be (note exlamation mark):

     

    if(!fireballindex[0].stage)

         addChild(fireballindex[0])

     

    Which is similar to

     

    if(fireballindex[0].stage != null)

         addChild(fireballindex[0])

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 26, 2012 8:44 AM   in reply to thunderxlight11111

    Although it is not related to the discussion directly, here is coding practices suggestion.

     

    You should use switch...case instead of if...else whenever possible. The thing is that switch...case is indexed at compilation while if...else is not. This means that if...else goes through all the conditions until it hits validation to true. switch...case goes to the appropriate case right away. This translates into a much faster performance.

     

    With that said, your HERO class should look something like the example below. Note onKeyDown case Keyboard.W that addresses fireball instantiation - if you are planning to use several fireball instances - suggested code is the way to go:

     

     

    package
    {
        import flash.display.MovieClip;
        import flash.events.KeyboardEvent;
        import flash.ui.Keyboard;
        import flash.events.Event;
        import flash.sensors.Accelerometer;
        import flash.media.Sound;
        import flash.media.SoundChannel;
        
        public class HERO extends MovieClip
        {
            private var NIGHTJUNGLE:nightjungle = new nightjungle();
            private var ELECTRICALSHIELD:electricalshield = new electricalshield();
            private var speed:int = 3;
            private var fireballindex:Array = new Array();
            private var dir:int = 0;
            private var ELECTRICITY:electricity = new electricity();
            private var SOUNDCHANNEL:SoundChannel = new SoundChannel();
            private var i:int = 0;
            
            public function HERO()
            {
                addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
            }
            
            public function onAddedToStage(event:Event):void
            {
                addEventListener(Event.ENTER_FRAME, onEnterFrame);
                stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
                stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
                SOUNDCHANNEL = NIGHTJUNGLE.play(1, 1000)
                gotoAndStop(4);
            }
            
            public function onEnterFrame(event:Event):void
            {
                x += dir * speed;
            }
            
            public function onKeyDown(event:KeyboardEvent):void
            {
                switch (event.keyCode)
                {
                    case Keyboard.RIGHT: 
                        gotoAndStop(2);
                        fireball.dir = 1;
                        dir = 1;
                        speed = 10;
                        if (ELECTRICALSHIELD.stage)
                            addChild(ELECTRICALSHIELD);
                        break;
                    
                    case Keyboard.LEFT: 
                        gotoAndStop(1);
                        fireball.dir = -1;
                        dir = -1;
                        speed = 10;
                        if (ELECTRICALSHIELD.stage)
                            addChild(ELECTRICALSHIELD);
                        break;
                    
                    case Keyboard.UP: 
                        gotoAndStop(1);
                        y = 10;
                        break;
                    
                    case Keyboard.Q: 
                        speed = 30;
                        addChild(ELECTRICALSHIELD);
                        SOUNDCHANNEL = ELECTRICITY.play();
                        break;
                    
                    case Keyboard.W: 
                        var fBall:fireball = new fireball();
                        fireballindex.push(fBall);
                        addChild(fBall);
                        break;
                }
            }
            
            public function onKeyUp(event:KeyboardEvent):void
            {
                switch (event.keyCode)
                {
                    case Keyboard.Q: 
                        if (ELECTRICALSHIELD.stage)
                        {
                            removeChild(ELECTRICALSHIELD);
                            if (SOUNDCHANNEL != null)
                                speed = 0;
                            SOUNDCHANNEL = ELECTRICITY.play(1, 1);
                        }
                        break;
                    case Keyboard.W:
                        
                        break;
                    
                    case Keyboard.LEFT: 
                        speed = 0
                        gotoAndStop(3);
                        if (ELECTRICALSHIELD.stage)
                            addChild(ELECTRICALSHIELD);
                        break;
                    
                    case Keyboard.RIGHT: 
                        speed = 0
                        gotoAndStop(4);
                        if (ELECTRICALSHIELD.stage)
                            addChild(ELECTRICALSHIELD);
                        break;
                }
            }
        }
    }
    
     
    |
    Mark as:
  • Currently Being Moderated
    Aug 26, 2012 8:52 AM   in reply to thunderxlight11111

    I think error 2025 originates in fireball class on lines:

     

    if (this.stage)

    {

         if (speed1 == -3 || speed1 == 3)

          {

             removeChild(this);

         }

    }

     

    What your code does is it attempts to remove fireball instance from itself (from its display list). Fireball IS NOT on its own display list.

     

    To remedy this you should do this:

     

    if (this.stage)

    {

         if (speed1 == -3 || speed1 == 3)

          {

              parent.removeChild(this);

         }

    }

     

    I feel the very approach of removing instance from display list from within the instance is faulty. It may introduce challenges in the parent. How does parent know that the object is removed from its display list?

     

    I believe you should rethink this approach.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points