在android 上开发弹球例子 cocos2dx+box2d

create-android-project.sh 批处理新建 一个工程

名字TestBox ---》
android ---->注意是jni目录
Android.mk Application.mk
helloworld--》Android.mk main.cpp

Classes------>AppDelegate.cpp AppDelegate.h HelloWorldScene.cpp HelloWorldScene.h

Resources --->存放资源文件


Android.mk :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

subdirs := $(addprefix $(LOCAL_PATH)/../../../,$(addsuffix /Android.mk, \
Box2D \
cocos2dx \
CocosDenshion/android \
))
subdirs += $(LOCAL_PATH)/helloworld/Android.mk

include $(subdirs)



Application.mk :

APP_STL := gnustl_static
APP_CPPFLAGS += -frtti
APP_MODULES := cocos2d cocosdenshion box2d game



Android.mk :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := game

LOCAL_SRC_FILES := main.cpp \
../../../Classes/AppDelegate.cpp \
../../../Classes/HelloWorldScene.cpp


LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../../cocos2dx \
$(LOCAL_PATH)/../../../../cocos2dx/platform \
$(LOCAL_PATH)/../../../../cocos2dx/include \
$(LOCAL_PATH)/../../../../CocosDenshion/include \
$(LOCAL_PATH)/../../../../ \
$(LOCAL_PATH)/../../../../Box2D \
$(LOCAL_PATH)/../../../Classes

LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/../../libs/$(TARGET_ARCH_ABI)) \
-lGLESv1_CM \
-lbox2d \
-lcocos2d -llog -lcocosdenshion \

include $(BUILD_SHARED_LIBRARY)


上面是一些makefile的内容 可以根据自己的路径进行修改

HelloWorldScene.h:


#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "Box2D.h" //box2d 头 文 件
using namespace cocos2d;

class HelloWorld : public cocos2d::CCLayer // 创 建 HelloWorld 类 继 承 自 CCLayer 布 景
{

private:
b2World* _world; // 世 界
b2Body *_body; // 刚 体
CCSprite* _ball; // 精 灵

public:
~HelloWorld(); // 析 构 函 数
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();

// there's no 'id' in cpp, so we recommand to return the exactly class pointer
static cocos2d::CCScene* scene(); // 声 明 返 回 场 景 的 方 法

virtual void draw();

void tick(cocos2d::ccTime dt);

// a selector callback
virtual void menuCloseCallback(CCObject* pSender); // 菜 单 关 闭 方 法

// implement the "static node()" method manually
LAYER_NODE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__



HelloWorldScene.cpp


#include "HelloWorldScene.h"
#include "Box2D.h" //box2d 头 文 件

USING_NS_CC;

#define PTM_RATIO 32 // 这 里 定 义 了 一 个“ 像 素 / 米” 的 比 率
HelloWorld::~HelloWorld() // 回 收 资 源 释 放 内 存
{
CC_SAFE_DELETE(_world);
_body = NULL;
_world = NULL;
}

// on "init" you need to initialize your instance
bool HelloWorld::init() // init 方 法 在 布 景 中 加 入 各 种 元 素 菜 单 标 签 按 钮 精 灵
{
//
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}

/
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.

// add a "close" icon to exit the progress. it's an autorelease object
CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage( // 创 建 菜 单 项 目
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback) );
pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );

// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
pMenu->setPosition( CCPointZero );
this->addChild(pMenu, 1);

/
// 3. add your codes below...

// add a label shows "Hello World"
// create and initialize a label
// CCLabelTTF* pLabel = CCLabelTTF::labelWithString("Hello World", "Arial", 24);
// ask director the window size
CCSize winSize = CCDirector::sharedDirector()->getWinSize(); // 获 取 屏 幕 的 参 数

// position the label on the center of the screen
// pLabel->setPosition( ccp(size.width / 2, size.height - 50) );

// add the label as a child to this layer
// this->addChild(pLabel, 1);

// add "HelloWorld" splash screen" 创 建 篮 球 精 灵
_ball = CCSprite::spriteWithFile("Ball.jpg",CCRectMake(0, 0, 52, 52));

// position the sprite on the center of the screen
_ball->setPosition( ccp(100,100) );

// add the sprite as a child to this layer
this->addChild(_ball, 0);

/*************************************************************************************************************/

// Create a world
/**
创 建 重 力 方 向 , y 轴 为 -30
因 此,所 有 的 body 都 会 往 屏 幕 下 面 下 落 。 同 时 , 我 们 还 指 定 了 一 个 值 , 用 以 指 明 对 象 不 参 与 碰 撞 时 ,
是 否 可 以 “ 休 眠 ” 。 一 个 休 眠 的 对 象 将 不 会 花 费 处 理 时 间 , 直 到 它 与 其 实 对 象 发 生 碰 撞 的 时 候 才 会 “ 醒 ” 过 来 。
*/
b2Vec2 gravity;
gravity.Set(0.0f, -30.0f);
bool doSleep = true; // 刚 体 休 眠
_world = new b2World(gravity,doSleep); // 创 建 世 界

