我有197个变量,定义部分占用太多行。它们全部用于各种功能,并通过以下方式进行修改。
这里是代码本身的一部分,而不是整个代码(页面的30k限制)。我要针对特定“语言” ActionScript3的特定情况寻求一种通用的编程方法/解决方案。
处理类似情况的正确方法是什么?有什么方法可以减少行数?
这是源文件,如果有人想看它:https://github.com/MA62CNB/ergeneriver/blob/master/GameL.as
package
{
import GameL.*;
import caurina.transitions.*;
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.media.*;
import flash.ui.*;
import flash.utils.*;
public class GameL extends Sprite
{
var sfxChannel:SoundChannel;
var bgmChannel:SoundChannel;
var bgmLoopChannel:SoundChannel;
var playerMove:cocuk_hareket;
var rightChoice:dogru_nesne;
var introMusic:giris_icin_tam_muzik;
var bgmLoop:oyun_ici_loop_muzik;
var sfxVarious:secimler;
var success:tebrikler;
var success2:tebrikler2;
var wrongChoice:yanlis_nesne;
const MAIN_VOLUME:Number = 0.7;
const INGAME_VOLUME:Number = 0.2;
var soundStr:String;
var sndTrans:SoundTransform;
var bgmMute:Boolean = false;
const ZERO:int = 3;
var htpScreen:MainHelp;
var ingameHelp:GameInfomc;
var titleBg:Harita;
var yildiz1:Yildiz1;
var yildiz2:Yildiz2;
var yildiz3:Yildiz3;
var yildizSon:Finale;
var randomPoint:RandomPlus;
var randomPoint2:RandomPlus;
var toplam:Array;
var char:int = -1;
var currentRoadLine:int = 3;
var index1:int = 0;
var index2:int = 0;
var index3:int = 0;
var info:Info;
var cont1:MovieClip;
var cont2:MovieClip;
var cont3:MovieClip;
var cont4:MovieClip;
var nehirBg:Blm1;
var bolumNo:BolumNo;
var kopruImg:Kopru;
var yesil:Yesil;
var sari:Sari;
var pembe:Pembe;
var bot:Bot;
var deter:Deter;
var bidon:Bidon;
var kutu:Kutu;
var lastik:Lastik;
var cips:Cips;
var pet:Pet;
var konserve:Konserve;
var balik:Balik;
var ordek:Ordek;
var kurbaga:Kurbaga;
var oyuncu:MovieClip;
var canImg:Health;
var point:Points;
var itemSpeed:Object;
var score:Object;
var lives:Object;
var levelNo:Object;
var completed:int;
var endInterval:Timer;
var interval2:Timer;
var allLives:int = 4;
var animalArray:Array;
var trashArray:Array;
var pos1:Array;
var posArray:Array;
var pos3:Array;
var posYs:Array;
var finalBg:Kirli;
var pops:Pops;
var oyunSonu:OyunSon;
var gunes:Sun;
var copler:Copler;
var pompa:Pompa;
var trak1:Traktor;
var trak2:Traktor;
var trak3:Traktor;
var trak4:Traktor;
var panel:Panel;
var ormanAlan:OrmanAlan;
var bacaAlan:BacaAlan;
var turbinAlan:TurbinAlan;
var aritmaAlan:AritmaAlan;
var bacaIcon:baca_ikon;
var ormanIcon:camlar_ikon;
var turbinIcon:turbin_ikon;
var aritmaIcon:aritma_ikon;
var pompaIcon:pompa_ikon;
var trkatorIcon:traktor_ikon;
var coplerIcon:copler_ikon;
var trackIndex:int;
var counter:int = 0;
var traktorCounter:int = 0;
var aritmaFlag:Boolean = false;
var how:HowPlay;
var howLast:HowLast;
var restart_bg:Restart;
var restart_btn:RButton;
var nextBg:Interscreen;
var nextBtn:Next;
var once:Boolean = true;
public function GameL()
{
this.sfxChannel = new SoundChannel();
this.bgmChannel = new SoundChannel();
this.bgmLoopChannel = new SoundChannel();
this.playerMove = new cocuk_hareket();
this.rightChoice = new dogru_nesne();
this.introMusic = new giris_icin_tam_muzik();
this.bgmLoop = new oyun_ici_loop_muzik();
this.sfxVarious = new secimler();
this.success = new tebrikler();
this.success2 = new tebrikler2();
this.wrongChoice = new yanlis_nesne();
this.sndTrans = new SoundTransform();
this.htpScreen = new MainHelp();
this.ingameHelp = new GameInfomc();
this.titleBg = new Harita();
this.yildiz1 = new Yildiz1();
this.yildiz2 = new Yildiz2();
this.yildiz3 = new Yildiz3();
this.yildizSon = new Finale();
this.randomPoint = new RandomPlus(0, 3);
this.randomPoint2 = new RandomPlus(0, 2);
this.toplam = new Array();
this.info = new Info();
this.nehirBg = new Blm1();
this.bolumNo = new BolumNo();
this.kopruImg = new Kopru();
this.yesil = new Yesil();
this.sari = new Sari();
this.pembe = new Pembe();
this.bot = new Bot();
this.deter = new Deter();
this.bidon = new Bidon();
this.kutu = new Kutu();
this.lastik = new Lastik();
this.cips = new Cips();
this.pet = new Pet();
this.konserve = new Konserve();
this.balik = new Balik();
this.ordek = new Ordek();
this.kurbaga = new Kurbaga();
this.oyuncu = new MovieClip();
this.canImg = new Health();
this.point = new Points();
this.endInterval = new Timer(500, 1);
this.interval2 = new Timer(250, 1);
this.trashArray = new Array();
this.pos1 = [395, 595, 795];
this.posArray = [296, 496, 696, 896];
this.pos3 = [300, 400, 500, 600, 700, 800, 900];
this.posYs = [-110, -210, -310];
this.copler = new Copler();
this.pompa = new Pompa();
this.trak1 = new Traktor();
this.trak2 = new Traktor();
this.trak3 = new Traktor();
this.trak4 = new Traktor();
this.ormanAlan = new OrmanAlan();
this.bacaAlan = new BacaAlan();
this.turbinAlan = new TurbinAlan();
this.aritmaAlan = new AritmaAlan();
this.bacaIcon = new baca_ikon();
this.ormanIcon = new camlar_ikon();
this.turbinIcon = new turbin_ikon();
this.aritmaIcon = new aritma_ikon();
this.pompaIcon = new pompa_ikon();
this.trkatorIcon = new traktor_ikon();
this.coplerIcon = new copler_ikon();
this.how = new HowPlay();
this.howLast = new HowLast();
this.restart_bg = new Restart();
this.restart_btn = new RButton();
this.nextBg = new Interscreen();
this.nextBtn = new Next();
this.init(0);
this.levelNo = 1;
this.completed = 0;
return;
}// end function
private function init(addChild:int) : void
{
switch(addChild)
{
case 0:
{
this.InitLobi();
break;
}
case 1:
{
this.completed = 0;
this.levelNo = 1;
this.itemSpeed = 12;
this.score = 0;
this.nehirBg.kirlinehirmc.alpha = 0.5;
this.point.texts.text = "0";
this.lives = 3;
this.canImg.gotoAndStop(1);
break;
}
case 2:
{
this.currentRoadLine = this.ZERO;
this.itemSpeed = 15;
this.score = 0;
this.nehirBg.kirlinehirmc.alpha = 0.5;
this.point.texts.text = "0";
this.lives = 3;
this.canImg.gotoAndStop(1);
break;
}
case 3:
{
this.currentRoadLine = this.ZERO;
this.itemSpeed = 16;
this.score = 0;
this.nehirBg.kirlinehirmc.alpha = 0.5;
this.point.texts.text = "0";
this.lives = 3;
this.canImg.gotoAndStop(1);
break;
}
case 4:
{
this.BeginLast();
break;
}
case -1:
{
addChild(this.restart_bg);
this.restart_bg.cacheAsBitmap = true;
addChild(this.restart_btn);
this.restart_bg.x = 0;
this.restart_bg.y = 0;
this.restart_btn.x = 352.5;
this.restart_btn.y = 512.5;
this.restart_btn.addEventListener(MouseEvent.CLICK, this.DoRestart, false, 0, true);
this.restart_btn.buttonMode = true;
this.canImg.gotoAndStop(4);
this.score = 0;
break;
}
default:
{
break;
}
}
return;
}// end function
private function InitLobi()
{
this.sndTrans.volume = this.MAIN_VOLUME;
addChild(this.titleBg);
if (this.bgmMute == false)
{
this.titleBg.ses_kontrol.gotoAndStop(1);
SoundMixer.stopAll();
this.bgmLoopChannel.stop();
this.bgmChannel.stop();
this.bgmChannel = this.introMusic.play(0, 1, this.sndTrans);
this.bgmChannel.addEventListener(Event.SOUND_COMPLETE, this.IsSoundFinished);
}
else
{
this.titleBg.ses_kontrol.gotoAndStop(2);
this.bgmMute = false;
}
this.titleBg.ses_kontrol.buttonMode = true;
this.titleBg.ses_kontrol.addEventListener(MouseEvent.CLICK, this.BgmControl);
this.soundStr = "introMusic";
this.titleBg.cacheAsBitmap = true;
this.titleBg.helpb.addEventListener(MouseEvent.CLICK, this.DisplayHtp);
this.titleBg.helpb.buttonMode = true;
this.titleBg.y2mc.visible = true;
this.titleBg.y3mc.visible = true;
this.titleBg.y4mc.visible = true;
addChild(this.yildiz1);
addChild(this.yildiz2);
addChild(this.yildiz3);
addChild(this.yildizSon);
this.yildiz2.visible = false;
this.yildiz3.visible = false;
this.yildizSon.visible = false;
this.yildiz1.addEventListener(MouseEvent.CLICK, this.FirstStage);
this.yildiz1.buttonMode = true;
this.YildizLoaderHandler();
this.titleBg.x = 0;
this.titleBg.y = 0;
this.yildiz1.x = 215.8;
this.yildiz1.y = 158.9;
this.yildiz2.x = 132.45;
this.yildiz2.y = 411.5;
this.yildiz3.x = 463.25;
this.yildiz3.y = 282.6;
this.yildizSon.x = 734.55;
this.yildizSon.y = 199.6;
return;
}// end function
private function InitMain()
{
var _loc_3:uint = 0;
var _loc_4:uint = 0;
var _loc_5:uint = 0;
var _loc_6:OyunK = null;
var _loc_7:OyunE = null;
this.sndTrans.volume = this.INGAME_VOLUME;
this.cont1 = new MovieClip();
this.cont2 = new MovieClip();
this.cont3 = new MovieClip();
this.cont1.addChild(this.nehirBg);
this.cont1.addChild(this.bolumNo);
this.cont1.addChild(this.canImg);
this.cont1.addChild(this.point);
if (this.bgmMute == false)
{
this.point.ses_cont.gotoAndStop(1);
this.bgmChannel.stop();
this.bgmLoopChannel.stop();
this.bgmLoopChannel = this.bgmLoop.play(0, 1, this.sndTrans);
this.bgmLoopChannel.addEventListener(Event.SOUND_COMPLETE, this.IsSoundFinished);
}
else
{
this.point.ses_cont.gotoAndStop(2);
this.bgmMute = false;
}
this.point.ses_cont.buttonMode = true;
this.point.ses_cont.addEventListener(MouseEvent.CLICK, this.BgmControl);
this.soundStr = "bgmLoop";
this.endInterval.addEventListener(TimerEvent.TIMER_COMPLETE, this.IntervalHandler);
this.nehirBg.cacheAsBitmap = true;
this.bolumNo.cacheAsBitmap = true;
this.canImg.cacheAsBitmap = true;
this.point.cacheAsBitmap = true;
addEventListener(Event.ENTER_FRAME, this.MainScenes);
stage.addEventListener(KeyboardEvent.KEY_DOWN, this.KeyMoves);
this.point.play_b.addEventListener(MouseEvent.CLICK, this.PauseGame);
this.point.lobi_b.addEventListener(MouseEvent.CLICK, this.GoLobi);
this.point.yardim_b.addEventListener(MouseEvent.CLICK, this.ShowIngameHelp);
this.point.play_b.buttonMode = true;
this.point.lobi_b.buttonMode = true;
this.point.yardim_b.buttonMode = true;
this.animalArray = [this.balik, this.kurbaga, this.ordek];
this.trashArray = [this.bidon, this.bot, this.cips, this.deter, this.konserve, this.kutu, this.lastik, this.pet, this.pembe, this.sari, this.yesil];
var _loc_1:uint = 0;
while (_loc_1++ < 3)
{
this.animalArray[_loc_1].x = this.posArray[this.randomPoint.getNum()];
this.animalArray[_loc_1].y = this.posYs[this.randomPoint2.getNum()];
addChild(this.cont1);
this.cont1.addChild(this.animalArray[_loc_1]);
}
var _loc_2:uint = 0;
while (_loc_2++ < 11)
{
this.trashArray[_loc_2].x = this.pos1[this.randomPoint2.getNum()];
this.trashArray[_loc_2].y = -150;
addChild(this.cont2);
this.cont2.addChild(this.trashArray[_loc_2]);
}
addChild(this.cont3);
this.cont3.addChild(this.kopruImg);
if (this.levelNo == 1)
{
_loc_3 = 0;
while (_loc_3++ < 11)
{
this.toplam[_loc_3] = 1;
}
}
if (this.levelNo == 2)
{
_loc_4 = 0;
while (_loc_4++ < 11)
{
this.toplam[_loc_4] = 1;
}
}
if (this.levelNo == 3)
{
_loc_5 = 0;
while (_loc_5++ < 11)
{
this.toplam[_loc_5] = 1;
}
}
if (this.char == 1)
{
_loc_6 = new OyunK();
this.oyuncu = _loc_6;
}
if (this.char == 0)
{
_loc_7 = new OyunE();
this.oyuncu = _loc_7;
}
this.oyuncu.cacheAsBitmap = true;
this.nehirBg.x = 0;
this.nehirBg.y = 0;
this.kopruImg.y = 588;
this.bolumNo.x = 43.2;
this.bolumNo.y = 22.8;
this.oyuncu.x = 600;
this.oyuncu.y = 450;
this.cont3.addChild(this.oyuncu);
this.canImg.x = 55.9;
this.canImg.y = 134;
this.point.x = stage.stageWidth - (this.point.width + this.point.width / 8);
this.point.y = 25;
this.addEventListener(Event.ENTER_FRAME, this.MainScenes, false, 0, true);
return;
}// end function
private function MainScenes(event:Event)
{
this.bolumNo.blm_num.text = String(this.levelNo);
stage.addEventListener(KeyboardEvent.KEY_DOWN, this.KeyMoves);
var _loc_2:uint = 0;
while (_loc_2++ < this.toplam.length)
{
if (this.toplam[_loc_2] < 0)
{
this.toplam[_loc_2] = 0;
}
}
this.point.txt0.text = String(this.toplam[0]);
this.point.txt1.text = String(this.toplam[1]);
this.point.txt2.text = String(this.toplam[2]);
this.point.txt3.text = String(this.toplam[3]);
this.point.txt4.text = String(this.toplam[4]);
this.point.txt5.text = String(this.toplam[5]);
this.point.txt6.text = String(this.toplam[6]);
this.point.txt7.text = String(this.toplam[7]);
this.point.txt8.text = String(this.toplam[8] + this.toplam[9] + this.toplam[10]);
if (!this.Puanlama(this.toplam))
{
if (this.oyuncu.x <= this.pos3[0])
{
this.oyuncu.x = this.pos3[0];
}
else if (this.oyuncu.x >= this.pos3[this.pos3.length--])
{
this.oyuncu.x = this.pos3[this.pos3.length--];
}
if (this.animalArray != null)
{
if (this.animalArray[0].y > stage.stageHeight)
{
this.animalArray[0].x = this.posArray[this.randomPoint.getNum()];
this.animalArray[0].y = -150;
var _loc_3:String = this;
_loc_3.index1 = this.index1++;
}
else
{
if (this.animalArray[0].hitTestObject(this.oyuncu.suzgec_alan_MC))
{
var _loc_3:String = this;
_loc_3.lives = this.lives--;
this.nehirBg.nega.gotoAndPlay(2);
this.canImg.gotoAndStop(this.allLives - this.lives);
this.score = this.score - 100;
this.sfxChannel.stop();
this.sfxChannel = this.wrongChoice.play();
this.point.texts.text = String(this.score);
this.animalArray[0].x = this.posArray[this.randomPoint.getNum()];
this.animalArray[0].y = -150;
}
this.animalArray[0].y = this.animalArray[0].y + this.itemSpeed;
}
}
if (this.levelNo > 1 && this.animalArray != null)
{
if (this.animalArray[1].y > stage.stageHeight)
{
this.animalArray[1].x = this.posArray[this.randomPoint.getNum()];
this.animalArray[1].y = -150;
}
else
{
if (this.animalArray[1].hitTestObject(this.oyuncu.suzgec_alan_MC))
{
var _loc_3:String = this;
_loc_3.lives = this.lives--;
this.nehirBg.nega.gotoAndPlay(2);
this.canImg.gotoAndStop(this.allLives - this.lives);
this.score = this.score - 100;
this.sfxChannel.stop();
this.sfxChannel = this.wrongChoice.play();
this.point.texts.text = String(this.score);
this.animalArray[1].x = this.posArray[this.randomPoint.getNum()];
this.animalArray[1].y = -150;
}
this.animalArray[1].y = this.animalArray[1].y + this.itemSpeed;
}
}
if (this.levelNo > 2 && this.animalArray != null)
{
if (this.animalArray[2].y > stage.stageHeight)
{
this.animalArray[2].x = this.posArray[this.randomPoint.getNum()];
this.animalArray[2].y = -150;
}
else
{
if (this.animalArray[2].hitTestObject(this.oyuncu.suzgec_alan_MC))
{
var _loc_3:String = this;
_loc_3.lives = this.lives--;
this.nehirBg.nega.gotoAndPlay(2);
this.canImg.gotoAndStop(this.allLives - this.lives);
this.score = this.score - 100;
this.sfxChannel.stop();
this.sfxChannel = this.wrongChoice.play();
this.point.texts.text = String(this.score);
this.animalArray[2].x = this.posArray[this.randomPoint.getNum()];
this.animalArray[2].y = -150;
}
this.animalArray[2].y = this.animalArray[2].y + this.itemSpeed;
}
}
if (this.trashArray != null && this.index3 < this.trashArray.length)
{
if (this.trashArray[this.index3].y > stage.stageHeight)
{
var _loc_3:String = this;
_loc_3.lives = this.lives--;
this.nehirBg.nega.gotoAndPlay(2);
this.canImg.gotoAndStop(this.allLives - this.lives);
this.score = this.score - 100;
this.sfxChannel.stop();
this.sfxChannel = this.wrongChoice.play();
this.point.texts.text = String(this.score);
this.trashArray[this.index3].x = this.pos1[this.randomPoint2.getNum()];
this.trashArray[this.index3].y = -150;
var _loc_3:String = this;
_loc_3.index3 = this.index3++;
if (this.index3 >= this.trashArray.length)
{
this.index3 = 0;
this.trashArray[this.index1].x = this.pos1[this.randomPoint2.getNum()];
this.trashArray[this.index1].y = -150;
}
}
else
{
if (this.trashArray[this.index3].hitTestObject(this.oyuncu.suzgec_alan_MC))
{
this.score = this.score + 100;
this.nehirBg.plus.gotoAndPlay(2);
this.sfxChannel.stop();
this.sfxChannel = this.rightChoice.play();
this.point.texts.text = String(this.score);
this.trashArray[this.index3].x = this.pos1[this.randomPoint2.getNum()];
this.trashArray[this.index3].y = -150;
var _loc_3:* = this.toplam;
var _loc_4:* = this.index3;
_loc_3[_loc_4] = this.toplam[this.index3]--;
var _loc_3:String = this;
_loc_3.index3 = this.index3++;
if (this.index3 >= this.trashArray.length)
{
this.index3 = 0;
this.trashArray[this.index3].x = this.pos1[this.randomPoint2.getNum()];
this.trashArray[this.index3].y = -150;
}
if (this.nehirBg.kirlinehirmc.alpha > 0)
{
this.nehirBg.kirlinehirmc.alpha = this.nehirBg.kirlinehirmc.alpha - 0.05;
}
}
this.trashArray[this.index3].y = this.trashArray[this.index3].y + this.itemSpeed;
}
}
if (this.lives < 1)
{
this.canImg.gotoAndStop(5);
removeEventListener(Event.ENTER_FRAME, this.MainScenes);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, this.KeyMoves);
this.endInterval.start();
}
}
else
{
removeEventListener(Event.ENTER_FRAME, this.MainScenes);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, this.KeyMoves);
this.endInterval.start();
this.score = 0;
}
return;
}// end function
private function FirstStage(event:MouseEvent)
{
this.sfxChannel.stop();
this.sfxChannel = this.sfxVarious.play();
this.init(1);
addChild(this.info);
this.info.cacheAsBitmap = true;
this.info.gotoAndStop(1);
this.info.ileriok.gotoAndStop(1);
this.info.x = 0;
this.info.y = 0;
this.info.ileriok.addEventListener(MouseEvent.CLICK, this.NextPage);
this.info.ileriok.buttonMode = true;
return;
}// end function
private function KeyMoves(event:KeyboardEvent) : void
{
var _loc_2:* = event.keyCode;
var _loc_3:* = 1.3 - 0.5;
this.sfxChannel.stop();
this.sfxChannel = this.playerMove.play();
switch(_loc_2)
{
case Keyboard.LEFT:
{
if (this.currentRoadLine > 0)
{
var _loc_4:String = this;
_loc_4.currentRoadLine = this.currentRoadLine--;
Tweener.addTween(this.oyuncu, {x:this.pos3[this.currentRoadLine], time:_loc_3});
}
break;
}
case Keyboard.RIGHT:
{
if (this.currentRoadLine < this.pos3.length)
{
if (this.currentRoadLine != this.pos3.length--)
{
var _loc_4:String = this;
_loc_4.currentRoadLine = this.currentRoadLine++;
}
Tweener.addTween(this.oyuncu, {x:this.pos3[this.currentRoadLine], time:_loc_3});
}
break;
}
default:
{
break;
}
}
return;
}// end function
private function Puanlama(txt2:Array) : Boolean
{
var _loc_2:Boolean = false;
var _loc_3:int = 0;
var _loc_4:uint = 0;
while (_loc_4++ < txt2.length)
{
if (txt2[_loc_4] == 0 || txt2[_loc_4] < 0)
{
_loc_3++;
}
}
if (_loc_3 == txt2.length)
{
_loc_2 = true;
}
return _loc_2;
}// end function
private function BeginLast()
{
addChild(this.how);
this.how.cacheAsBitmap = true;
this.how.gotoAndStop(1);
this.how.start_b.addEventListener(MouseEvent.CLICK, this.FinalScene);
this.how.start_b.buttonMode = true;
this.how.start_b.gotoAndStop(1);
return;
}// end function
private function StartGame(event:MouseEvent)
{
this.InitMain();
return;
}// end function
private function ReturnGame(event:MouseEvent)
{
addEventListener(Event.ENTER_FRAME, this.MainScenes);
stage.addEventListener(KeyboardEvent.KEY_DOWN, this.KeyMoves);
this.ingameHelp.visible = false;
return;
}// end function
private function PauseGame(event:MouseEvent)
{
this.point.play_b.removeEventListener(MouseEvent.CLICK, this.ResumeGame);
removeEventListener(Event.ENTER_FRAME, this.MainScenes);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, this.KeyMoves);
this.point.play_b.gotoAndStop(2);
this.point.play_b.addEventListener(MouseEvent.CLICK, this.ResumeGame);
this.point.play_b.buttonMode = true;
return;
}// end function
private function ResumeGame(event:MouseEvent)
{
this.point.play_b.removeEventListener(MouseEvent.CLICK, this.PauseGame);
addEventListener(Event.ENTER_FRAME, this.MainScenes);
stage.addEventListener(KeyboardEvent.KEY_DOWN, this.KeyMoves);
this.point.play_b.gotoAndStop(1);
this.point.play_b.addEventListener(MouseEvent.CLICK, this.PauseGame);
this.point.play_b.buttonMode = true;
return;
}// end function
}
}
我浏览了您的代码,主要问题是代码未优化。该变量反映了代码的方式或症状。没错,您正处于学习如何编程的阶段,从头开始。如果清除代码,对大量变量的需求就会消失。
您正在使用文字命名作为变量。当您开始优化代码时,您会看到自己不想按字面命名的方式。
例如,我看不到发生了什么,但是看起来bacaIcon和ormaIcon在代码中对它们所做的一切都可能相同,除了它们被附加到两个不同的对象上。
查找代码中的优化问题的最佳方法是简单地寻找重复。理想情况下,您实际上不会在优化的代码中看到重复。不要阅读代码并尝试对其进行处理,只需查看页面上的代码并查找看起来相似的代码块即可。
以下是您的代码中的几个示例,它们显示了我在说什么:
首先,一个简单的例子,看看其中的重复:
this.ormanIcon.addEventListener(MouseEvent.MOUSE_DOWN, this.StartDragn);
this.ormanIcon.addEventListener(MouseEvent.MOUSE_UP, this.StopDragn);
this.ormanIcon.buttonMode = true;
this.bacaIcon.addEventListener(MouseEvent.MOUSE_DOWN, this.StartDragn);
this.bacaIcon.addEventListener(MouseEvent.MOUSE_UP, this.StopDragn);
this.bacaIcon.buttonMode = true;
this.turbinIcon.addEventListener(MouseEvent.MOUSE_DOWN, this.StartDragn);
this.turbinIcon.addEventListener(MouseEvent.MOUSE_UP, this.StopDragn);
this.turbinIcon.buttonMode = true;
this.aritmaIcon.addEventListener(MouseEvent.MOUSE_DOWN, this.StartDragn);
this.aritmaIcon.addEventListener(MouseEvent.MOUSE_UP, this.StopDragn);
this.aritmaIcon.buttonMode = true;
只需查看代码,您就会看到所有这些单词都在重复。您不必知道代码在做什么就可以知道重复是错误的。因此,请查找重复中实际发生的变化,在这种情况下,将动作的目标,图标,变化的部分变成变量,将其余部分变成函数,然后循环。
这里是另一个更大但相同的例子。您有很多这些,它们应该是一个功能
if (this.copler.hitTestObject(this.panel.kp.area))
{
this.sfxChannel.stop();
this.sfxChannel = this.rightChoice.play();
this.copler.removeEventListener(MouseEvent.MOUSE_DOWN, this.StartDragn);
this.copler.removeEventListener(MouseEvent.MOUSE_UP, this.StopDragn);
removeChild(this.copler);
addChild(this.coplerIcon);
addChild(this.pops);
this.finalBg.kirlihava.alpha = this.finalBg.kirlihava.alpha - 0.13;
this.finalBg.kirlitoprak.alpha = this.finalBg.kirlitoprak.alpha - 0.5;
this.finalBg.cicekmc.visible = true;
this.gunes.alpha = this.gunes.alpha + 0.16;
var _loc_3:String = this;
_loc_3.counter = this.counter++;
this.pops.pop_cop.visible = true;
this.pops.bg.visible = true;
this.pops.pop_cop.popx.addEventListener(MouseEvent.CLICK, this.PopTicker);
this.pops.pop_cop.popx.buttonMode = true;
}
else
{
this.sfxChannel.stop();
this.sfxChannel = this.wrongChoice.play();
this.copler.x = 863.65;
this.copler.y = 521.6;
}
}
if (event.target == this.pompa)
{
this.pompa.stopDrag();
if (this.pompa.hitTestObject(this.panel.kp.area))
{
this.sfxChannel.stop();
this.sfxChannel = this.rightChoice.play();
this.pompa.removeEventListener(MouseEvent.MOUSE_DOWN, this.StartDragn);
this.pompa.removeEventListener(MouseEvent.MOUSE_UP, this.StopDragn);
removeChild(this.pompa);
addChild(this.pompaIcon);
addChild(this.pops);
this.finalBg.kirlihava.alpha = this.finalBg.kirlihava.alpha - 0.13;
this.finalBg.kirlitoprak.alpha = this.finalBg.kirlitoprak.alpha - 0.1;
this.gunes.alpha = this.gunes.alpha + 0.16;
var _loc_3:String = this;
_loc_3.counter = this.counter++;
this.pops.pop_pompa.visible = true;
this.pops.bg.visible = true;
this.pops.pop_pompa.popx.addEventListener(MouseEvent.CLICK, this.PopTicker);
this.pops.pop_pompa.popx.buttonMode = true;
}
else
{
this.sfxChannel.stop();
this.sfxChannel = this.wrongChoice.play();
this.pompa.x = 124.9;
this.pompa.y = 405.8;
}
[将它们转换为通用功能时,您将看到如何优化变量设计。
键可能是数组。大多数变量应位于数组中。当您开始将代码放入函数中时,您将了解为什么数组起作用。