实现的功能:(分关说明)
主界面:
1.点击三个不同的按钮实现界面跳转。
第一二三关:
1.用简单的物理公式在update控制小球的运动,实时刷新小球的坐标。
2.使用box2d物理引擎来制作游戏场景,控制小球的运动以及碰撞。
3.点击退出按钮返回主界面。
游戏截图:
(嘿嘿,找了个巨配的背景)
然后就是点击第一关第二关第三关进入不同的关卡
第一关是没有使用box2d物理引擎的,大致的做法就是直接更改update里面小球的position。运用了一丢丢物理公式。
拖拽鼠标左键的时候可以控制小球的发射方向以及力度。
第一关没有使用物理引擎,所以小球之前、小球与物体的碰撞都需要自己写。
#include "DemoLayer.h"
#include "math.h"
#include "Box2DDemoLayer.h"
#include "HelloWorldScene.h"
#define MASS 0.4
#define Gravity ccp(0,-9.8*100*MASS)
#define Height 500
#define Width 640
#define RadiusOfBall 36
#define LossRateOfCollide 0.8
CCScene* DemoLayer::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
DemoLayer *layer = DemoLayer::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
bool DemoLayer::init()
{
if (!CCLayer::init())
{
return false;
}
//
size = CCDirector::sharedDirector()->getVisibleSize();
left = (size.width - Width) / 2;
right = (size.width + Width) / 2;
bottom = (size.height - Height) / 2;
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
this->setTouchEnabled(true);
CCLabelTTF* pLabel = CCLabelTTF::create("The first level(don't use Box2D)", "Arial", 24);
// position the label on the center of the screen
pLabel->setPosition(ccp(origin.x + visibleSize.width / 2, origin.y + visibleSize.height - pLabel->getContentSize().height));
// add the label as a child to this layer
this->addChild(pLabel, 1);
//按钮的代码
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"blackBtnbackground.png",//正常状态的图片,系统自带的
"blackBtnbackground.png",//被点击的图片
this,
menu_selector(DemoLayer::menuCloseCallback));
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width / 2 - 10,
origin.y + pCloseItem->getContentSize().height / 2 + 10));
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu);
//这里的退出按钮由于找不到合适的素材,我就用文本代替了,然后按钮的图片是用一张黑色的图片放在按钮上,所以看不出来。
CCLabelTTF* pLabel1 = CCLabelTTF::create("Exit", "Arial", 30);
pLabel1->setPosition(ccp(origin.x + visibleSize.width - 35, origin.y + pLabel1->getContentSize().height - 15));
this->addChild(pLabel1, 1);
//用纹理缓存(资源池)加载纹理 这里的纹理就是一个点
CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("texball.png");
//创建批处理渲染方式的精灵节点
CCSpriteBatchNode *mgr = CCSpriteBatchNode::createWithTexture(texture);
//添加到层中
this->addChild(mgr);
background = CCSprite::createWithTexture(texture, CCRectMake(0, 0, 640, 500));
//创建背景
background->setAnchorPoint(ccp(0, 0));
background->setPosition(ccp(left, bottom));
mgr->addChild(background);
//开始箭头位置
startArea = CCSprite::createWithTexture(texture, CCRectMake(647, 124, 100, 100));
startArea->setPosition(ccp(left + 80, bottom + 150));
mgr->addChild(startArea);
//箭头
arrow = CCSprite::createWithTexture(texture, CCRectMake(0, 500, 690, 120));
arrow->setPosition(startArea->getPosition());
arrow->setScale(0.1);
mgr->addChild(arrow);
//小球
ball = CCSprite::createWithTexture(texture, CCRectMake(654, 0, 72, 72));
ball->setPosition(startArea->getPosition());
mgr->addChild(ball);
//篮板
basketry = CCSprite::createWithTexture(texture, CCRectMake(656, 86, 114, 24));
basketry->setPosition(ccp(left + 550, bottom + 300));
mgr->addChild(basketry);
//篮筐
target = CCSprite::createWithTexture(texture, CCRectMake(648, 272, 16, 160));
target->setPosition(ccp(left + 600, bottom + 350));
mgr->addChild(target);
//铁三角
trangle = CCSprite::createWithTexture(texture, CCRectMake(760, 0, 90, 90));
trangle->setPosition(ccp(left + Width - 45, bottom + 45));
mgr->addChild(trangle);
isShoot = false;
//加速度为Gravity
acceleration = Gravity;
//篮筐左点和右点
basketryLeftPos = ccp(left + 493, bottom + 300);//篮筐左