// Create edges around the entire screen // 在 屏 幕 创 建 边 框 刚 体
/**
1 首 先 创 建 一 个 body 定 义 结 构 体 , 并 且 指 定 它 应 该 放 在 左 下 角 。
2 然 后,使 用 world 对 象 来 创 建 body 对 象 。( 注 意, 这 里 一 定 要 使 用 world 对 象 来 创 建,不 能 直 接 new,因 为world 对 象 会 做一 些 内 存 管 理 操 作 。)
3 接 着,为 屏 幕 的 每 一 个 边 界 创 建 一 个 多 边 形 shape 。 这 些 “shape” 仅 仅 是 一些 线 段 。 注 意,我 们 把 像 素 转 换 成 了“meter”。通 过 除 以 之 前 定 义 的
比 率 来 实 现 的 。
4 再 创 建 一 个fixture 定 义 ,指 定 shape 为polygon shape 。
再 使 用 body 对 象 来 为 每 一 个shape 创 建 一 个fixture 对 象 。
注 意 :一 个 body 对 象 可 以 包 含 许 许 多 多 的 fixture 对 象 。
*/
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0); // 设 置 起 始 点
b2Body *groundBody = _world->CreateBody(&groundBodyDef);

b2PolygonShape groundBox; // 隐 形 形 状 和 四 个 边 框
b2FixtureDef boxShapeDef;
boxShapeDef.shape = &groundBox;
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&boxShapeDef);

groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&boxShapeDef);

groundBox.SetAsEdge(b2Vec2(0, winSize.height/PTM_RATIO),b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&boxShapeDef);

groundBox.SetAsEdge(b2Vec2(winSize.width/PTM_RATIO,winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&boxShapeDef);

/*************************************************************************************************************/

// Create ball body and shape 小 球 刚 体 定 义
// 1 我 们 指 定 body 的 类 型 为 dynamic body 。 默 认 值 是 static body,那 意 味 着 那 个body 不 能 被 移 动 也 不 会 参 与 仿 真。很 明 显 。
// 2 设 置 body 的 user data 属 性 为 篮 球 精 灵 。你 可 以 设 置 任 何 东 西,但 是,你 设 置 成 精 灵 会 很 方 便 , 特 别 是 当 两 个 body 碰 撞 的 时 候
// ,你 可 以 通 过 这 个 参 数 把 精 灵 对 象 取 出 来,然 后 做 一 些 逻 辑 处 理 。
// 3 创 建 圆 形
b2BodyDef ballBodyDef;
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(100/PTM_RATIO, 100/PTM_RATIO); // 设 置 位 置
ballBodyDef.userData = _ball;
_body = _world->CreateBody(&ballBodyDef); // 根 据 定 义 创 建

b2CircleShape circle; // 形 状 为 圆 形
circle.m_radius = 26.0/PTM_RATIO;

b2FixtureDef ballShapeDef; // 材 料 : 形 状 密 度 质 量
ballShapeDef.shape = &circle;
ballShapeDef.density = 1.0f;
ballShapeDef.friction = 0.2f;
ballShapeDef.restitution = 0.8f;
_body->CreateFixture(&ballShapeDef); // 创 建 有 材 料 的 刚 体

this->schedule( schedule_selector(HelloWorld::tick) ); // 调 用 更 新 方 法
// [self schedule:@selector(tick:)];

return true;
}

void HelloWorld::draw() // gl 画 图
{
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

//world->DrawDebugData();

// restore default GL states
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}

CCScene* HelloWorld::scene() // 返 回 一 个 场 景 实 现
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::node(); // 实 例 化 一 个 场 景 可 以 自 动 释 放 内 存 CCScene::node()

// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::node(); // 实 例 化 布 景 可 以 自 动 释 放 内 存

// add layer as a child to scene
scene->addChild(layer); // 在 场 景 中 添 加 布 景

// return the scene
return scene;
}

// 游 戏 逻 辑
void HelloWorld::tick(ccTime dt){
/**
调 用 world 对 象 的 step 方 法 ,这 样 它 就 可 以 进 行 物 理 仿 真 了
这 里 的 两 个 参 数 分 别 是 “ 速 度 迭 代 次 数 ” 和 “ 位 置 迭 代 次 数 ”- - 你 应 该 设 置 他 们 的 范 围 在 8-10 之 间 。
( 译 者 :这 里 的 数 字 越 小 ,精 度 越 小, 但 是 效 率 更 高 。 数 字 越 大 , 仿 真 越 精 确 , 但 同 时 耗 时 更 多 。 8 一 般 是 个 折 中 ,
如 果 学 过 数 值 分 析 , 应 该 知 道 迭 代 步 数 的 具 体 作 用)。
*/
_world->Step(dt, 10, 10);
/**
接 下 来 ,我 们 要 使 我 们 的 精 灵 匹 配 物 理 仿 真 。 因 此 ,我 们 遍 历 world 对 象 里 面 的 所 有 body ,
然 后 看 body 的 user data 属 性 是 否 为 空 , 如 果 不 为 空 , 就 可 以 强 制 转 换 成 精 灵 对 象 。
接 下 来 , 就 可 以 根 据 body 的 位 置 来 更 新 精 灵 的 位 置 了 。
*/
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite* ballData = (CCSprite*)b->GetUserData();
// CCPoint* point = CCPoint(ccp(b->GetPosition().x * PTM_RATIO,b->GetPosition().y * PTM_RATIO));
ballData->setPosition( CCPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO) );
ballData->setRotation( -1 * CC_RADIANS_TO_DEGREES(b->GetAngle()) );
}
}

}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
CCDirector::sharedDirector()->end(); // 结 束 方 法

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}


有注释 可以看
编译运行 即可
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值