我有一些AS3(我是新手!)并且我能让代码工作的唯一方法是非常文字并反复复制它所以我想知道是否有人能够提供更优雅的解决方案那有用吗?
我保证我会从中学习 - 由于动态影片剪辑在尝试使用for循环中的整数来构建名称时动态影片剪辑无法被其实例名称识别,因此我无法获得for循环工作(如果这有道理吗?)......
到目前为止,这是我的(笨重)代码,我希望它能够满足50次点击,而不仅仅是我在这里的6次:
import flash.display.*
pic.addEventListener(MouseEvent.CLICK,addNewPoint);
var n:Number = 0;
function addNewPoint(e:MouseEvent):void {
n++;
pointNo.text = String(n);
// Create a new MovieClip
var nextPoint:MovieClip = new newPoint();
// Add the new MovieClip to the MainTimeline
// so that we can see it.
addChild(nextPoint);
nextPoint.name = "mc"+pointNo.text;
// Set the location of the new MovieClip
nextPoint.x = e.target.mouseX;
nextPoint.y = e.target.mouseY;
//Draw the line between points
if (n == 1) {
var joinPoints:MovieClip = new MovieClip();
this.addChild(joinPoints);
joinPoints.graphics.lineStyle(0.5,0xFF0000);
joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
}else if (n == 2) {
var joinPoints:MovieClip = new MovieClip();
this.addChild(joinPoints);
joinPoints.graphics.lineStyle(0.5,0xFF0000);
joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
joinPoints.graphics.lineTo(this.getChildByName("mc2").x, this.getChildByName("mc2").y);
}else if (n == 3) {
var joinPoints:MovieClip = new MovieClip();
this.addChild(joinPoints);
joinPoints.graphics.lineStyle(0.5,0xFF0000);
joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
joinPoints.graphics.lineTo(this.getChildByName("mc2").x, this.getChildByName("mc2").y);
joinPoints.graphics.lineTo(this.getChildByName("mc3").x, this.getChildByName("mc3").y);
}else if (n == 4) {
var joinPoints:MovieClip = new MovieClip();
this.addChild(joinPoints);
joinPoints.graphics.lineStyle(0.5,0xFF0000);
joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
joinPoints.graphics.lineTo(this.getChildByName("mc2").x, this.getChildByName("mc2").y);
joinPoints.graphics.lineTo(this.getChildByName("mc3").x, this.getChildByName("mc3").y);
joinPoints.graphics.lineTo(this.getChildByName("mc4").x, this.getChildByName("mc4").y);
}
else if (n == 5) {
var joinPoints:MovieClip = new MovieClip();
this.addChild(joinPoints);
joinPoints.graphics.lineStyle(0.5,0xFF0000);
joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
joinPoints.graphics.lineTo(this.getChildByName("mc2").x, this.getChildByName("mc2").y);
joinPoints.graphics.lineTo(this.getChildByName("mc3").x, this.getChildByName("mc3").y);
joinPoints.graphics.lineTo(this.getChildByName("mc4").x, this.getChildByName("mc4").y);
joinPoints.graphics.lineTo(this.getChildByName("mc5").x, this.getChildByName("mc5").y);
}
else if (n == 6) {
var joinPoints:MovieClip = new MovieClip();
this.addChild(joinPoints);
joinPoints.graphics.lineStyle(0.5,0xFF0000);
joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
joinPoints.graphics.lineTo(this.getChildByName("mc2").x, this.getChildByName("mc2").y);
joinPoints.graphics.lineTo(this.getChildByName("mc3").x, this.getChildByName("mc3").y);
joinPoints.graphics.lineTo(this.getChildByName("mc4").x, this.getChildByName("mc4").y);
joinPoints.graphics.lineTo(this.getChildByName("mc5").x, this.getChildByName("mc5").y);
joinPoints.graphics.lineTo(this.getChildByName("mc6").x, this.getChildByName("mc6").y);
}
}
pic.addEventListener(MouseEvent.CLICK, addNewPoint);
function fillDriveway(eventObject:MouseEvent) {
var joinPoints:MovieClip = new MovieClip();
this.addChild(joinPoints);
if (n == 3) {
var joinPoints:MovieClip = new MovieClip();
this.addChild(joinPoints);
joinPoints.graphics.lineStyle(0.5,0xFFFFFF);
joinPoints.graphics.beginFill(0xFFFFFF, 0.2);
joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
joinPoints.graphics.lineTo(this.getChildByName("mc2").x, this.getChildByName("mc2").y);
joinPoints.graphics.lineTo(this.getChildByName("mc3").x, this.getChildByName("mc3").y);
this.getChildByName("mc1").visible=false;
this.getChildByName("mc2").visible=false;
this.getChildByName("mc3").visible=false;
}else if (n == 4) {
var joinPoints:MovieClip = new MovieClip();
this.addChild(joinPoints);
joinPoints.graphics.lineStyle(0.5,0xFFFFFF);
joinPoints.graphics.beginFill(0xFFFFFF, 0.2);
joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
joinPoints.graphics.lineTo(this.getChildByName("mc2").x, this.getChildByName("mc2").y);
joinPoints.graphics.lineTo(this.getChildByName("mc3").x, this.getChildByName("mc3").y);
joinPoints.graphics.lineTo(this.getChildByName("mc4").x, this.getChildByName("mc4").y);
this.getChildByName("mc1").visible=false;
this.getChildByName("mc2").visible=false;
this.getChildByName("mc3").visible=false;
this.getChildByName("mc4").visible=false;
}else if (n == 5) {
var joinPoints:MovieClip = new MovieClip();
this.addChild(joinPoints);
joinPoints.graphics.lineStyle(0.5,0xFFFFFF);
joinPoints.graphics.beginFill(0xFFFFFF, 0.2);
joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
joinPoints.graphics.lineTo(this.getChildByName("mc2").x, this.getChildByName("mc2").y);
joinPoints.graphics.lineTo(this.getChildByName("mc3").x, this.getChildByName("mc3").y);
joinPoints.graphics.lineTo(this.getChildByName("mc4").x, this.getChildByName("mc4").y);
joinPoints.graphics.lineTo(this.getChildByName("mc5").x, this.getChildByName("mc5").y);
this.getChildByName("mc1").visible=false;
this.getChildByName("mc2").visible=false;
this.getChildByName("mc3").visible=false;
this.getChildByName("mc4").visible=false;
this.getChildByName("mc5").visible=false;
}else if (n == 6) {
var joinPoints:MovieClip = new MovieClip();
this.addChild(joinPoints);
joinPoints.graphics.lineStyle(0.5,0xFFFFFF);
joinPoints.graphics.beginFill(0xFFFFFF, 0.2);
joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
joinPoints.graphics.lineTo(this.getChildByName("mc2").x, this.getChildByName("mc2").y);
joinPoints.graphics.lineTo(this.getChildByName("mc3").x, this.getChildByName("mc3").y);
joinPoints.graphics.lineTo(this.getChildByName("mc4").x, this.getChildByName("mc4").y);
joinPoints.graphics.lineTo(this.getChildByName("mc5").x, this.getChildByName("mc5").y);
joinPoints.graphics.lineTo(this.getChildByName("mc6").x, this.getChildByName("mc6").y);
this.getChildByName("mc1").visible=false;
this.getChildByName("mc2").visible=false;
this.getChildByName("mc3").visible=false;
this.getChildByName("mc4").visible=false;
this.getChildByName("mc5").visible=false;
this.getChildByName("mc6").visible=false;
}
}
btn1.addEventListener(MouseEvent.CLICK, fillDriveway);
function undoit(eventObject:MouseEvent) {
if(n > 0) {
this.removeChild(this.getChildAt(this.numChildren -1));
this.removeChild(this.getChildAt(this.numChildren -1));
n--;
pointNo.text = String(n);
}
}
undo.addEventListener(MouseEvent.CLICK, undoit);
** EDIT --------------------------------------------
感谢Eduardo - 完成的代码如下:**
import flash.display.*
pic.addEventListener(MouseEvent.CLICK,addNewPoint);
var n:Number = 0;
function addNewPoint(e:MouseEvent):void {
n++;
pointNo.text = String(n);
// Create a new MovieClip
var nextPoint:MovieClip = new newPoint();
// Add the new MovieClip to the MainTimeline
// so that we can see it.
addChild(nextPoint);
nextPoint.name = "mc"+pointNo.text;
// Set the location of the new MovieClip
nextPoint.x = e.target.mouseX;
nextPoint.y = e.target.mouseY;
//Draw the line between points
var joinPoints:MovieClip = new MovieClip();
this.addChild(joinPoints);
joinPoints.graphics.lineStyle(0.5,0xFF0000);
joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
for(var i:int=2; i<=n; ++i){
joinPoints.graphics.lineTo(this.getChildByName("mc"+i).x, this.getChildByName("mc"+i).y);
}
}
pic.addEventListener(MouseEvent.CLICK, addNewPoint);
function fillDriveway(eventObject:MouseEvent) {
var joinPoints:MovieClip = new MovieClip();
this.addChild(joinPoints);
joinPoints.graphics.lineStyle(0.5,0xFFFFFF);
joinPoints.graphics.beginFill(0xFFFFFF, 0.2);
joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
for(var i:int=2; i<=n; ++i){
joinPoints.graphics.lineTo(this.getChildByName("mc"+i).x, this.getChildByName("mc"+i).y);
}
}
btn1.addEventListener(MouseEvent.CLICK, fillDriveway);
答案 0 :(得分:4)
你可以通过以下方式简化ifs:
var joinPoints:MovieClip = new MovieClip();
this.addChild(joinPoints);
joinPoints.graphics.lineStyle(0.5,0xFF0000);
joinPoints.graphics.moveTo(this.getChildByName("mc1").x, this.getChildByName("mc1").y);
for(var i:int=2; i<=numpoints; ++i){
joinPoints.graphics.lineTo(this.getChildByName("mc"+i).x, this.getChildByName("mc"+i).y);
}
那将照顾第一组ifs,第二组可以用类似的方式处理