Cocos2d-x Box2D debugDraw 必须背景是有透明度的!

我使用的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);

}


贴上一些代码给同学们做参考 coco2d-x 2.0.4



实例代码这里下载:猛击这里


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值