我写完之后一运行
[img pic_type= width= height=]file:///D:\Documents\Tencent Files\364210582\Image\Group\3NJ@JT}A86)00VIZI88CWSN.png[/img]
然后我就一点一点试 好像是这几行代码出的问题

但是不知道怎么解决啊 求解决
//源码
#include "cocos2d.h"
#include "Box.h"
#include "cocos-ext.h"
#include "renderer/CCRenderer.h"
#include "renderer/CCCustomCommand.h"
USING_NS_CC_EXT;
#define PTM_RATIO 32
enum
{
KTagParentNode = 1
};
Scene* Box::scene()
{
auto scen = Scene::create();
scen->addChild(Box::create());
return scen;
}
bool Box::init()
{
if (!Layer::init())
{
return false;
}
initPhysics();
auto blocks = SpriteBatchNode::create("Imager/blocks.png", 100);
m_pSpriteTexture = blocks->getTexture();
addChild(blocks, 0, KTagParentNode);
auto dispatcher = Director::getInstance()->getEventDispatcher();
auto touchListener = EventListenerTouchAllAtOnce::create();
touchListener->onTouchesEnded = CC_CALLBACK_2(Box::onTouchesEnded, this);
dispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
return true;
}
void Box::initPhysics()
{
b2Vec2 b2;
b2.Set(0.0, -10);
world = new b2World(b2);
world->SetAllowSleeping(true);
world->SetContinuousPhysics(true);
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0, 0);
b2Body* groundBody = world->CreateBody(&groundBodyDef);
b2EdgeShape groundBox;
// bottom
groundBox.Set(b2Vec2(0,0), b2Vec2(480 / PTM_RATIO,0));
groundBody->CreateFixture(&groundBox, 0);
// top
groundBox.Set(b2Vec2(0, 320 / PTM_RATIO), b2Vec2(480 / PTM_RATIO, 320 / PTM_RATIO));
groundBody->CreateFixture(&groundBox, 0);
// left
groundBox.Set(b2Vec2(0, 320/ PTM_RATIO), b2Vec2(0, 0));
groundBody->CreateFixture(&groundBox, 0);
// right
groundBox.Set(b2Vec2(480 / PTM_RATIO, 320/ PTM_RATIO), b2Vec2(480/ PTM_RATIO, 320 / PTM_RATIO));
groundBody->CreateFixture(&groundBox, 0);
}
void Box::update(float s)
{
}
void Box::addNewSpriteAtPosition(Point p)
{
// Define the dynamic body.
//Set up a 1m squared box in the physics world
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(p.x / PTM_RATIO, p.y / PTM_RATIO);
b2Body *body = world->CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
body->CreateFixture(&fixtureDef);
auto parent = this->getChildByTag(KTagParentNode);
int idx = (CCRANDOM_0_1() > .5 ? 0 : 1);
int idy = (CCRANDOM_0_1() > .5 ? 0 : 1);
auto sprite = PhysicsSprite::createWithTexture(m_pSpriteTexture, Rect(32 * idx, 32 * idy, 32, 32));
parent->addChild(sprite);
sprite->setB2Body(body);
sprite->setPTMRatio(PTM_RATIO);
sprite->setPosition(ccp(p.x, p.y));
}
void Box::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
{
//Add a new body/atlas sprite at the touched location
MessageBox("1", "2");
for (auto& touch : touches)
{
if (!touch)
break;
Point location = touch->getLocation();
addNewSpriteAtPosition(location);
}
}
[img pic_type= width= height=]file:///D:\Documents\Tencent Files\364210582\Image\Group\3NJ@JT}A86)00VIZI88CWSN.png[/img]

然后我就一点一点试 好像是这几行代码出的问题

但是不知道怎么解决啊 求解决
//源码
#include "cocos2d.h"
#include "Box.h"
#include "cocos-ext.h"
#include "renderer/CCRenderer.h"
#include "renderer/CCCustomCommand.h"
USING_NS_CC_EXT;
#define PTM_RATIO 32
enum
{
KTagParentNode = 1
};
Scene* Box::scene()
{
auto scen = Scene::create();
scen->addChild(Box::create());
return scen;
}
bool Box::init()
{
if (!Layer::init())
{
return false;
}
initPhysics();
auto blocks = SpriteBatchNode::create("Imager/blocks.png", 100);
m_pSpriteTexture = blocks->getTexture();
addChild(blocks, 0, KTagParentNode);
auto dispatcher = Director::getInstance()->getEventDispatcher();
auto touchListener = EventListenerTouchAllAtOnce::create();
touchListener->onTouchesEnded = CC_CALLBACK_2(Box::onTouchesEnded, this);
dispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
return true;
}
void Box::initPhysics()
{
b2Vec2 b2;
b2.Set(0.0, -10);
world = new b2World(b2);
world->SetAllowSleeping(true);
world->SetContinuousPhysics(true);
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0, 0);
b2Body* groundBody = world->CreateBody(&groundBodyDef);
b2EdgeShape groundBox;
// bottom
groundBox.Set(b2Vec2(0,0), b2Vec2(480 / PTM_RATIO,0));
groundBody->CreateFixture(&groundBox, 0);
// top
groundBox.Set(b2Vec2(0, 320 / PTM_RATIO), b2Vec2(480 / PTM_RATIO, 320 / PTM_RATIO));
groundBody->CreateFixture(&groundBox, 0);
// left
groundBox.Set(b2Vec2(0, 320/ PTM_RATIO), b2Vec2(0, 0));
groundBody->CreateFixture(&groundBox, 0);
// right
groundBox.Set(b2Vec2(480 / PTM_RATIO, 320/ PTM_RATIO), b2Vec2(480/ PTM_RATIO, 320 / PTM_RATIO));
groundBody->CreateFixture(&groundBox, 0);
}
void Box::update(float s)
{
}
void Box::addNewSpriteAtPosition(Point p)
{
// Define the dynamic body.
//Set up a 1m squared box in the physics world
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(p.x / PTM_RATIO, p.y / PTM_RATIO);
b2Body *body = world->CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
body->CreateFixture(&fixtureDef);
auto parent = this->getChildByTag(KTagParentNode);
int idx = (CCRANDOM_0_1() > .5 ? 0 : 1);
int idy = (CCRANDOM_0_1() > .5 ? 0 : 1);
auto sprite = PhysicsSprite::createWithTexture(m_pSpriteTexture, Rect(32 * idx, 32 * idy, 32, 32));
parent->addChild(sprite);
sprite->setB2Body(body);
sprite->setPTMRatio(PTM_RATIO);
sprite->setPosition(ccp(p.x, p.y));
}
void Box::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
{
//Add a new body/atlas sprite at the touched location
MessageBox("1", "2");
for (auto& touch : touches)
{
if (!touch)
break;
Point location = touch->getLocation();
addNewSpriteAtPosition(location);
}
}









