Skip navigation
Currently Being Moderated

error #1069

Aug 28, 2012 5:41 AM

hi

 

i get this error after doing some change in the hero class , i mean after this command

 

MovieClip(parent) only!.

 

the old code (works very well)

 

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 {
var NIGHTJUNGLE:nightjungle = new nightjungle
        var ELECTRICALSHIELD:electricalshield=new electricalshield  ;
var fBall:fireball = new fireball();

var FIREBALLSOUND:fireballsound = new fireballsound
        var speed:int=3;
var fireballindex:Array = new Array
        var dir:int;
  var ELECTRICITY:electricity = new electricity;
  var SOUNDCHANNEL:SoundChannel = new SoundChannel
  var firespeed:int = 10
  var totalfirespeed = 0
var i:int
        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
   if (fBall.stage){
   fBall.x += firespeed * dir;
   fBall.width +=2
   fBall.height += 2
   totalfirespeed +=1
  
   }
   if (totalfirespeed == 10){
    removeChild(fBall)
    totalfirespeed = 0
   
   
   }
  
        }

        public function onKeyDown(event:KeyboardEvent):void {

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

                gotoAndStop(2);

                dir=1;
    speed = 10
    if (ELECTRICALSHIELD.stage)
    addChild(ELECTRICALSHIELD)
   

            }


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

                gotoAndStop(1);


                dir=-1;
    speed  = 10
    if (ELECTRICALSHIELD.stage)
    addChild(ELECTRICALSHIELD)

            }

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

                gotoAndStop(1);

                y=10;

            }

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

                speed=30;

                addChild(ELECTRICALSHIELD);
    SOUNDCHANNEL = ELECTRICITY.play()
   
            }
if (event.keyCode == Keyboard.W) {

SOUNDCHANNEL = FIREBALLSOUND.play()
    fBall.name = "fb"+String(i);

fBall.width = 50;
fBall.x



fireballindex.push(fBall);


addChild(fBall);
SOUNDCHANNEL = FIREBALLSOUND.play()

i++;
trace(i)
trace( fBall.name)

   }
  if (event.keyCode == Keyboard.E) {
MovieClip(parent).aa.x = x +100
  }
}

        public function onKeyUp(event:KeyboardEvent):void {

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

                if (ELECTRICALSHIELD.stage) {

                    removeChild(ELECTRICALSHIELD);
if (SOUNDCHANNEL != null)
                    speed=0;
     SOUNDCHANNEL = ELECTRICITY.play(1,1)

                }

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


      }
     

            if (event.keyCode==Keyboard.LEFT) {
speed = 0
      gotoAndStop(3);
      if (ELECTRICALSHIELD.stage)
    addChild(ELECTRICALSHIELD)

            } else if (event.keyCode == Keyboard.RIGHT) {
speed =0
                gotoAndStop(4);
if (ELECTRICALSHIELD.stage)
    addChild(ELECTRICALSHIELD)
            }

        }

    }

}

 

 

 

 

 

new code (which is the one who has the error message)

 

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 {
var NIGHTJUNGLE:nightjungle = new nightjungle
        var ELECTRICALSHIELD:electricalshield=new electricalshield  ;
var fBall:fireball = new fireball();

var FIREBALLSOUND:fireballsound = new fireballsound
        var speed:int=3;
var fireballindex:Array = new Array
        var dir:int;
  var ELECTRICITY:electricity = new electricity;
  var SOUNDCHANNEL:SoundChannel = new SoundChannel
  var firespeed:int = 10
  var totalfirespeed = 0
var i:int
        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
   if (MovieClip(parent).fBall.stage){
   MovieClip(parent).fBall.x += firespeed * dir;
   MovieClip(parent).fBall.width +=2
   MovieClip(parent).fBall.height += 2
   totalfirespeed +=1
  
   }
   if (totalfirespeed == 10){
    MovieClip(parent).removeChild(fBall)
    totalfirespeed = 0
   
   
   }
  
        }

        public function onKeyDown(event:KeyboardEvent):void {

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

                gotoAndStop(2);

                dir=1;
    speed = 10
    if (ELECTRICALSHIELD.stage)
    addChild(ELECTRICALSHIELD)
   

            }


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

                gotoAndStop(1);


                dir=-1;
    speed  = 10
    if (ELECTRICALSHIELD.stage)
    addChild(ELECTRICALSHIELD)

            }

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

                gotoAndStop(1);

                y=10;

            }

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

                speed=30;

                addChild(ELECTRICALSHIELD);
    SOUNDCHANNEL = ELECTRICITY.play()
   
            }
