Rain.h
#ifndef Rain_H
#define Rain_H
#include “cocos2d.h”
#include “Global.h”
using namespace cocos2d;
class Rain:public CCNode
{
private:
CCSprite* pSprite;
int iColor;
float fSpeedH;
public:
float x,y;
float fSpriteW,fSpriteH;
bool isDie;
public:
Rain();
~Rain();
static Rain* rain();
bool init();
void update();
void addRain();
CREATE_FUNC(Rain);
};
#endif
Rain.cpp
#include “Rain.h”
using namespace cocos2d;
Rain::Rain()
{
}
Rain::~Rain()
{
}
Rain* Rain::rain()
{
Rain *rain = new Rain();
if(rain && rain->init())
{
rain->autorelease();
return rain;
}
CC_SAFE_DELETE(rain);
return NULL;
}
bool Rain::init()
{
bool bRet = false;
do{
bRet = true;
}while(0);
return bRet;
}
void Rain::update()
{
y -= fSpeedH;
this->setPosition(ccp(x,y));
}
void Rain::addRain()
{
iColor = CCRANDOM_0_1() * 3 - 0.1f;
fSpeedH = size.height / (CCRANDOM_0_1() * 3 + 1.0f);
pSprite = CCSprite::create(“rain.png”);
switch(iColor)
{
case 0:
pSprite->setColor(ccGREEN);
break;
case 1:
pSprite->setColor(ccYELLOW);
break;
case 2:
pSprite->setColor(ccBLUE);
}
fSpriteW = pSprite->getContentSize().width;
fSpriteH = pSprite->getContentSize().height;
isDie = false;
this->addChild(pSprite);
y = 480.0f;
x = CCRANDOM_0_1() * size.width;
this->setPosition(ccp(x,y));
}
这个是游戏主体里面调用的函数
void GameBody::addRain(float dt)
{
Rain* rain = Rain::rain();
rain->addRain();
this->addChild(rain);
pRain->addObject(rain);
}
但是一旦添加后,就直接黑屏,什么也没有了,找了一晚上没有找到,特来求助,麻烦大家了。。。