Adding a roll-over 'close' button on a component
I have a class that extends the image class. When the user clicks a button, a
handler creates an instance of this class. The class has methods to allow for
dragging, etc.
I would like to add a "close button" for the object, when the user rolls over
the object it appears, and clicking it removes the object. I'm not sure how to
integrate that into my AS class file. Or should I have made this an mxml
component?
package assets
{
import flash.events.MouseEvent;
import mx.controls.Image;
import mx.effects.SoundEffect;
import mx.events.MoveEvent;
import mx.core.Application;
public class ItemImage extends Image
{
public var filePath:String;
public var clickSound:SoundEffect = new SoundEffect(this);
public var createSound:SoundEffect = new SoundEffect(this);
public var deleteSound:SoundEffect = new SoundEffect(this);
private var sfxPath:String = '/assets/flash/sewing-room/';
private var deleted:Boolean = false;
public function ItemImage(fp:String)
{
super();
filePath = fp;
this.source = filePath;
this.addEventListener(MouseEvent.MOUSE_DOWN, dragMe);
this.addEventListener(MouseEvent.MOUSE_UP, dropMe);
this.addEventListener(MouseEvent.CLICK, clickedMe);
this.toolTip = "Drag to wherever you like. Click and release to bring to
front.";
clickSound.source = sfxPath+"assets/sfx/tink.mp3";
createSound.source = sfxPath+"assets/sfx/pop.mp3";
deleteSound.source = sfxPath+"assets/sfx/blow.mp3";
createSound.play();
}
private function dragMe(e:MouseEvent):void {
this.startDrag();
this.alpha = .5;
Application.application.trashCan.visible = true;
}
private function dropMe(e:MouseEvent):void {
this.stopDrag();
this.alpha = 1;
if (this.x > 486 && this.y > 256){
parent.removeChild(this);
deleted = true;
deleteSound.play();
}
Application.application.trashCan.visible = false;
}
private function clickedMe(e:MouseEvent):void {
if (!deleted){
parent.setChildIndex(this, parent.numChildren-1);
clickSound.play();
}
}
}
}
|