发现问题:监听代码部分不能在onEnter()段书写
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
visibleSize=Director::getInstance()->getVisibleSize();
auto listener=EventListenerTouchOneByOne::create();
listener->onTouchBegan = [this](Touch *t,Event *){
this->addBall(t->getLocation());
return false;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,this);
return true;
}
void HelloWorld::addBall(float positionX,float positionY){
auto b=Sprite::create("GOODS_3201_65.png");
b->setPhysicsBody(PhysicsBody::createBox(b->getContentSize()));
b->setPosition(positionX,positionY);
addChild(b);
}
void HelloWorld::onEnter(){
Layer::onEnter();
addBall(visibleSize.width/2,visibleSize.height/2);
addEdges();
}
void HelloWorld::addBall(Vec2 position){
addBall(position.x,position.y);
}
void HelloWorld::addEdges(){
auto body=PhysicsBody::createEdgeBox(visibleSize,PHYSICSBODY_MATERIAL_DEFAULT,3);
auto edgeShape=Node::create();
edgeShape->setPhysicsBody(body);
edgeShape->setPosition(visibleSize.width/2,visibleSize.height/2);
addChild(edgeShape);
}