Cocos2d-x加Box2D制作弹弓类游戏
来源:互联网 发布:电脑pdf软件 编辑:程序博客网 时间:2024/05/01 00:23
怎么用旋转关节(rotation joints)
怎么用连接关节(weld joints)
怎么让视角跟随抛射物
怎么根据碰撞检测判断力量来消除敌人
和很多其他的
CCSprite *sprite = CCSprite::spriteWithFile("bg.png"); //背景图
sprite->setAnchorPoint(CCPointZero);
this->addChild(sprite, -1);
CCSprite *sprite = CCSprite::spriteWithFile("catapult_base_2.png"); //投射器底部后面那块
sprite->setAnchorPoint(CCPointZero);
sprite->setPosition(CCPointMake(181.0, FLOOR_HEIGHT));
this->addChild(sprite, 0);
sprite = CCSprite::spriteWithFile("squirrel_1.png"); //左边松鼠
sprite->setAnchorPoint(CCPointZero);
sprite->setPosition(CCPointMake(11.0, FLOOR_HEIGHT));
this->addChild(sprite, 0);
sprite = CCSprite::spriteWithFile("catapult_base_1.png"); //投射器底部前面那块
sprite->setAnchorPoint(CCPointZero);
sprite->setPosition(CCPointMake(181.0, FLOOR_HEIGHT));
this->addChild(sprite, 9);
sprite = CCSprite::spriteWithFile("squirrel_2.png"); //右边松鼠
sprite->setAnchorPoint(CCPointZero);
sprite->setPosition(CCPointMake(240.0, FLOOR_HEIGHT));
this->addChild(sprite, 9);
sprite = CCSprite::spriteWithFile("fg.png"); //带冰的地面
sprite->setAnchorPoint(CCPointZero);
this->addChild(sprite, 10);
#define FLOOR_HEIGHT 62.0f
private:
b2World* m_world;
b2Body* m_groundBody;
b2Vec2 gravity;
gravity.Set(0.0f, -10.0f);
bool doSleep = true;
m_world = new b2World(gravity);
m_world->SetAllowSleeping(doSleep);
m_world->SetContinuousPhysics(true);
// Define the ground body.
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0, 0); // bottom-left corner
// Call the body factory which allocates memory for the ground body
// from a pool and creates the ground box shape (also from a pool).
// The body is also added to the world.
m_groundBody = m_world->CreateBody(&groundBodyDef);
b2EdgeShape groundBox;
// bottom
groundBox.Set(b2Vec2(0,FLOOR_HEIGHT/PTM_RATIO), b2Vec2(screenSize.width*2.0f/PTM_RATIO,FLOOR_HEIGHT/PTM_RATIO));
m_groundBody->CreateFixture(&groundBox, 0);
// top
groundBox.Set(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width*2.0f/PTM_RATIO,screenSize.height/PTM_RATIO));
m_groundBody->CreateFixture(&groundBox, 0);
// left
groundBox.Set(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(0,0));
m_groundBody->CreateFixture(&groundBox, 0);
// right
groundBox.Set(b2Vec2(screenSize.width*1.5f/PTM_RATIO,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width*1.5f/PTM_RATIO,0));
m_groundBody->CreateFixture(&groundBox, 0);
private:
b2Fixture *m_armFixture;
b2Body *m_armBody;
// Create the catapult's arm
CCSprite *arm = CCSprite::spriteWithFile("catapult_arm.png");
this->addChild(arm, 1);
b2BodyDef armBodyDef;
armBodyDef.type = b2_dynamicBody;
armBodyDef.linearDamping = 1;
armBodyDef.angularDamping = 1;
armBodyDef.position.Set(230.0f/PTM_RATIO, (FLOOR_HEIGHT+91.0f)/PTM_RATIO);
armBodyDef.userData = arm;
m_armBody = m_world->CreateBody(&armBodyDef);
b2PolygonShape armBox;
b2FixtureDef armBoxDef;
armBoxDef.shape = &armBox;
armBoxDef.density = 0.3F;
armBox.SetAsBox(11.0f/PTM_RATIO, 91.0f/PTM_RATIO);
m_armFixture = m_armBody->CreateFixture(&armBoxDef);
void tick(cocos2d::ccTime dt);
void HelloWorld::tick(ccTime dt)
{
int velocityIterations = 8;
int positionIterations = 1;
m_world->Step(dt, velocityIterations, positionIterations);
//Iterate over the bodies in the physics world
for (b2Body* b = m_world->GetBodyList(); b; b = b->GetNext())
{
if (b->GetUserData() != NULL) {
//Synchronize the AtlasSprites position and rotation with the corresponding body
CCSprite* myActor = (CCSprite*)b->GetUserData();
myActor->setPosition( CCPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO) );
myActor->setRotation( -1 * CC_RADIANS_TO_DEGREES(b->GetAngle()) );
}
}
}
schedule(schedule_selector(Catapult::tick));
b2RevoluteJoint *m_armJoint;
b2RevoluteJointDef armJointDef;
armJointDef.Initialize(m_groundBody, m_armBody, b2Vec2(233.0f/PTM_RATIO, FLOOR_HEIGHT/PTM_RATIO));
armJointDef.enableMotor = true;
armJointDef.enableLimit = true;
armJointDef.motorSpeed = -10; //-1260;
armJointDef.lowerAngle = CC_DEGREES_TO_RADIANS(9);
armJointDef.upperAngle = CC_DEGREES_TO_RADIANS(75);
armJointDef.maxMotorTorque = 700;
m_armJoint = (b2RevoluteJoint*)m_world->CreateJoint(&armJointDef);
private:
b2MouseJoint *m_mouseJoint;
public:
virtualvoid ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
virtualvoid ccTouchesMoved(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
virtualvoid ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
void Catapult::ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event)
{
if (m_mouseJoint != NULL) { return; }
CCTouch *touch = (CCTouch *)touches->anyObject();
CCPoint location = touch->locationInView(touch->view());
location = CCDirector::sharedDirector()->convertToGL(location);
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
if (locationWorld.x < m_armBody->GetWorldCenter().x + 150.0/PTM_RATIO)
{
b2MouseJointDef md;
md.bodyA = m_groundBody;
md.bodyB = m_armBody;
md.target = locationWorld;
md.maxForce = 2000;
m_mouseJoint = (b2MouseJoint *)m_world->CreateJoint(&md);
}
}
m_mouseJoint = NULL;
void Catapult::ccTouchesMoved(cocos2d::CCSet* touches, cocos2d::CCEvent* event)
{
if (m_mouseJoint == NULL) { return; }
CCTouch *touch = (CCTouch *)touches->anyObject();
CCPoint location = touch->locationInView(touch->view());
location = CCDirector::sharedDirector()->convertToGL(location);
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
m_mouseJoint->SetTarget(locationWorld);
}
void Catapult::ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event)
{
if (m_mouseJoint != NULL)
{
m_world->DestroyJoint(m_mouseJoint);
m_mouseJoint = NULL;
return;
}
}
std::vector<b2Body *> m_bullets;
int m_currentBullet;
void Catapult::createBullets(int count)
{
m_currentBullet = 0;
float pos = 62.0f;
if (count > 0)
{
// delta is the spacing between corns
// 62 is the position o the screen where we want the corns to start appearing
// 165 is the position on the screen where we want the corns to stop appearing
// 30 is the size of the corn
float delta = (count > 1)?((165.0f - 62.0f - 30.0f) / (count - 1)):0.0f;
for (int i=0; i<count; i++, pos += delta)
{
// Create the bullet
CCSprite *sprite = CCSprite::spriteWithFile("acorn.png");
this->addChild(sprite, 1);
b2BodyDef bulletBodyDef;
bulletBodyDef.type = b2_dynamicBody;
bulletBodyDef.bullet = true;
bulletBodyDef.position.Set(pos/PTM_RATIO,(FLOOR_HEIGHT+15.0f)/PTM_RATIO);
bulletBodyDef.userData = sprite;
b2Body *bullet = m_world->CreateBody(&bulletBodyDef);
bullet->SetActive(false);
b2CircleShape circle;
circle.m_radius = 15.0/PTM_RATIO;
b2FixtureDef ballShapeDef;
ballShapeDef.shape = &circle;
ballShapeDef.density = 0.8f;
ballShapeDef.restitution = 0.2f;
ballShapeDef.friction = 0.99f;
bullet->CreateFixture(&ballShapeDef);
m_bullets.push_back(bullet);
}
}
}
b2Body *m_bulletBody;
b2WeldJoint *m_bulletJoint;
bool Catapult::attachBullet()
{
if (m_currentBullet < m_bullets.size())
{
m_bulletBody = (b2Body*)m_bullets.at(m_currentBullet++);
m_bulletBody->SetTransform(b2Vec2(230.0f/PTM_RATIO, (155.0f+FLOOR_HEIGHT)/PTM_RATIO), 0.0f);
m_bulletBody->SetActive(true);
b2WeldJointDef weldJointDef;
weldJointDef.Initialize(m_bulletBody, m_armBody, b2Vec2(230.0f/PTM_RATIO,(155.0f+FLOOR_HEIGHT)/PTM_RATIO));
weldJointDef.collideConnected = false;
m_bulletJoint = (b2WeldJoint*)m_world->CreateJoint(&weldJointDef);
returntrue;
}
returnfalse;
}
void Catapult::resetGame( )
{
this->createBullets(4);
this->attachBullet();
}
CCCallFunc *callSelectorAction = CCCallFunc::actionWithTarget(this, callfunc_selector(HelloWorld::resetGame));
this->runAction(CCSequence::actions(
callSelectorAction,
NULL));
CCDelayTime *delayAction = CCDelayTime::actionWithDuration(0.2f);
CCCallFunc *callSelectorAction = CCCallFunc::actionWithTarget(this, callfunc_selector(HelloWorld::resetGame));
this->runAction(CCSequence::actions(delayAction,
callSelectorAction,
NULL));
bool m_releasingArm;
void Catapult::ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event)
{
if (m_mouseJoint != NULL)
{
if (m_armJoint->GetJointAngle() >= CC_DEGREES_TO_RADIANS(5))
{
m_releasingArm = true;
}
m_world->DestroyJoint(m_mouseJoint);
m_mouseJoint = NULL;
return;
}
}
m_releasingArm = false;
// Arm is being released
if (m_releasingArm && m_bulletJoint != NULL)
{
// Check if the arm reached the end so we can return the limits
if (m_armJoint->GetJointAngle() <= CC_DEGREES_TO_RADIANS(10))
{
m_releasingArm = false;
// Destroy joint so the bullet will be free
m_world->DestroyJoint(m_bulletJoint);
m_bulletJoint = NULL;
}
}
armJointDef.maxMotorTorque = 700;//4800
// Bullet is moving.
if (m_bulletBody && m_bulletJoint == NULL)
{
b2Vec2 position = m_bulletBody->GetPosition();
CCPoint myPosition = this->getPosition();
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
// Move the camera.
if (position.x > screenSize.width / 2.0f / PTM_RATIO)
{
myPosition.x = -MIN(screenSize.width * 2.0f - screenSize.width, position.x * PTM_RATIO - screenSize.width / 2.0f);
this->setPosition(myPosition);
}
}
- Cocos2d-x加Box2D制作弹弓类游戏
- Cocos2d-x加Box2D制作弹弓类游戏
- (转)Cocos2d-x加Box2D制作弹弓类游戏
- Cocos2d-x加Box2D制作弹弓类游戏
- cocos2d-x加box2d制作弹弓类游戏 第二部分
- Cocos2d-x加Box2D制作弹弓类游戏
- Cocos2d-x加Box2D制作弹弓类游戏(第一部分)
- Cocos2d-x加Box2D制作弹弓类游戏(第二部分)
- 用Box2D和cocos2d-x制作弹弓类游戏
- 使用Box2d和cocos2d-x从头开始制作一个弹弓类游戏系列教材的第二部分
- cocos2d-x 使用Box2d制作的台球游戏
- Android基于box2d开发弹弓类游戏[0]
- Android基于box2d开发弹弓类游戏[四]-------------创建发射器
- Android基于box2d开发弹弓类游戏[五]-------------发射子弹
- Android基于box2d开发弹弓类游戏[二]-------------游戏界面的搭建&移动游戏场景
- box2d初级教程之一cocos2d box2d 制作弹球游戏
- <cocos2d-x for wp7>使用cocos2d-x和BOX2D来制作一个BreakOut(打砖块)游戏(一)
- <cocos2d-x for wp7>使用cocos2d-x和BOX2D来制作一个BreakOut(打砖块)游戏(二)
- 6、13iOS项目代码例子微博、地图、淘宝、豆瓣、指南针
- 多线程,进程与线程的解释
- iOS之第三方FMDB框架操作数据库
- iPhone/iPad 屏幕旋屏 iphone / ipod deployment info 调整
- win7下搭建cocos2d-x androi开发环境不用cygwin的方法
- Cocos2d-x加Box2D制作弹弓类游戏
- iOS解决TextField被键盘遮住的问题
- C++第一章定义构造函数
- iOS一些代码小例子
- win7下安装虚拟机详细教程
- 在C++中引用与指针的区别
- eclipse下运行Android版cocos2d-x
- 第二章继承与派生
- 第三章多态