#include "HelloWorldScene.h"
using namespace cocos2d;
#define PTM_RATIO 32.0
HelloWorld::HelloWorld()
{
//b2Vec2 gravity;
//g//ravity.Set(0.0f, -10.0f);
//world = new b2World(gravity);
body = NULL;
}
HelloWorld::~HelloWorld()
{
delete world;
world = NULL;
//delete body;
//body = NULL;
}
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());
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCLabelTTF *title = CCLabelTTF::create("Boxing", "Arial", 24);//labelWithString("Boxing", "Arial", 24);
title->setColor(ccc3(255,255,255));
title->setPosition(CCPoint(winSize.width / 2, winSize.height - 50));
//title.pos = new CCPoint(winSize.width / 2, winSize.height - 50);
this->addChild(title, 1);
ball = CCSprite::create("ball.png");
ball->setPosition(CCPoint(100, 300));
this->addChild(ball);
b2Vec2 gravity;
gravity.Set(0.0f, -30.0f);
bool doSleep = true;
world = new b2World(gravity);
world->SetAllowSleeping(doSleep);
b2BodyDef groundBodyDef;
groundBodyDef.position = b2Vec2(0, 0);
b2Body *groundBody = world->CreateBody(&groundBodyDef);
b2EdgeShape groundBox;
b2FixtureDef boxShapeDef;
boxShapeDef.shape = &groundBox;
//shape.Set(b2Vec2(-20.0f, 0.0f), b2Vec2(20.0f, 0.0f));
groundBox.Set(b2Vec2(0, 0), b2Vec2((float)(winSize.width / PTM_RATIO), 0));
groundBody->CreateFixture(&boxShapeDef);
groundBox.Set(b2Vec2(0, 0), b2Vec2(0, (float)(winSize.height / PTM_RATIO)));
groundBody->CreateFixture(&boxShapeDef);
groundBox.Set(b2Vec2(0, (float)(winSize.height / PTM_RATIO)),
b2Vec2((float)(winSize.width / PTM_RATIO), (float)(winSize.height / PTM_RATIO)));
groundBody->CreateFixture(&boxShapeDef);
groundBox.Set(b2Vec2((float)(winSize.width / PTM_RATIO), (float)(winSize.height / PTM_RATIO)),
b2Vec2((float)(winSize.width / PTM_RATIO), 0));
groundBody->CreateFixture(&boxShapeDef);
b2BodyDef ballBodyDef;
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position = b2Vec2((float)(100 / PTM_RATIO), (float)(300 / PTM_RATIO));
ballBodyDef.userData = ball;
body = world->CreateBody(&ballBodyDef);
b2CircleShape circle;
circle.m_radius = (float)(26.0 / PTM_RATIO);
b2FixtureDef ballShapeDef;
ballShapeDef.shape = &circle;
ballShapeDef.density = 1.0f;
ballShapeDef.friction = 0.0f;
ballShapeDef.restitution = 1.0f;
body->CreateFixture(&ballShapeDef);
this->scheduleUpdate();
//////////////////////////////////////////////////////////////////////////
// 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);
bRet = true;
} while (0);
return bRet;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
// "close" menu item clicked
CCDirector::sharedDirector()->end();
}
void HelloWorld::update(float dt)
{
world->Step(dt, 10, 10);
for (b2Body *b = world->GetBodyList(); b != NULL;b = b->GetNext() )
{
if (b->GetUserData() != NULL)
{
CCSprite *ballData = (CCSprite*)(b->GetUserData());
ballData->setPosition(CCPoint((float)(b->GetPosition().x * PTM_RATIO),
(float)(b->GetPosition().y * PTM_RATIO)));
//ballData.setRotation(-1 * MathHelper.ToDegrees(b.GetAngle()));
}
}
/*CCArray *bgSprites = this->getChildren();
CCObject *obj = NULL;
CCARRAY_FOREACH(bgSprites, obj)
{
movingBackground(obj, dt);
} */
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页