我使用的coco2d-x 版本2.0.4
根据网上的资源学习box2d 绘制 debugDraw 没有看到绘制的debugDraw,死活没出来。
就因为这个原因:背景图片是jpg格式的,但是这个不影响什么。。因为jpg格式的图片coco2d-x会在内存中把它转换为png格式的纹理。
发现背景透明值是255就是没有透明度,不会看到debugDraw. 看看下面有趣的过程
1.加载透明度为255的背景的效果
hideGames = new CCArray();
// srand(time(NULL));
GB2ShapeCache::sharedGB2ShapeCache()->addShapesWithFile("buttonsbodys.plist");
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("chooselayerpics.plist");
setTouchEnabled(true);
setAccelerometerEnabled(true);
this->initPhysics();//初始化物理世界
CCSprite* bg =CCSprite::create("u16bg1.jpg");
bg->setPosition(ccp(512,384));
this->addChild(bg);
// bg->setOpacity(100);//注意这里
//幕布
screenCover = CCSprite::create("s170loading.png");
screenCover->setPosition(ccp(512,467));
this->addChild(screenCover,99999);
CCSprite* bg2 =CCSprite::createWithSpriteFrameName("u17bg2.png");
bg2->setPosition(ccp(512,746));
this->addChild(bg2);
模拟器效果
2.加载透明度为100的背景的效果
this->initPhysics();//初始化物理世界
CCSprite* bg =CCSprite::create("u16bg1.jpg");
bg->setPosition(ccp(512,384));
this->addChild(bg);
bg->setOpacity(100);//注意这里
模拟器效果
让我纠结一天的jpg的图片
这塔吗太奇怪了!
知道内在原因的朋友,告诉我这个是为什么??非常感谢!
bool ColorChooseGameLayer::init()
{
if (!CCLayer::init())
{
return false;
}
hideGames = new CCArray();
srand(time(NULL));
GB2ShapeCache::sharedGB2ShapeCache()->addShapesWithFile("buttonsbodys.plist");
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("chooselayerpics.plist");
setTouchEnabled(true);
setAccelerometerEnabled(true);
this->initPhysics();
//幕布
CCSprite* bg =CCSprite::create("u16bg1.jpg");
bg->setPosition(ccp(512,384));
this->addChild(bg);
bg->setOpacity(100);//注意这里
CCSprite* bg =CCSprite::create("u16bg1.jpg");
bg->setPosition(ccp(512,384));
this->addChild(bg);
CCSprite* bg2 =CCSprite::createWithSpriteFrameName("u17bg2.png");
bg2->setPosition(ccp(512,746));
this->addChild(bg2);
//home
CCMenuItemImage* homeItem =CCMenuItemImage::create("home.png","home.png",this,menu_selector(ColorChooseGameLayer::homeMenuClick));
CCMenu* homeMenu =CCMenu::createWithItem(homeItem);
homeMenu->setPosition(ccp(50,718));
this->addChild(homeMenu,1002);
for (int i =0; i<5; i++) {
b2Body* body =addButtonicon("u17button",ccp(512,425), i);
}
this->scheduleUpdate();
return true;
}
void ColorChooseGameLayer::draw()
{
//
// IMPORTANT:
// This is only for debug purposes
// It is recommend to disable it
//
CCLayer::draw();
ccGLEnableVertexAttribs(kCCVertexAttribFlag_Position );
kmGLPushMatrix();
m_world->DrawDebugData();
kmGLPopMatrix();
}
void ColorChooseGameLayer::update(float dt)
{
//It is recommended that a fixed time step is used with Box2D for stability
//of the simulation, however, we are using a variable time step here.
//You need to make an informed choice, the following URL is useful
//http://gafferongames.com/game-physics/fix-your-timestep/
int velocityIterations =8;
int positionIterations =1;
// Instruct the world to perform a single step of simulation. It is
// generally best to keep the time step and iterations fixed.
m_world->Step(dt, velocityIterations, positionIterations);
//Iterate over the bodies in the physics world
for (b2Body* b =m_world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() !=NULL) {
//Synchronize the AtlasSprites position and rotation with the corresponding body
CCSprite* myActor = (CCSprite*)b->GetUserData();
myActor->setPosition(CCPointMake( b->GetPosition().x *PTM_RATIO, b->GetPosition().y *PTM_RATIO) );
myActor->setRotation( -1 *CC_RADIANS_TO_DEGREES(b->GetAngle()) );
}
}
}
b2Body* ColorChooseGameLayer::addButtonicon(string name,cocos2d::CCPoint p,int index)
{
// int index = rand()%5 + 1;
char s[64] ={0};
sprintf(s,"%s%d",name.c_str(),index+1);
string sname =string(s);
// CCSprite *sprite = CCSprite::spriteWithFile((name+".png").c_str());
CCSprite* sprite =CCSprite::createWithSpriteFrameName((sname +".png").c_str());
sprite->setPosition(p);
addChild(sprite,2,index +1);
b2BodyDef bodyDef;
bodyDef.type =b2_dynamicBody;
bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
bodyDef.userData = sprite;
b2Body *body =m_world->CreateBody(&bodyDef);
// add the fixture definitions to the body
GB2ShapeCache *sc =GB2ShapeCache::sharedGB2ShapeCache();
// sc->addFixturesToBody(body, name.c_str());
// sprite->setAnchorPoint(sc->anchorPointForShape(name.c_str()));
sc->addFixturesToBody(body, sname);
sprite->setAnchorPoint(sc->anchorPointForShape(sname));
return body;
}
void ColorChooseGameLayer::initPhysics()
{
CCSize s =CCDirector::sharedDirector()->getWinSize();
b2Vec2 gravity;
gravity.Set(0.0f, -10.0f);
m_world =newb2World(gravity);
m_world->SetAllowSleeping(true);
m_world->SetContinuousPhysics(true);
m_debugDraw = new GLESDebugDraw(PTM_RATIO);
m_world->SetDebugDraw(m_debugDraw);
uint32 flags =0;
flags +=b2Draw::e_shapeBit;
flags +=b2Draw::e_jointBit;
flags +=b2Draw::e_aabbBit;
flags +=b2Draw::e_pairBit;
flags += b2Draw::e_centerOfMassBit;
m_debugDraw->SetFlags(flags);
// Define the ground body.
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);// bottom-left corner
// Call the body factory which allocates memory for the ground body
// from a pool and creates the ground box shape (also from a pool).
// The body is also added to the world.
b2Body* groundBody =m_world->CreateBody(&groundBodyDef);
// Define the ground box shape.
b2EdgeShape groundBox;
// bottom
groundBox.Set(b2Vec2(0,0),b2Vec2(s.width/PTM_RATIO,0));
groundBody->CreateFixture(&groundBox,0);
// top
groundBox.Set(b2Vec2(0,s.height/PTM_RATIO),b2Vec2(s.width/PTM_RATIO,s.height/PTM_RATIO));
groundBody->CreateFixture(&groundBox,0);
// left
groundBox.Set(b2Vec2(0,s.height/PTM_RATIO),b2Vec2(0,0));
groundBody->CreateFixture(&groundBox,0);
// right
groundBox.Set(b2Vec2(s.width/PTM_RATIO,s.height/PTM_RATIO),b2Vec2(s.width/PTM_RATIO,0));
groundBody->CreateFixture(&groundBox,0);
}
实例代码这里下载:猛击这里