Image 当我把一个图像放到AS3中的垃圾桶图像上时,我需要将其从舞台上移除
每当我拖放到垃圾桶时,我需要从舞台上移除图像。我一直在考虑这个问题,似乎真的找不到解决办法Image 当我把一个图像放到AS3中的垃圾桶图像上时,我需要将其从舞台上移除,image,actionscript-3,Image,Actionscript 3,每当我拖放到垃圾桶时,我需要从舞台上移除图像。我一直在考虑这个问题,似乎真的找不到解决办法 <fx:Declarations> <!-- Place non-visual elements (e.g., services, value objects) here --> </fx:Declarations> <fx:Script> <![CDATA[ import mx.controls.Image;
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Image;
//array of movies
public var myArray:Array = ["batman.jpg","cabin.jpg","christmasVacation.jpg"];
//init function
public function init():void{
var trashImage:Image = new Image();
//set trash img
trashImage.source = "asset/trash.jpg";
//adds img to stage
trashGrp.addElement(trashImage);
//loops 4 times
for(var i:Number = 0; i < myArray.length; i++){
var arrayEntry:String = myArray[i];
//makes new image
var newImage:Image = new Image();
//sets the image source for each image
newImage.source = "asset/" + arrayEntry;
//adds the image to the stage
grpMovies.addElement(newImage);
//set drag/drop
newImage.addEventListener(MouseEvent.MOUSE_DOWN, dragMe);
newImage.addEventListener(MouseEvent.MOUSE_UP, dropMe);
}
}
//dragMe function
public function dragMe(e:MouseEvent):void
{
// Get the Image that saw the mouse-down.
var img:Image = Image(e.currentTarget);
// Use the built-in method to start dragging.
img.startDrag();
}
//dropMe function
public function dropMe(e:MouseEvent):void
{
// Get the Image that saw the mouse-up.
var img:Image = Image(e.currentTarget);
// Use the built-in method to stop dragging.
img.stopDrag();
-**this is where my issue is. I cant get it to remove it when the image is hit**
if (trashGrp.hitTestObject(img)) {
grpMovies.removeElement(img);
}
}
]]>
</fx:Script>
<s:TileGroup id="grpMovies" columnWidth="225" rowHeight="225" requestedRowCount="1"
paddingTop="20"/>
<s:HGroup id= "trashGrp" x="950" y="20"/>
</s:Application>
查看以下代码,我认为它符合您的要求。我将事件处理程序修改为附加到主容器,以便在释放垃圾桶上的鼠标按钮时正确触发它
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="init()">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Image;
//array of movies
public var myArray:Array = ["batman.jpg","cabin.jpg","christmasVacation.jpg"];
private var _draggedImage:Image;
//init function
public function init():void{
var trashImage:Image = new Image();
//set trash img
trashImage.source = "assets/trash.png";
trashImage.mouseEnabled = false;
trashImage.mouseChildren = false;
//adds img to stage
trashGrp.addElement(trashImage);
//loops 4 times
for(var i:Number = 0; i < myArray.length; i++){
var arrayEntry:String = myArray[i];
//makes new image
var newImage:Image = new Image();
//sets the image source for each image
newImage.source = "assets/"+arrayEntry;
//adds the image to the stage
grpMovies.addElement(newImage);
//set drag/drop
newImage.addEventListener(MouseEvent.MOUSE_DOWN, dragMe);
}
}
//dragMe function
public function dragMe(e:MouseEvent):void
{
// Get the Image that saw the mouse-down.
var img:Image = Image(e.currentTarget);
// Use the built-in method to start dragging.
img.startDrag();
_draggedImage = img;
this.addEventListener(MouseEvent.MOUSE_UP, dropMe);
}
//dropMe function
public function dropMe(e:MouseEvent):void
{
_draggedImage.stopDrag();
this.removeEventListener(MouseEvent.MOUSE_UP, dropMe);
if (trashGrp.hitTestObject(_draggedImage)) {
grpMovies.removeElement(_draggedImage);
_draggedImage = null;
}
}
]]>
</fx:Script>
<s:TileGroup id="grpMovies" columnWidth="225" rowHeight="225" requestedRowCount="1"
paddingTop="20"/>
<s:HGroup id= "trashGrp" x="950" y="20"/>
</s:Application>
查看以下代码,我认为它符合您的要求。我将事件处理程序修改为附加到主容器,以便在释放垃圾桶上的鼠标按钮时正确触发它
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="init()">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Image;
//array of movies
public var myArray:Array = ["batman.jpg","cabin.jpg","christmasVacation.jpg"];
private var _draggedImage:Image;
//init function
public function init():void{
var trashImage:Image = new Image();
//set trash img
trashImage.source = "assets/trash.png";
trashImage.mouseEnabled = false;
trashImage.mouseChildren = false;
//adds img to stage
trashGrp.addElement(trashImage);
//loops 4 times
for(var i:Number = 0; i < myArray.length; i++){
var arrayEntry:String = myArray[i];
//makes new image
var newImage:Image = new Image();
//sets the image source for each image
newImage.source = "assets/"+arrayEntry;
//adds the image to the stage
grpMovies.addElement(newImage);
//set drag/drop
newImage.addEventListener(MouseEvent.MOUSE_DOWN, dragMe);
}
}
//dragMe function
public function dragMe(e:MouseEvent):void
{
// Get the Image that saw the mouse-down.
var img:Image = Image(e.currentTarget);
// Use the built-in method to start dragging.
img.startDrag();
_draggedImage = img;
this.addEventListener(MouseEvent.MOUSE_UP, dropMe);
}
//dropMe function
public function dropMe(e:MouseEvent):void
{
_draggedImage.stopDrag();
this.removeEventListener(MouseEvent.MOUSE_UP, dropMe);
if (trashGrp.hitTestObject(_draggedImage)) {
grpMovies.removeElement(_draggedImage);
_draggedImage = null;
}
}
]]>
</fx:Script>
<s:TileGroup id="grpMovies" columnWidth="225" rowHeight="225" requestedRowCount="1"
paddingTop="20"/>
<s:HGroup id= "trashGrp" x="950" y="20"/>
</s:Application>
是否执行了grpMovies.removeElement(img)
?不,这是我无法理解的。我觉得应该这样,但事实并非如此。if语句中的条件由于某种原因没有得到满足,我想执行grpMovies.removeElement(img)
了吗?不,这是我无法理解的。我觉得应该这样,但事实并非如此。如果声明中的条件由于某种原因不能得到满足,我想非常感谢。它工作得很好。所以我得到的只是在某个函数中处理它?不是整个应用程序?您所做的是尝试在正在拖动的图像上检测鼠标上升事件,当鼠标在垃圾桶上释放时,事件从未触发,因此它没有删除图像。非常感谢。它工作得很好。所以我得到的只是在某个函数中处理它?不是整个应用程序?您所做的是尝试在正在拖动的图像上检测鼠标上升事件,当鼠标在垃圾桶上释放时,事件从未触发,因此它没有删除图像。