function setImgSize(img:Loader,limitedWidth:Number,limitedHeight:Number):void { if (img.width>limitedWidth) { img.height=limitedWidth/img.width*img.height; img.width=limitedWidth; } if (img.height>limitedHeight) { img.width=limitedHeight/img.height*img.width; img.height=limitedHeight; } } 以上通过2个if来限定宽高在限定的范围内,如果是用if……else,那么则应该如下写:(由此例我们也知道以后可以利用几个if来限制得到自己需要的结果) function setImgSize(img:Loader,limitedWidth:Number,limitedHeight:Number):void { if (img.width<=limitedWidth&&img.height<=limitedHeight) { //不需要执行任何操作 } else { if (img.width/limitedWidth>=img.height/limitedHeight) { img.height=limitedWidth/img.width*img.height; img.width=limitedWidth; } else { img.width=limitedHeight/img.height*img.width; img.height=limitedHeight; } } }