1、随机函数
float AppUtils::randomFloat(float low, float high)
{
//0.0-1.0
float r = rand()/(RAND_MAX+0.1);
return ((high+1) - low) * r + low;
}
CCRect AppUtils::boundingBoxForWorld(CCNode* node)
{
CCRect rect = CCRectZero;
if (node==NULL)
{
return rect;
}
CCPoint anchorPoint = node->getAnchorPoint();
CCSize widgetSize = node->boundingBox().size;
CCPoint worldPosition = node->getPosition();
worldPosition.x -= widgetSize.width*anchorPoint.x;
worldPosition.y -= widgetSize.height*anchorPoint.y;
if (node!=NULL)
{
CCNode* parent = node->getParent();
while (parent != NULL)
{
float scaleX = parent->getScaleX();
float scaleY = parent->getScaleY();
float scaledWidth = widgetSize.width*scaleX;
float scaledHeight = widgetSize.height*scaleY;
worldPosition.x += (widgetSize.width - scaledWidth) / 2;
worldPosition.y += (widgetSize.height - scaledHeight) / 2;
widgetSize.width = scaledWidth;
widgetSize.height = scaledHeight;
worldPosition = worldPosition + parent->getBoundingBox().origin;
parent = parent->getParent();
}
rect.origin = worldPosition;
rect.size= widgetSize;
return rect;
}
return rect;
}
static void setColorForAll(Sprite* target, Color3B color= Color3B::GRAY);
static void btnTouchBegin(Sprite* target, Point p, Color3B color = Color3B::GRAY);
static void btnTouchMoved(Sprite* target, Point p, Color3B color = Color3B::WHITE);
static bool btnTouchEnded(Sprite* target, Point p, Color3B color = Color3B::WHITE);
void AppUtils::setColorForAll(Sprite* target, Color3B color)
{
if (target != NULL)
{
target->setColor(color);
Vector<Node*> children = target->getChildren();
for (size_t i = 0; i < children.size(); i++)
{
Sprite* item = (Sprite*)children.at(i);
if (item != NULL)
{
item->setColor(color);
}
}
}
}
void AppUtils::btnTouchBegin(Sprite* target, Point p, Color3B color)
{
if (target != NULL&&target->boundingBox().containsPoint(p))
{
setColorForAll(target, color);
}
}
void AppUtils::btnTouchMoved(Sprite* target, Point p, Color3B color)
{
if (target != NULL&&!target->boundingBox().containsPoint(p) && (target->getColor()== Color3B::GRAY))
{
setColorForAll(target, color);
}
}
bool AppUtils::btnTouchEnded(Sprite* target, Point p, Color3B color)
{
if (target != NULL&&target->boundingBox().containsPoint(p) && (Color3B::GRAY==target->getColor()))
{
setColorForAll(target, color);
return true;
}
return false;
}
4、android下输出log常用宏
#pragma once
#include "cocos2d.h"
USING_NS_CC;
#if !defined(COCOS2D_DEBUG) || COCOS2D_DEBUG == 0
#define LOG_DEBUG(...) do {} while (0)
#define LOG_ERROR(...) do {} while (0)
#define LOG_INFO(...) do {} while (0)
#define LOG_WARN(...) do {} while (0)
#elif (COCOS2D_DEBUG == 1&&(CC_TARGET_PLATFORM == CC_PLATFORM_WIN32))
#define LOG_DEBUG(__TAG__,format, ...) log(__TAG__":"##format, ##__VA_ARGS__)
#define LOG_ERROR(__TAG__,format, ...) log(__TAG__":"##format, ##__VA_ARGS__)
#define LOG_INFO(__TAG__,format, ...) log(__TAG__":"##format, ##__VA_ARGS__)
#define LOG_WARN(__TAG__,format, ...) log(__TAG__":"##format, ##__VA_ARGS__)
#elif (COCOS2D_DEBUG == 1&&(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID))
#define LOG_DEBUG( __TAG__, ...) __android_log_print(ANDROID_LOG_DEBUG, __TAG__, __VA_ARGS__)
#define LOG_ERROR( __TAG__, ...) __android_log_print(ANDROID_LOG_ERROR, __TAG__, __VA_ARGS__)
#define LOG_INFO( __TAG__, ...) __android_log_print(ANDROID_LOG_INFO, __TAG__, __VA_ARGS__)
#define LOG_WARN( __TAG__, ...) __android_log_print(ANDROID_LOG_WARN, __TAG__, __VA_ARGS__)
#endif // COCOS2D_DEBUG