开发者

AS3: How can I shorten this if else statement?

I'm new to AS3 so please forgive me if this is a silly question.

I'm trying to write an if else statement for something, and although its pretty long, it's only 1/5 of what I need开发者_如何学JAVA. I definitely see a pattern in this, and I want to write a loop for it.

but I can't seem to get my head wrapped around it. Can someone please help me out?

if(all_mc.x<-1 && all_mc.x>-panelWidth+1) {
    mcPosX = 0;
} else if(all_mc.x<-panelWidth-1 && all_mc.x>-panelWidth*2+1) {
    mcPosX = -panelWidth;
} else if(all_mc.x<-panelWidth*2-1 && all_mc.x>-panelWidth*3+1) {
    mcPosX = -panelWidth*2;
} else if(all_mc.x<-panelWidth*3-1 && all_mc.x>-panelWidth*4+1) {
    mcPosX = -panelWidth*3;
} else if(all_mc.x<-panelWidth*4-1 && all_mc.x>-panelWidth*5+1) {
    mcPosX = -panelWidth*4;
} else if(all_mc.x<-panelWidth*5-1 && all_mc.x>-panelWidth*6+1) {
    mcPosX = -panelWidth*5;
} else if(all_mc.x<-panelWidth*6-1 && all_mc.x>-panelWidth*7+1) {
    mcPosX = -panelWidth*6;
} else if(all_mc.x<-panelWidth*7-1 && all_mc.x>-panelWidth*8+1) {
    mcPosX = -panelWidth*7;
} else if(all_mc.x<-panelWidth*8-1 && all_mc.x>-panelWidth*9+1) {
    mcPosX = -panelWidth*8;
} else if(all_mc.x<-panelWidth*9-1 && all_mc.x>-panelWidth*10+1) {
    mcPosX = -panelWidth*9;
} else if(all_mc.x<-panelWidth*10-1 && all_mc.x>-panelWidth*11+1) {
    mcPosX = -panelWidth*10;
} else if(all_mc.x<-panelWidth*11-1 && all_mc.x>-panelWidth*12+1) {
    mcPosX = -panelWidth*11;
} else if(all_mc.x<-panelWidth*12-1 && all_mc.x>-panelWidth*13+1) {
    mcPosX = -panelWidth*12;
} else if(all_mc.x<-panelWidth*13-1 && all_mc.x>-panelWidth*14+1) {
    mcPosX = -panelWidth*13;
} else if(all_mc.x<-panelWidth*14-1 && all_mc.x>-panelWidth*15+1) {
    mcPosX = -panelWidth*14;
} else if(all_mc.x<-panelWidth*15-1 && all_mc.x>-panelWidth*16+1) {
    mcPosX = -panelWidth*15;
} else if(all_mc.x<-panelWidth*16-1 && all_mc.x>-panelWidth*17+1) {
    mcPosX = -panelWidth*16;
} else if(all_mc.x<-panelWidth*17-1 && all_mc.x>-panelWidth*18+1) {
    mcPosX = -panelWidth*17;
} else if(all_mc.x<-panelWidth*18-1 && all_mc.x>-panelWidth*19+1) {
    mcPosX = -panelWidth*18;
} else if(all_mc.x<-panelWidth*19-1 && all_mc.x>-panelWidth*20+1) {
    mcPosX = -panelWidth*19;
}

//Tween all_mc to position
var toRightTween:TweenLite = new TweenLite(all_mc,0.5, {x:mcPosX});

mcPosX needs to go up to = -panelWidth*200!


var i:int = 200;
if(all_mc.x<-1 && all_mc.x>-panelWidth+1) {
    mcPosX = 0;
} else{
    while(all_mc.x<(-panelWidth*i)-1 && all_mc.x>(-panelWidth*(i+1))+1) {
       i--;
    }
    mcPosX = -panelWidth*i;
}

There might be better solution for your problem i think, but this is ok for the part you post.


I think you may make this question easier to answer if you explain what you are trying to achieve as opposed to trying to fix your implementation. I'm guessing that you can probably achieve your intended result using different logic altogether.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