- 本帖最后由 aster2012 于 2012-4-11 20:11 编辑 *
代码见附件,使用方法:设计大小为320x480的时候;
例如一个Sprite在右下方,距离屏幕右方40像素,距离屏幕下方为30像素。
则需要如下设置:
sprite->SetScale( CCGetScaleX() ); // 应用放缩,仅仅X方向即可。
sprite->setPosition( CCGetScreenPoint(-40.0f, 30.0f, CC_ALIGNMENT_RB) );
那么在屏幕大小为480x800的时候,看到的效果是:
sprite被放大了1.5倍数
sprite在右下方,距离屏幕右方60像素,距离屏幕下方为50像素。
以上一点小代码,希望对大家能有所帮助,如有建议欢迎指出。
附源代码如下:
#ifndef CCLayout_h__
#define CCLayout_h__
#include “cocos2d.h”
USING_NS_CC;
// design size.
// you can change it yourself.
#define CC_DESIGN_W (480.0f)
#define CC_DESIGN_H (320.0f)
inline float CCGetScaleX()
{
CCSize winSize =
CCDirector::sharedDirector()->getWinSize();
return winSize.width / CC_DESIGN_W;
}
inline float CCGetScaleY()
{
CCSize winSize =
CCDirector::sharedDirector()->getWinSize();
return winSize.height / CC_DESIGN_H;
}
inline float CCGetCenterX()
{
CCSize winSize =
CCDirector::sharedDirector()->getWinSize();
return winSize.width / 2.0f;
}
inline float CCGetCenterY()
{
CCSize winSize =
CCDirector::sharedDirector()->getWinSize();
return winSize.height / 2.0f;
}
inline CCPoint CCGetCenterXY()
{
CCSize winSize =
CCDirector::sharedDirector()->getWinSize();
return ccp(winSize.width / 2.0f, winSize.height / 2.0f);
}
enum CCAlignment
{
CC_ALIGNMENT_LB = 0, // left bottom
CC_ALIGNMENT_CB = 1, // center bottom
CC_ALIGNMENT_RB = 2, // right bottom
CC_ALIGNMENT_LC = 3, // left center
CC_ALIGNMENT_CC = 4, // center center
CC_ALIGNMENT_RC = 5, // right center
CC_ALIGNMENT_LT = 6, // left top
CC_ALIGNMENT_CT = 7, // center top
CC_ALIGNMENT_RT = 8, // right top
};
inline CCPoint CCGetScreenPoint(float x, float y, CCAlignment alignment)
{
CCSize winSize =
CCDirector::sharedDirector()->getWinSize();
const float scaleX = winSize.width / CC_DESIGN_W;
const float scaleY = winSize.height / CC_DESIGN_H;
CCPoint result;
if (alignment % 3 == 0)
{
result.x = x * scaleX;
}
else if (alignment % 3 == 2)
{
result.x = winSize.width + x * scaleX;
}
else
{
result.x = winSize.width / 2.0f + x * scaleY;
}
if (alignment / 3 == 0)
{
result.y = y * scaleY;
}
else if (alignment / 3 == 2)
{
result.y = winSize.height + y * scaleY;
}
else
{
result.y = winSize.height / 2.0f + y * scaleY;
}
return result;
}
inline CCPoint CCGetScreenPoint(const CCPoint& point, CCAlignment alignment)
{
return CCGetScreenPoint(point.x, point.y, alignment);
}
#endif // CCLayout_h__