Skip to main content
Inspiring
January 18, 2014
Answered

Stopping my object [newb]

  • January 18, 2014
  • 2 replies
  • 607 views

I have an ball that moves across the screen and it works fine.

Now I am trying to add code that will stop it when I click the mouse button.

When I run the movie my clicks do nothing.

Can someone look at my code and tell me where I am going wrong?

Thank you.

<code>

package {

 

          import flash.display.MovieClip;

          import flash.events.Event;

          import flash.events.MouseEvent;

 

          public class Soccer extends MovieClip {

                    public function Soccer(){

                              this.addEventListener(Event.ENTER_FRAME,update);

                              this.addEventListener(MouseEvent.CLICK, clickHandler);

                    }

 

                    private function updatePosition():void {

                              this.x++;

                              this.y++

                    }

 

                    private function update(e:Event): void {

                              this.updatePosition();

                    }

 

                    public function clickHandler(evt:MouseEvent): void {

                              this.stop();

                    }

 

          }//close constructor

}//close package

</code>

This topic has been closed for replies.
Correct answer kglad

if the ball movieclip had an animation on the ball timeline, your code would stop that animation.

to stop the ball moving across its parent timeline you need to stop your update loop.  ie, remove that event listener.

also, update() does nothing except call updatePosition so it can be eliminated:

package {

          import flash.display.MovieClip;

          import flash.events.Event;

          import flash.events.MouseEvent;

          public class Soccer extends MovieClip {

                    public function Soccer(){

                              this.addEventListener(Event.ENTER_FRAME,updatePosition);

                              this.addEventListener(MouseEvent.CLICK, clickHandler);

                    }

                    private function updatePosition(e:Event):void {

                              this.x++;

                              this.y++

                    }

                    public function clickHandler(evt:MouseEvent): void {

                              this.removeEventListener(Event.ENTER_FRAME,updatePosition);

                    }

          }//close constructor

}//close package

2 replies

Inspiring
January 18, 2014

A couple of suggestions/tricks.

1. Class constructor should have as little logic as possible. Eventually you will find that this is not only a coding style advice but something that has positive practical implications. In general the more specialized functions are, the more scalable solution you deal with.

2. It does feel intuitive to use stop() methods to seize object movement. However, MovieClip documentation states that "Stops the playhead in the movie clip." Note that in your class you did not do anything move playhead.

Adding ENTER_FRAME listener has nothing to do with the generic MovieClip playhead. ENTER_FRAME just makes code to execute before screen is refreshed.In other words, stop() does not remove ENTER_FRAME event listeners automatically.

You can look at this from a different perspective. At this point it is clear that method stop() does not have enough functionality to accommodate your objectives. But it can be changed. Subclass can override any public API entities in AS3 (polymorphism).

Class below demonstrates that. It kills two birds with one stone - preserves native MovieClip stop() method functionality and adds logic that implements objectives of your Soccer

package

{

          import flash.display.MovieClip;

          import flash.events.Event;

          import flash.events.MouseEvent;

          public class Soccer extends MovieClip

          {

                    public function Soccer()

                    {

                              init();

                    }

                    private function init():void

                    {

                              addEventListener(Event.ENTER_FRAME, update);

                              addEventListener(MouseEvent.CLICK, clickHandler);

                    }

                    private function update(e:Event):void

                    {

                              x++;

                              y++;

                    }

                    public function clickHandler(e:MouseEvent):void

                    {

                              stop();

                    }

                    override public function stop():void

                    {

                              super.stop();

                              removeEventListener(Event.ENTER_FRAME, update);

                    }

          }

}

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
January 18, 2014

if the ball movieclip had an animation on the ball timeline, your code would stop that animation.

to stop the ball moving across its parent timeline you need to stop your update loop.  ie, remove that event listener.

also, update() does nothing except call updatePosition so it can be eliminated:

package {

          import flash.display.MovieClip;

          import flash.events.Event;

          import flash.events.MouseEvent;

          public class Soccer extends MovieClip {

                    public function Soccer(){

                              this.addEventListener(Event.ENTER_FRAME,updatePosition);

                              this.addEventListener(MouseEvent.CLICK, clickHandler);

                    }

                    private function updatePosition(e:Event):void {

                              this.x++;

                              this.y++

                    }

                    public function clickHandler(evt:MouseEvent): void {

                              this.removeEventListener(Event.ENTER_FRAME,updatePosition);

                    }

          }//close constructor

}//close package