同级判断,就对同一种参数进行判断.不要同时掺杂多种类型.比如
/**
* 图片滑动
* @param scrollview scrollview
* @param dirction 滑动方向
* @param distance 滑动距离
*/
private UpdateScrollviewHonorSpritesContainer( scrollview:cc.ScrollView , dirction:EnumSlideDirection , distance:number ) : void
{
if( scrollview == null )
{
cc.warn("DlgHonor.UpdateScrollviewHonorSpritesContainer:scrollview不能为空");
return;
}
let totalSpriteNumberMax = scrollview.content.children.length - 1;
let totalSpriteNumberMin = 0;
let playAnimationTime = 0.2;
let scrollEndPoint = cc.Vec2.ZERO;
this.ChangeIsPlayingAnimation(playAnimationTime);
if ( dirction == EnumSlideDirection.eDirection_Right )//向右滑动一张图片
{
if ( this.m_nCurrentSpriteIndex < totalSpriteNumberMax)
{
this.m_nCurrentSpriteIndex += 1;
}
scrollEndPoint = cc.p( this.m_nHonorSpriteWidth * this.m_nCurrentSpriteIndex, 0 );
}
else if ( dirction == EnumSlideDirection.eDirection_Left )//向左滑动一张图片
{
if ( this.m_nCurrentSpriteIndex > totalSpriteNumberMin )
{
this.m_nCurrentSpriteIndex -= 1;
}
scrollEndPoint = cc.p( this.m_nHonorSpriteWidth * this.m_nCurrentSpriteIndex , 0 );
}
else if ( distance != 0 )//根据距离滑动
{
playAnimationTime = 0;
scrollEndPoint = cc.p( distance , 0 );
playAnimationTime = 0;
else//根据图片索引滑动
{
scrollEndPoint = cc.p( this.m_nHonorSpriteWidth * this.m_nCurrentSpriteIndex , 0 );
}
scrollview.scrollToOffset( scrollEndPoint , playAnimationTime );
}
这里先是判断方向,是个枚举,对参数二进行判断,后面又对参数三进行判断,而且还是不同类型,在同一级的判断里,这都是不允许的.
应该写成如下样子:
/**
* 图片滑动
* @param scrollview scrollview
* @param dirction 滑动方向
* @param distance 滑动距离
*/
private UpdateScrollviewHonorSpritesContainer( scrollview:cc.ScrollView , dirction:EnumSlideDirection , distance:number ) : void
{
if( scrollview == null )
{
cc.warn("DlgHonor.UpdateScrollviewHonorSpritesContainer:scrollview不能为空");
return;
}
let totalSpriteNumberMax = scrollview.content.children.length - 1;
let totalSpriteNumberMin = 0;
let playAnimationTime = 0.2;
let scrollEndPoint = cc.Vec2.ZERO;
this.ChangeIsPlayingAnimation(playAnimationTime);
if ( dirction == EnumSlideDirection.eDirection_Right )//向右滑动一张图片
{
if ( this.m_nCurrentSpriteIndex < totalSpriteNumberMax)
{
this.m_nCurrentSpriteIndex += 1;
}
scrollEndPoint = cc.p( this.m_nHonorSpriteWidth * this.m_nCurrentSpriteIndex, 0 );
}
else if ( dirction == EnumSlideDirection.eDirection_Left )//向左滑动一张图片
{
if ( this.m_nCurrentSpriteIndex > totalSpriteNumberMin )
{
this.m_nCurrentSpriteIndex -= 1;
}
scrollEndPoint = cc.p( this.m_nHonorSpriteWidth * this.m_nCurrentSpriteIndex , 0 );
}
else
{
if ( distance != 0 ) //根据距离滑动
{
playAnimationTime = 0;
scrollEndPoint = cc.p( distance , 0 );
playAnimationTime = 0;
}
else//根据图片索引滑动
{
scrollEndPoint = cc.p( this.m_nHonorSpriteWidth * this.m_nCurrentSpriteIndex , 0 );
}
}
scrollview.scrollToOffset( scrollEndPoint , playAnimationTime );
}
尽量避免如下代码:
/**
* 界面即将退出动画
* @param callBack 回调
*/
PlayAnimationOnWillDisable(callBack)
{
let endPosition = cc.p( - this.node.width , 0 );
let playAnimationTime = 0.1;
let moveAnimation=cc.moveTo(playAnimationTime,endPosition);
this.ChangeIsPlayingAnimation(playAnimationTime);
this.Node_playAnimation.stopAllActions();
this.Node_playAnimation.runAction(moveAnimation);
if ( callBack )
{
this.scheduleOnce(function()
{
callBack();
},playAnimationTime);
}
}
由于是通过计算时间来回调,并不能确切地在执行完动画之后马上执行回调,这样的代码是不足的.回调函数也应尽量少用,更别在计时器里面回调.
当拿一个布尔值进行判断时,不需要用”==false”之类的==号或者”!=”号,直接放在括号里面就行了,如果不是布尔值,需明确的用”==”或者”!=”来表明意思.
枚举类型在自定义的时候需要给其一个默认值,即没有意义的值.如:
/**
* 滑动方向
*/
export enum EnumSlideDirection
{
eDirection_Defult = 0,
eDirection_Right = 1,
eDirection_Left = 2,
}
此处的Defult就位默认值,在做判断的时候,枚举类型不用判空,而是判默认值.
当一个算式过长的时候需要分成多步计算,每一步有一个变量名,这样就很好理解算式的意义,容易读懂也方便日后修改,比如:
let contentHeight = ( this.m_nGridHeight + layoutSpacingValue ) * Number( ( this.m_nTotalGridNumber / this.m_nTotalGridLineNumber ).toFixed(0) ) + 1 + layoutSpacingValue;
这样的算式应该拆分成多个,并取有意义的变量名.
let verticalGridNumber = Number( ( this.m_nTotalGridNumber / this.m_nTotalGridLineNumber ).toFixed(0) ) + 1 ;
let contentHeight = ( this.m_nGridHeight + layoutSpacingValue ) * verticalGridNumber + layoutSpacingValue;