if (event.keyCode == Keyboard.W) {

SOUNDCHANNEL = FIREBALLSOUND.play()
    MovieClip(parent).fBall.name = "fb"+String(i);

MovieClip(parent).fBall.width = 20;
MovieClip(parent).fBall.x =400
MovieClip(parent).fBall.y = 400



fireballindex.push(MovieClip(parent).fBall);


MovieClip(parent).addChild(fBall);
SOUNDCHANNEL = FIREBALLSOUND.play()

i++;
trace(i)
trace( MovieClip(parent).fBall.name)

   }
  if (event.keyCode == Keyboard.E) {
MovieClip(parent).aa.x = x +100
  }
}

        public function onKeyUp(event:KeyboardEvent):void {

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

                if (ELECTRICALSHIELD.stage) {

                    removeChild(ELECTRICALSHIELD);
if (SOUNDCHANNEL != null)
                    speed=0;
     SOUNDCHANNEL = ELECTRICITY.play(1,1)

                }

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


      }
     

            if (event.keyCode==Keyboard.LEFT) {
speed = 0
      gotoAndStop(3);
      if (ELECTRICALSHIELD.stage)
    addChild(ELECTRICALSHIELD)

            } else if (event.keyCode == Keyboard.RIGHT) {
speed =0
                gotoAndStop(4);
if (ELECTRICALSHIELD.stage)
    addChild(ELECTRICALSHIELD)
            }

        }

    }

}

 

