AS3 - 如何删除我点击的项目?
AS3 - How do I delete an item I clicked on?
我是 AS3 的新手,我遇到了一个无法解决的愚蠢的小问题。
我有一个场景,当我点击舞台时,我生成了一个 MovieClip 框并将其以网格方式放置在舞台上。我想再次单击它以将其删除,但是这部分不起作用。我想我把它缩小到 removeChild()
脚本本身,因为在 AS3 中,我得到 DisplayObject
错误......但是,这正是我发现的,我可能是错的。大多数情况下,我正在使用 'WonderFL' 网站进行测试。
package {
import flash.display.AVM1Movie;
import flash.display.Shape;
import flash.geom.Rectangle;
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
//Begin Code
//Event Listeners
stage.addEventListener(MouseEvent.MOUSE_DOWN, stageClick);
//Variables
var squareSize:Number = 120;
var xGridPos:Number = 0;
var yGridPos:Number = 0;
var tallyContainer:MovieClip = new MovieClip();
//Clicking on the stage
function stageClick(me:MouseEvent):void {
xGridPos = Math.floor(stage.mouseX/squareSize) * squareSize;
yGridPos = Math.floor(stage.mouseY/squareSize) * squareSize;
createTally();
}
//Clicking on a tally
function tallyDown(me:MouseEvent):void {
removeChild(tally);
var visibleTrace:MovieClip = new MovieClip();
visibleTrace.graphics.beginFill (0x000000, 1) ;
visibleTrace.graphics.drawRect (0, 0, 20, 20);
visibleTrace.graphics.endFill();
addChild(visibleTrace);
}
//Creating a tally
function createTally():void {
var tally:MovieClip = new MovieClip();
tally.graphics.beginFill(0xDD1111, 1) ;
tally.graphics.drawRect(xGridPos, yGridPos, squareSize, squareSize) ;
tally.graphics.endFill() ;
tally.addEventListener(MouseEvent.MOUSE_DOWN, tallyDown);
addChild(tally);
}
//End Code
}
}
}
我尝试查找解决方案(Stack Overflow 上还有其他关于苹果和熊的帖子),但我无法成功实施它们。我还查阅了数组和循环(具体来说,code.tutsplus 上的 'Basix' 教程)。同样,我无法成功实施它们。
删除理货我做错了什么?
除了DisplayObject
的错误,你的代码还有一些小问题,让我们看看。
首先,您不能将所有代码都放在 class 的构造函数中,因此您的 class 可以像这样:
package {
// imports
public class FlashTest extends Sprite
{
// properties and constants declaration part
// constructor
public function FlashTest()
{
// class's initialization instructions and other stuff
}
// other methods
}
}
然后,在 createTally()
函数 ( var tally:MovieClip = new MovieClip();
) 中创建的 tally
对象是它自己的本地对象,在它之外无法访问(当然 undefined
) , 所以其他功能无法使用。
但在您的情况下,您甚至不需要访问该对象,因为要获取当前单击的对象,您可以使用 target
property of your MouseEvent
实例:
function tallyDown(me:MouseEvent):void
{
var tally:MovieClip = MovieClip(me.target);
// then to remove that "tally" :
tally.parent.removeChild(tally);
// ...
}
...
顺便说一句,您可以使用免费的 FlashDevelop 来测试您的代码...
希望能帮到你。
我是 AS3 的新手,我遇到了一个无法解决的愚蠢的小问题。
我有一个场景,当我点击舞台时,我生成了一个 MovieClip 框并将其以网格方式放置在舞台上。我想再次单击它以将其删除,但是这部分不起作用。我想我把它缩小到 removeChild()
脚本本身,因为在 AS3 中,我得到 DisplayObject
错误......但是,这正是我发现的,我可能是错的。大多数情况下,我正在使用 'WonderFL' 网站进行测试。
package {
import flash.display.AVM1Movie;
import flash.display.Shape;
import flash.geom.Rectangle;
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
//Begin Code
//Event Listeners
stage.addEventListener(MouseEvent.MOUSE_DOWN, stageClick);
//Variables
var squareSize:Number = 120;
var xGridPos:Number = 0;
var yGridPos:Number = 0;
var tallyContainer:MovieClip = new MovieClip();
//Clicking on the stage
function stageClick(me:MouseEvent):void {
xGridPos = Math.floor(stage.mouseX/squareSize) * squareSize;
yGridPos = Math.floor(stage.mouseY/squareSize) * squareSize;
createTally();
}
//Clicking on a tally
function tallyDown(me:MouseEvent):void {
removeChild(tally);
var visibleTrace:MovieClip = new MovieClip();
visibleTrace.graphics.beginFill (0x000000, 1) ;
visibleTrace.graphics.drawRect (0, 0, 20, 20);
visibleTrace.graphics.endFill();
addChild(visibleTrace);
}
//Creating a tally
function createTally():void {
var tally:MovieClip = new MovieClip();
tally.graphics.beginFill(0xDD1111, 1) ;
tally.graphics.drawRect(xGridPos, yGridPos, squareSize, squareSize) ;
tally.graphics.endFill() ;
tally.addEventListener(MouseEvent.MOUSE_DOWN, tallyDown);
addChild(tally);
}
//End Code
}
}
}
我尝试查找解决方案(Stack Overflow 上还有其他关于苹果和熊的帖子),但我无法成功实施它们。我还查阅了数组和循环(具体来说,code.tutsplus 上的 'Basix' 教程)。同样,我无法成功实施它们。
删除理货我做错了什么?
除了DisplayObject
的错误,你的代码还有一些小问题,让我们看看。
首先,您不能将所有代码都放在 class 的构造函数中,因此您的 class 可以像这样:
package {
// imports
public class FlashTest extends Sprite
{
// properties and constants declaration part
// constructor
public function FlashTest()
{
// class's initialization instructions and other stuff
}
// other methods
}
}
然后,在 createTally()
函数 ( var tally:MovieClip = new MovieClip();
) 中创建的 tally
对象是它自己的本地对象,在它之外无法访问(当然 undefined
) , 所以其他功能无法使用。
但在您的情况下,您甚至不需要访问该对象,因为要获取当前单击的对象,您可以使用 target
property of your MouseEvent
实例:
function tallyDown(me:MouseEvent):void
{
var tally:MovieClip = MovieClip(me.target);
// then to remove that "tally" :
tally.parent.removeChild(tally);
// ...
}
...
顺便说一句,您可以使用免费的 FlashDevelop 来测试您的代码...
希望能帮到你。