10 Replies Latest reply: Nov 30, 2012 12:07 PM by kglad RSS

    Question about Variables

    kanamemadoka

      This might seem a bit strange, but I'm having trouble figuring it out. I have no errors, I'm just trying to figure out how to add in a simple points system, which at the moment all I want it to do is keep track of itself. I made an external class for 'coins', and whenever you mouse over one, it disappears. I want to have it so that every time you do this, it adds 1 to the points variable. The problem is that the way I have it set up, it continuously resets every time the mouse over event listener is called, and stays at one, because I've added the event listener to the entire class. I know I could do it from the main class, but I have the 'coins' generated in a for loop so their placement will be random, and I really don't want to have to manually add every single one. Any suggestions on how to fix it so that it goes up one every time instead of resetting to 0?

       

      Here is my current code, I'm only including the bits that are important.

       

      this is the coin class (currently named enemy, since that's what it was originally going to be)

      package  {

       

                import flash.display.MovieClip;

                import flash.events.Event;

                import flash.events.MouseEvent;

       

                public class enemy extends MovieClip {

       

                          var points:uint = 0;

       

       

                          public function enemy() {

                                    // constructor code

                                    this.addEventListener(Event.ENTER_FRAME, moveEnemys);

                                    this.addEventListener(MouseEvent.MOUSE_OVER, disappear);

       

       

                          }

       

                          function disappear(event:MouseEvent)

                          {

                                         this.visible = false;

                                         points ++;

                                         trace(points);

                          }

       

                          function moveEnemys(event:Event)

                          {

       

       

                                    this.x -= 6;

       

                          }

       

       

                }

       

      }

       

      Here is where I add it to the stage in the main class:

       

      var en:enemy;

       

      function addEnemy()

                          {

                                    for (var i:int = 0; i<10; i++)

                                    {

                                              en = new enemy();

       

                                              en.y = Math.ceil(Math.random() * 290) + 10;

                                              en.x = Math.ceil(Math.random() * 1000) + 500;

                                              addChild(en);

       

                                    }

       

                          }

      And in the output window, this is what I get after mousing over 6 of the coins:

       

      1

      1

      1

      1

      1

      1

       


        • 1. Re: Question about Variables
          kglad CommunityMVP

          use:

           

          package  {

           

                    import flash.display.MovieClip;

                    import flash.events.Event;

                    import flash.events.MouseEvent;

           

                    public class enemy extends MovieClip {

           

                   private var main:YourMainClass;

           

           

                              public function enemy(mc:MovieClip) {  // if your main class extends MovieClip

          main=mc;

                                        // constructor code

                                        this.addEventListener(Event.ENTER_FRAME, moveEnemys);

                                        this.addEventListener(MouseEvent.MOUSE_OVER, disappear);

           

           

                              }

           

                              function disappear(event:MouseEvent)

                              {

                                             this.visible = false;

                                             mc.pointF();

           

                              }

           

                              function moveEnemys(event:Event)

                              {

           

           

                                        this.x -= 6;

           

                              }

           

           

                    }

           

          }

           

          Here is where I add it to the stage in the main class:

           

          var en:enemy;

          private var points:int = 0;

           

          function addEnemy()

                              {

                                        for (var i:int = 0; i<10; i++)

                                        {

                                                  en = new enemy(this);

           

                                                  en.y = Math.ceil(Math.random() * 290) + 10;

                                                  en.x = Math.ceil(Math.random() * 1000) + 500;

                                                  addChild(en);

           

                                        }

           

                              }

          public function pointF():void{
          points++;
          }


          • 2. Re: Question about Variables
            kanamemadoka Community Member

            Thank you, but this didn't work. It gives me error 1118, implicit coercion of a value with a static type flash.display:MovieClip to a possibly unrelated type proFinal. My main class is named proFinal, and it does extend movie clip, so I'm not sure what's wrong.

             

            It also gives me error 1061, call to a possibly undefined method pointF through a reference with a static type Class.

            • 3. Re: Question about Variables
              kanamemadoka Community Member

              And another note, I tried something similar with a completely new class called point. It still didn't work, and I don't understand why, since it should be calling the method more than once.The important bits are bolded:

               

              package  {

               

                        import flash.display.MovieClip;

                        import flash.events.Event;

                        import flash.events.MouseEvent;

                        import flash.media.Sound;

                        import flash.media.SoundChannel;

                        import flash.media.SoundTransform;

               

                            public class enemy extends MovieClip {

               

               

                                  var score:point;

                                  

                                  var sound:Sound = new saw();

                                  var soundchannel:SoundChannel = new SoundChannel();

                                  var volumeAdjust:SoundTransform = new SoundTransform();

               

               

               

                      public function enemy()

                                  {

                                            // constructor code

                                            score = new point();

               

                                            addEventListener(Event.ENTER_FRAME, moveEnemys);

                                            addEventListener(MouseEvent.MOUSE_OVER, disappear);

               

               

                                  }

               

                                  function disappear(event:MouseEvent)

                                  {

                                            this.visible = false;

                                            sound.play();

                                            volumeAdjust.volume += 1;

                                            soundchannel = sound.play();

                                            soundchannel.soundTransform = volumeAdjust;

                                            score.pointAdd();

               

               

               

                                  }

               

               

                                  function moveEnemys(event:Event)

                                  {

                                            this.x -= 6;

                                  }

               

               

                        }

               

              }

               

               

              package  {

               

                        import flash.display.MovieClip;

               

               

                            public class point extends MovieClip {

               

               

                                  var points:uint = 0;

               

                      public function point()

                                  {

               

                                  }

               

               

                                  public function pointAdd():void

                                  {

                                            points ++;

                                            trace(points);

                                  }

                        }

               

              }

              • 4. Re: Question about Variables
                kglad CommunityMVP

                use:

                 

                 

                package  {

                 

                          import flash.display.MovieClip;

                          import flash.events.Event;

                          import flash.events.MouseEvent;

                          import flash.media.Sound;

                          import flash.media.SoundChannel;

                          import flash.media.SoundTransform;

                 

                              public class enemy extends MovieClip {

                 

                 

                              

                 

                                    var sound:Sound = new saw();

                                    var soundchannel:SoundChannel = new SoundChannel();

                                    var volumeAdjust:SoundTransform = new SoundTransform();

                 

                 

                 

                        public function enemy()

                                    {

                                              // constructor code

                                        

                 

                                              addEventListener(Event.ENTER_FRAME, moveEnemys);

                                              addEventListener(MouseEvent.MOUSE_OVER, disappear);

                 

                 

                                    }

                 

                                    function disappear(event:MouseEvent)

                                    {

                                              this.visible = false;

                                              sound.play();

                                              volumeAdjust.volume += 1;

                                              soundchannel = sound.play();

                                              soundchannel.soundTransform = volumeAdjust;

                                              point.pointAdd();

                 

                 

                 

                                    }

                 

                 

                                    function moveEnemys(event:Event)

                                    {

                                              this.x -= 6;

                                    }

                 

                 

                          }

                 

                }

                 

                 

                package  {

                 

                          import flash.display.MovieClip;

                 

                 

                              public class point extends MovieClip {

                 

                 

                                    var points:uint = 0;

                 

                        public function point()

                                    {

                 

                                    }

                 

                 

                                    public static function pointAdd():void

                                    {

                                              points ++;

                                              trace(points);

                                    }

                          }

                 

                }

                • 5. Re: Question about Variables
                  kanamemadoka Community Member

                  Even after copying and pasting that exactly, it still gives me a count of 1, 1, 1, etc.

                  • 6. Re: Question about Variables
                    kglad CommunityMVP

                    then you failed to copy correctly because you should see an error message unless you made points a static variable.

                     

                    copy and paste the code you used (after making points a static variable).

                    • 7. Re: Question about Variables
                      kanamemadoka Community Member

                      package  {

                       

                                import flash.display.MovieClip;

                                import flash.events.Event;

                                import flash.events.MouseEvent;

                       

                       

                                    public class enemy extends MovieClip {

                       

                           public function enemy()

                                          {

                                                    // constructor code

                       

                                                    addEventListener(Event.ENTER_FRAME, moveEnemys);

                                                    this.addEventListener(MouseEvent.MOUSE_OVER, disappear);

                       

                                          }

                       

                                          function disappear(event:MouseEvent)

                                          {

                                                    this.visible = false;

                                                    point.pointAdd();

                                          }

                       

                       

                                          function moveEnemys(event:Event)

                                          {

                                                    this.x -= 6;

                                          }

                       

                       

                                }

                       

                      }

                       

                       

                       

                       

                      package  {

                       

                                import flash.display.MovieClip;

                                import flash.events.Event;

                       

                       

                                    public class point extends MovieClip {

                       

                       

                       

                              public function point()

                                          {

                                          }

                       

                       

                       

                                          public static function pointAdd():void

                                          {

                      //it gave me an error unless I used the variable in this function

                                                    var points:uint;

                                                    points += 1;

                                                    trace(points);

                                          }

                                }

                       

                      }

                      • 8. Re: Question about Variables
                        kglad CommunityMVP

                        that's incorrect.  use:

                         

                         

                        package  {

                         

                                  import flash.display.MovieClip;

                                  import flash.events.Event;

                         

                         

                                      public class point extends MovieClip {

                         

                        private static var points:int;

                         

                                public function point()

                                            {

                                            }

                         

                         

                         

                                            public static function pointAdd():void

                                            {

                                                      points += 1;

                                                      trace(points);

                                            }

                                  }

                         

                        }

                        • 9. Re: Question about Variables
                          kanamemadoka Community Member

                          All right, thank you.

                          • 10. Re: Question about Variables
                            kglad CommunityMVP

                            you're welcome.

                             

                            p.s. please mark helpful/correct responses.