thank you

 
Replies
  • Currently Being Moderated
    Aug 28, 2012 6:15 AM   in reply to thunderxlight11111

    You should always include the entire error message(s) in your postings.  With a few exceptions, there are only a couple of errors that I remember what they involve without having to see the message. 

     

    Be sure you have enabled the Permit Debugging option in your Flash Publish Settings before testing again to get the message.  It can help by isolating the line where the problem is.

     

    Also, you can then help others to help you by highlighting where in all the code you show the problemed code is.

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

    You're adding an ADDED_TO_STAGE and an ENTER_FRAME event one after another. The ENTER_FRAME event is checking fBall.stage. At that point you probably have not even added fBall to the stage yet. You should check to see if it exists in your onEnterFrame() method.

     

    e.g.

     

    public function onEnterFrame(event:Event):void

    {

        if (!fBall) { return; } // no fBall exists, ignore

     

        x += dir * speed

        if (fBall.stage)

        {

            fBall.x += firespeed * dir;

            fBall.width += 2

            fBall.height += 2

            totalfirespeed += 1

     

        }

        if (totalfirespeed == 10)

        {

            removeChild(fBall)

            totalfirespeed = 0

     

        }

    }

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

    Please highlight line 56 in your code, either bold it or even better make it appear a different color.

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

    Line 56 when I copy your code is exactly what I posted, "if (fBall.stage){". You're checking a property (stage) of something that has not been added to the display list and therefore has no stage property. Please try adding the code I bolded (the easy way) or only add an ENTER_FRAME handler once it has been added to the display list (the better way to reduce wasted cycles).

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

    Ah didn't see it was 2 different pastes of code but the issue is the same. Here:

    public function onEnterFrame(event:Event):void

    {

         if (!MovieClip(this.parent).fBall) { return; } // doesn't exist yet

         x += dir * speed

         if (MovieClip(parent).fBall.stage)

         {

              MovieClip(parent).fBall.x += firespeed * dir;

              MovieClip(parent).fBall.width += 2

              MovieClip(parent).fBall.height += 2

              totalfirespeed += 1

             

         }

         if (totalfirespeed == 10)

         {

              MovieClip(parent).removeChild(fBall)

              totalfirespeed = 0

             

         }

    }

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

    So it exists but it's not on the display list. You'll need to amend that line of code to check if it's on a display list:

     

    if ((!MovieClip(this.parent).fBall) || (!MovieClip(this.parent).contains(MovieClip(this.parent).fBall))) { return; }

     

    Again it needs to not only exist but actually be on the display list. I should have remembered that

     

    Optionally you can just check to see if the stage property is null, same difference. e.g.: MovieClip(this.parent).fBall.stage == null.

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

    Can you paste the whole code block including what you inserted? It must be at the beginning of the function.

     

    All the code is doing is quickly checking 2 things. 1, if fBall exists. 2, if it's currently on the display list.

     

    If it doesn't exist you'll get one error. If it exists but it's not on the display list then the "stage" property won't be valid and you'll get a different error.

     

    Include the error you're getting as well please.

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

    I'm assuming you have a class that is loading this "HERO" class inside itself because you're trying to access MovieClip(parent). I see you're adding fBall to the parent when the user presses the "W" key. I presumed the parent would have a defined object you're accessing via MovieClip(fBall) when I should have just read all of the code.

     

    You have a direct reference to fBall inside this class that fires off onEnterFrame. You can't reference it via MovieClip(parent).fBall as if it's a direct object reference because it's not. And if you were to try to access it as a MovieClip then you'd need to use a getChild* function of MovieClip(parent) to gain access to it via index or name. But you don't need to do that.

     

    Your first class had it correct. Just access fBall directly like this:

     

    public function onEnterFrame(event:Event):void

    {

        if ((!fBall) || (!MovieClip(this.parent).contains(fBall))) { return; }

     

        x += dir * speed

     

        fBall.x += firespeed * dir;

        fBall.width += 2

        fBall.height += 2

     

        totalfirespeed += 1

     

        if (totalfirespeed == 10)

        {

            MovieClip(parent).removeChild(fBall)

            totalfirespeed = 0

     

        }

    }

     

    edit:

     

    In the future you only need to paste the relevant lines of code giving you an error, plus a few lines above and below it. If you need to paste a large amount of code it's advisable to use something that makes code easy to see like:

    http://pastebin.com/

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

    You have to remove any MovieClip(parent).fBall references overall. Use fBall directly as I've shown you above. I'll show you how your W key code would look rewritten with it:

     

    if (event.keyCode == Keyboard.W)

    {

        SOUNDCHANNEL = FIREBALLSOUND.play()

        fBall.name = "fb" + String(i);

     

        fBall.width = 20;

        fBall.x = 400

        fBall.y = 400

     

        fireballindex.push(fBall);

     

        MovieClip(parent).addChild(fBall);

        SOUNDCHANNEL = FIREBALLSOUND.play()

     

        i++;

        trace(i)

        trace(fBall.name)

    }

     

    Remove the MovieClip(parent) off of any references to fBall in all your code. That class can directly access fBall. Even though you're adding it to the parent display list you still access it in the class directly using fBall. The class still owns the object.

     

    The HERO class extends MovieClip so you don't need to add it to the parent at all. I'm assuming you have a reason for doing so. Adding it to the HERO display list would make it show up as long as HERO was on the parents display list. Although the position of the object would be relative to the position of the HERO.

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

    When you add the fBall you still want to add it to the parent. While removing MovieClip(parent) from fBall, you didn't remove the MovieClip(parent) when you were adding it to the parents display list did you? Like in the code I posted above I'm still adding the fBall to the parent via:

     

    MovieClip(parent).addChild(fBall);

     

    So if you did a global find/replace and removed MovieClip(parent) then it might look like:

     

    addChild(fBall);

     

    If your HERO class isn't on the display list then you won't see the fireball anymore so make sure you still have MovieClip(parent) on that line.

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 28, 2012 11:37 AM   in reply to thunderxlight11111

    You're welcome

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 28, 2012 11:56 AM   in reply to thunderxlight11111

    You're welcome and good luck!

     
    |
    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