开发者

AS3 How to shorten this bit of code?

开发者 https://www.devze.com 2023-03-15 23:57 出处:网络
开发者_高级运维I was working on something and I have this chunky bit of code in there: if(contents.x>-199 && contents.x<-1) {

开发者_高级运维I was working on something and I have this chunky bit of code in there:

if(contents.x>-199 && contents.x<-1) {
    mcPosX = 0;
} else if(contents.x>-399 && contents.x<-201) {
    mcPosX = -200;
} else if(contents.x>-599 && contents.x<-401) {
    mcPosX = -400;
} else if(contents.x>-799 && contents.x<-601) {
    mcPosX = -600;
} else if(contents.x>-999 && contents.x<-801) {
    mcPosX = -800;
} else if(contents.x>-1199 && contents.x<-1001) {
    mcPosX = -1000;
} else if(contents.x>-1399 && contents.x<-1201) {
    mcPosX = -1200;
} else if(contents.x>-1599 && contents.x<-1401) {
    mcPosX = -1400;
} else if(contents.x>-1799 && contents.x<-1601) {
    mcPosX = -1600;
} else if(contents.x>-1999 && contents.x<-1801) {
    mcPosX = -1800;
} else if(contents.x>-2199 && contents.x<-2001) {
    mcPosX = -2000;
} else if(contents.x>-2399 && contents.x<-2201) {
    mcPosX = -2200;
} else if(contents.x>-2599 && contents.x<-2401) {
    mcPosX = -2400;
} else if(contents.x>-2799 && contents.x<-2601) {
    mcPosX = -2600;
} else if(contents.x>-2999 && contents.x<-2801) {
    mcPosX = -2800;
} else {
    //mcPosX = contents.x;
}

basically I have a long movieclip (much wider than the stage), it snaps to certain points as you drag it around, depending on which section is shown on the screen.

I feel like this section is really long and I'm trying to improve my AS3. Can this be shortened/improved at all?


Hard to determine exactly what you're trying to do, butI think this might help you on the right direction:

mPos.x -= (Math.floor(contents.x / 200) * 200);
0

精彩评论

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