首次启动的时候,两个物理碰撞时,触发一次回调函数,但是当切换到别的场景,然后再切换回来,每次碰撞触发2次,再切换一次,就变3次,以此类推,每次切换一次场景就多一次触发。
这是最终的界面:
这是第一次启动的时候的log,每触碰一次就调用一次回调函数:
![]()
当切换一次场景后再切换回来后,每次触碰就回调两次:
下面附代码,望大家帮忙看看怎么回事。
//头文件
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menubackCallback(cocos2d::Ref* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
//phy call backs
bool contactBegin(cocos2d::PhysicsContact &contact);
};
class AnotherScene : public cocos2d::Layer {
public:
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(AnotherScene);
void menubackCallback(cocos2d::Ref* pSender);
};
#endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
scene->getPhysicsWorld()->setGravity(Vect(0, -10));
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menubackCallback, this));
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
/////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
// position the label on the center of the screen
label->setPosition(Vec2(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - label->getContentSize().height));
// add the label as a child to this layer
this->addChild(label, 1);
// add "HelloWorld" splash screen"
auto sprite = Sprite::create("HelloWorld.png");
// position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
this->addChild(sprite, 0);
//物理世界测试
auto obj1 = Sprite::create("CloseNormal.png");
addChild(obj1);
obj1->setPosition(Vec2(200, 500));
auto obj2 = Sprite::create("CloseNormal.png");
addChild(obj2);
obj2->setPosition(Vec2(200, 300));
auto phy1 = PhysicsBody::createCircle(obj1->getBoundingBox().size.width / 2);
phy1->setCategoryBitmask(1);
phy1->setCollisionBitmask(1);
phy1->setContactTestBitmask(1);
auto phy2 = PhysicsBody::createEdgeSegment(Vec2(-obj2->getBoundingBox().size.width/2, 0), Vec2(obj2->getBoundingBox().size.width/2, 0));
phy2->setCategoryBitmask(1);
phy2->setCollisionBitmask(1);
phy2->setContactTestBitmask(1);
obj1->setPhysicsBody(phy1);
obj2->setPhysicsBody(phy2);
auto phyListener = EventListenerPhysicsContact::create();
phyListener->onContactBegin = CC_CALLBACK_1(HelloWorld::contactBegin, this);
_eventDispatcher->addEventListenerWithFixedPriority(phyListener, 1);
return true;
}
void HelloWorld::menubackCallback(Ref* pSender)
{
Director::getInstance()->replaceScene(AnotherScene::createScene());
}
bool HelloWorld::contactBegin(PhysicsContact &contact) {
CCLOG("contact begin!");
return true;
}
// scene 2
Scene* AnotherScene::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = AnotherScene::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
bool AnotherScene::init() {
//////////////////////////////
// 1. super init first
if (!Layer::init())
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(AnotherScene::menubackCallback, this));
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width / 2,
origin.y + closeItem->getContentSize().height / 2));
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
return true;
}
void AnotherScene::menubackCallback(Ref* pSender)
{
Director::getInstance()->replaceScene(HelloWorld::createScene());
}

