Skip navigation
kanamemadoka
Currently Being Moderated

Question about Variables

Nov 29, 2012 8:08 PM

Tags: #variable #variables #mouse_event

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

 


 
Replies
  • kglad
    72,261 posts
    Jul 21, 2002
    Currently Being Moderated
    Nov 29, 2012 9:55 PM   in reply to kanamemadoka

    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++;
    }


     
    |
    Mark as:
  • kglad
    72,261 posts
    Jul 21, 2002
    Currently Being Moderated
    Nov 30, 2012 6:47 AM   in reply to kanamemadoka

    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);

                        }

              }

     

    }

     
    |
    Mark as:
  • kglad
    72,261 posts
    Jul 21, 2002
    Currently Being Moderated
    Nov 30, 2012 10:24 AM   in reply to kanamemadoka

    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).

     
    |
    Mark as:
  • kglad
    72,261 posts
    Jul 21, 2002
    Currently Being Moderated
    Nov 30, 2012 10:34 AM   in reply to kanamemadoka

    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);

                        }

              }

     

    }

     
    |
    Mark as:
  • kglad
    72,261 posts
    Jul 21, 2002
    Currently Being Moderated
    Nov 30, 2012 12:07 PM   in reply to kanamemadoka

    you're welcome.

     

    p.s. please mark helpful/correct responses.

     
    |
    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