第二阶段:基础知识4
---Cocos2d-x 3.x中自带物理引擎使用教程
1.Cocos2d-x 3.x物理引擎使用介绍
2.Cocos2d-x 3.x中使用物理引擎创建有物理特性的scene
3.Cocos2d-x 3.x中使用物理引擎创建边界
4.Cocos2d-x 3.x中使用物理引擎创建物理元素
5.Cocos2d-x 3.x中使用物理引擎动态添加元素
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::Layer
{
private :
cocos2d::Size visibleSize;
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();
virtual void onEnter();
void addEdges();
void addBall( float positionX, float positionY);
void addBall(cocos2d::Vec2 position);
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
};
#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);
// '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 ;
}
visibleSize = Director::getInstance()->getVisibleSize();
return true ;
}
void HelloWorld::onEnter(){
Layer::onEnter();
addBall(visibleSize.width/ 2 , visibleSize.height/ 2 );
addEdges();
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [ this ](Touch *t,Event *){
this ->addBall(t->getLocation());
return false ;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this );
}
void HelloWorld::addBall(cocos2d::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);
}
void HelloWorld::addBall( float positionX, float positionY){
auto b = Sprite::create( "ball.png" );
b->setPhysicsBody(PhysicsBody::createBox(b->getContentSize()));
b->cocos2d::Node::setPosition(positionX, positionY);
addChild(b);
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox( "You pressed the close button. Windows Store Apps do not implement a close button." , "Alert" );
return ;
#endif
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit( 0 );
#endif
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
// '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 ;
}
visibleSize = Director::getInstance()->getVisibleSize();
return true ;
}
void HelloWorld::onEnter(){
Layer::onEnter();
addBall(visibleSize.width/ 2 , visibleSize.height/ 2 );
addEdges();
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [ this ](Touch *t,Event *){
this ->addBall(t->getLocation());
return false ;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this );
}
void HelloWorld::addBall(cocos2d::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);
}
void HelloWorld::addBall( float positionX, float positionY){
auto b = Sprite::create( "ball.png" );
b->setPhysicsBody(PhysicsBody::createBox(b->getContentSize()));
b->cocos2d::Node::setPosition(positionX, positionY);
addChild(b);
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox( "You pressed the close button. Windows Store Apps do not implement a close button." , "Alert" );
return ;
#endif
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit( 0 );
#endif
}