在方法内创建对象
function shoot() {
var circle:Sprite = new Sprite();
circle.graphics.beginFill(0xFF794B);
circle.graphics.drawCircle(0, 00, 7.5);
circle.graphics.endFill();
addChild(circle);
circle.x = ship_mc.x;
circle.y = ship_mc.y + 43;
}
否则,您将只有一个circle
变量。这一次,每次调用该方法时都会创建一个新圆圈。
但是,您可能希望以某种方式存储所有圈子,以便以后删除它们。
var allCircles: Vector.<Sprite> = new Vector.<Sprite>();
function shoot() {
var circle:Sprite = new Sprite();
circle.graphics.beginFill(0xFF794B);
circle.graphics.drawCircle(0, 00, 7.5);
circle.graphics.endFill();
addChild(circle);
circle.x = ship_mc.x;
circle.y = ship_mc.y + 43;
allCircles.push(circle);
}
然后,稍后,您可以遍历所有圈子:
for each (var circle: Sprite in allCircles) {
// do something with this circle
}
并清除所有圈子:
for each (var circle: Sprite in allCircles) {
removeChild(circle);
}
allCircles.clear();