views:

432

answers:

2

I'm working on learning the basics of AS3 and have been working through a tutorial book. We just made a class that, when linked to movie clips (or conceivably any sprite) would enlarge them when rolling the mouse over them. To make sure I remembered all the principles, I tried to make a class that would make the sprite spin when moused over it, and stop when I rolled out, however I'm having trouble making the ENTER_FRAME listener play nicely. Any idea where I'm going wrong?

package { import flash.display.Sprite; import flash.events.MouseEvent; import flash.events.Event;

public class Spinnah extends Sprite
{
 private var _origRotation:Number;

 public function Spinnah()
 {
  _origRotation = this.rotation;
  this.addEventListener(MouseEvent.ROLL_OVER, eFrameOn);
  this.addEventListener(MouseEvent.ROLL_OUT, stopSpin);
 }

 private function eFrameOn (Event:MouseEvent):void
 {
  stage.addEventListener(Event.ENTER_FRAME, spin);
 }

 private function spin (event:Event):void
 {
  this.rotation += 1;
 }

 private function stopSpin (event:Event):void
 {
  stage.removeEventListener(Event.ENTER_FRAME, spin);
  this.rotation = _origRotation;
 }
}

}

A: 

wow, nevermind. I'm an idiot. I was making the ROLL_OUT function use the wrong listener, and had some errors in capitalization. Sorry. For the sake of the archives, here's working code.

package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;

public class Spinnah extends Sprite
{
 private var _origRotation:Number;

 public function Spinnah()
 {
  _origRotation = this.rotation;
  this.addEventListener(MouseEvent.ROLL_OVER, eFrameOn);
  this.addEventListener(MouseEvent.ROLL_OUT, stopSpin);
 }

 private function eFrameOn (event:MouseEvent):void
 {
  stage.addEventListener(Event.ENTER_FRAME, spin);
 }

 private function spin (event:Event):void
 {
  this.rotation += 1;
 }

 private function stopSpin (event:MouseEvent):void
 {
  stage.removeEventListener(Event.ENTER_FRAME, spin);
  this.rotation = _origRotation;
 }
}

}

Cool. Wouldn't be nicer if you had a startSpin and stopSpin methods as public and no ROLL_OVER, ROLL_OUT handlers within the class, but outside, where you would make calls yo the startSpin() and stopSpin() ?
George Profenza
What would the benefits of doing it that way be?
you would set roll over from outside the Spinnah, not from inside, so who ever uses this can set the spinning on and off at will. They wouldn't be contrained by the roll overs handled inside the component. better encapsulation I guess ?
George Profenza
all you can do from the outside is create an instance, nothing else...maybe some inlets/outlets ? when you build a house, you make windows, although you could not have windows and use light bulbs all the time...or something like that :)
George Profenza
A: 

why do private classes suck so much?!!!!!!!?

PrivateClassesREvil