#include "HelloWorldScene.h"
using namespace cocos2d;
enum {
//设定一个父节点的tag
kTagParentNode = 1,
};
enum {
Z_PHYSICS_DEBUG = 100,
};
HelloWorld::HelloWorld(){
}
CCScene* HelloWorld::scene()
{
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
scene = CCScene::create();
CC_BREAK_IF(! scene);
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
CC_BREAK_IF(! layer);
// add layer as a child to scene
scene->addChild(layer);
} while (0);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
bool bRet = false;
do
{
//////////////////////////////////////////////////////////////////////////
// super init first
//////////////////////////////////////////////////////////////////////////
CC_BREAK_IF(! CCLayer::init());
//////////////////////////////////////////////////////////////////////////
// add your codes below...
//////////////////////////////////////////////////////////////////////////
// 1. Add a menu item with "X" image, which is clicked to quit the program.
// Create a "close" menu item with close icon, it's an auto release object.
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
CC_BREAK_IF(! pCloseItem);
// Place the menu item bottom-right conner.
pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
// Create a menu with the "close" menu item, it's an auto release object.
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
CC_BREAK_IF(! pMenu);
// Add the menu to HelloWorld layer as a child layer.
this->addChild(pMenu, 1);
// 2. Add a label shows "Hello World".
// Create a label and initialize with string "Hello World".
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
CC_BREAK_IF(! pLabel);
// Get window size and place the label upper.
CCSize size = CCDirector::sharedDirector()->getWinSize();
pLabel->setPosition(ccp(size.width / 2, size.height - 50));
// Add the label to HelloWorld layer as a child layer.
this->addChild(pLabel, 1);
// 3. Add add a splash screen, show the cocos2d splash image.
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
CC_BREAK_IF(! pSprite);
// Place the sprite on the center of the screen
pSprite->setPosition(ccp(size.width/2, size.height/2));
// Add the sprite to HelloWorld layer as a child layer.
this->addChild(pSprite, 0);
#if CC_ENABLE_CHIPMUNK_INTEGRATION
// enable events
setTouchEnabled(true);
setAccelerometerEnabled(true);
// title
CCLabelTTF *label = CCLabelTTF::create("Multi touch the screen", "Arial", 36);
label->setPosition(ccp( VisibleRect::center().x, VisibleRect::top().y - 30));
this->addChild(label, -1);
// reset button
// init physics 物理引擎初始化
initPhysics();
#if 1
// Use batch node. Faster
CCSpriteBatchNode *parent = CCSpriteBatchNode::create("grossini_dance_atlas.png", 100);
m_pSpriteTexture = parent->getTexture();
#else
// doesn't use batch node. Slower
m_pSpriteTexture = CCTextureCache::sharedTextureCache()->addImage("grossini_dance_atlas.png");
CCNode *parent = CCNode::create();
#endif
//添加一个父节点
addChild(parent, 0, kTagParentNode);
//初始化时添加一个精灵
addNewSpriteAtPosition(ccp(200,200));
spriteType=0;
// menu for debug layer 设定物理引擎运行的模式用的菜单按钮
CCMenuItemFont::setFontSize(24);
CCMenuItemFont *item = CCMenuItemFont::create("Toggle debug", this, menu_selector(HelloWorld::toggleDebugCallback));
CCMenu *menu = CCMenu::create(item, NULL);
this->addChild(menu);
menu->setPosition(ccp(VisibleRect::right().x-100, VisibleRect::top().y-60));
//调用计时器,此类计时器只认update方法,也就是层里定义的那个
scheduleUpdate();
#else
CCLabelTTF *pLabel = CCLabelTTF::create("Should define CC_ENABLE_CHIPMUNK_INTEGRATION=1\n to run this test case",
"Arial",
18);
CCSize size = CCDirector::sharedDirector()->getWinSize();
pLabel->setPosition(ccp(size.width/2, size.height/2));
addChild(pLabel);
#endif
bRet = true;
} while (0);
return bRet;
}
//调试模式下可以看到为刚体定义的形状,否则形状会不可见
void HelloWorld::toggleDebugCallback(CCObject* pSender)
{
#if CC_ENABLE_CHIPMUNK_INTEGRATION
m_pDebugLayer->setVisible(! m_pDebugLayer->isVisible());
#endif
}
HelloWorld::~HelloWorld()
{
// manually Free rogue shapes
for( int i=0;i<4;i++) {
cpShapeFree( m_pWalls[i] );
}
cpSpaceFree( m_pSpace );
}
void HelloWorld::initPhysics()
{
#if CC_ENABLE_CHIPMUNK_INTEGRATION
// init chipmunk
//cpInitChipmunk();
//创建一个物理空间
m_pSpace = cpSpaceNew();
//设定空间的重力点
m_pSpace->gravity = cpv(0, -100);
//
// rogue shapes
// We have to free them manually
//这里创建了四个形状用于创建一个矩形的墙体,其对应的刚体是静态刚体,也就是保持静止不动的刚体
//cpSegmentShapeNew用来建立一个段状形状,其它定义请看源码
// bottom
m_pWalls[0] = cpSegmentShapeNew( m_pSpace->staticBody,
cpv(VisibleRect::leftBottom().x,VisibleRect::leftBottom().y),
cpv(VisibleRect::rightBottom().x, VisibleRect::rightBottom().y), 9.0f);
// top
m_pWalls[1] = cpSegmentShapeNew( m_pSpace->staticBody,
cpv(VisibleRect::leftTop().x, VisibleRect::leftTop().y),
cpv(VisibleRect::rightTop().x, VisibleRect::rightTop().y), 0.0f);
// left
m_pWalls[2] = cpSegmentShapeNew( m_pSpace->staticBody,
cpv(VisibleRect::leftBottom().x,VisibleRect::leftBottom().y),
cpv(VisibleRect::leftTop().x,VisibleRect::leftTop().y), 0.0f);
// right
m_pWalls[3] = cpSegmentShapeNew( m_pSpace->staticBody,
cpv(VisibleRect::rightBottom().x, VisibleRect::rightBottom().y),
cpv(VisibleRect::rightTop().x, VisibleRect::rightTop().y), 0.0f);
//为形状设定弹性及摩擦系数并加入到物理空间
for( int i=0;i<4;i++) {
m_pWalls[i]->e = 1.0f;
m_pWalls[i]->u = 1.0f;
cpSpaceAddStaticShape(m_pSpace, m_pWalls[i] );
}
// Physics debug layer
m_pDebugLayer = CCPhysicsDebugNode::create(m_pSpace);
this->addChild(m_pDebugLayer, Z_PHYSICS_DEBUG);
#endif
}
void HelloWorld::update(float delta)
{
// Should use a fixed size step based on the animation interval.
int steps = 2;
float dt = CCDirector::sharedDirector()->getAnimationInterval()/(float)steps;
//为物理空间赋予时间,这里是按全局的动画间隔时间来设定的
for(int i=0; i<steps; i++){
cpSpaceStep(m_pSpace, dt);
}
}
void HelloWorld::addNewSpriteAtPosition(CCPoint pos)
{
#if CC_ENABLE_CHIPMUNK_INTEGRATION
int posx, posy;
//获取节点
pParent = getChildByTag(kTagParentNode);
//随机取重心位置坐标
posx = CCRANDOM_0_1() * 200.0f;
posy = CCRANDOM_0_1() * 200.0f;
posx = (posx % 4) * 85;
posy = (posy % 3) * 121;
//设定刚体形状,这里定义了四个矢量点,所以形状为矩形
int num = 4;
cpVect verts[] = {
cpv(-24,-54),
cpv(-24, 54),
cpv( 24, 54),
cpv( 24,-54),
};
//创建刚体
cpBody *body = cpBodyNew(1.0f, cpMomentForPoly(1.0f, num, ver
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
collision-level2.rar (17个子文件)
main.h 270B
collision.win32.sln 896B
res
collision.ico 47KB
collision.rc 2KB
Resources
CloseSelected.png 5KB
CloseNormal.png 6KB
grossini_dance_atlas.png 8KB
HelloWorld.png 142KB
Classes
AppDelegate.h 960B
HelloWorldScene.h 3KB
AppDelegate.cpp 1KB
HelloWorldScene.cpp 14KB
collision.win32.vcxproj.filters 2KB
resource.h 498B
collision.win32.vcxproj.user 763B
collision.win32.vcxproj 8KB
main.cpp 863B
共 17 条
- 1
资源评论
- warmantang2013-08-19一般 没什么太多值得学习的
- trhy19902014-05-05稍微调整下跑起来了,学习了
cyistudio
- 粉丝: 129
- 资源: 36
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功