Cocos2d Box2D 开发Android下的 Breakout 撞球游戏

本文介绍了如何在Android平台上使用Cocos2d和Box2D库开发一款类似Breakout的撞球游戏。从集成Box2D库,修改HelloWorldScene的源文件,复制资源,到生成可运行的程序,并添加触摸交互功能,逐步实现了一个具备音乐和互动性的简单游戏。
摘要由CSDN通过智能技术生成

网上有文章介绍如何构造 撞球游戏( http://www.raywenderlich.com/28602/intro-to-box2d-with-cocos2d-2-x-tutorial-bouncing-balls )

但是它是基于 Apple, 我通过实践构造了 Android 下项目并成功编译运行


现假设已经生成 Android 下的 Hello World 项目 (可以参见 分析Cocos2d Android 项目的生成和运行)

现在我们通过改写 HelloWorldScene.h, HelloWorldScene.cpp 生成撞球游戏


集成 box2d

Cocos 已经帮我们集成了 Box2D ( cocos2d-2.1rc0-x-2.1.2\external\Box2D ), 并且 build 到 cocos_extension_static, 最终生成的 libgame 会被自动 load, 因此我们直接使用就可以了

就是头文件有些问题, 用下面方法解决

修改Cocos3\proj.android\jni\Android.mk

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes

改为

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes  $(LOCAL_PATH)/../../../external/Box2D


修改文件

(参照 http://www.raywenderlich.com/28602/intro-to-box2d-with-cocos2d-2-x-tutorial-bouncing-balls) 

改动 HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "Box2D.h"
#define PTM_RATIO 32.0



class HelloWorld : public cocos2d::CCLayer
{
public:
    // 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();
    
    // a selector callback
    void menuCloseCallback(CCObject* pSender);

    // implement the "static node()" method manually
    CREATE_FUNC(HelloWorld);
	
	b2World *_world;
    b2Body *_body;
    cocos2d::CCSprite *_ball;

#endif // __HELLOWORLD_SCENE_H__

改动 HelloWorldScene.cpp

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

using namespace cocos2d;
using namespace CocosDenshion;

CCScene* HelloWorld::scene()
{
	// 'scene' is an autorelease object
	CCScene *scene = CCScene::create();
	
	// 'layer' is an autorelease object
	HelloWorld *layer = HelloWorld::create();

	// add layer as a child to scene
	scene->addChild(layer);

	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
	//
	// 1. super init first
	if ( !CCLayer::init() )
	{
		return false;
	}
	
	CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
	"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::create(pCloseItem, NULL);
	pMenu->setPosition( CCPointZero );
	this->addChild(pMenu, 1);

	
	
	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	CCSprite* _ball = CCSprite::create("ball.png");
	this->addChild(_ball, 0);
	_ball->setPosition( ccp(100,300) );
	
	
	b2Vec2 gravity(0.0f, -8.0f);
	_world = new b2World(gravity);


	b2BodyDef groundBodyDef;
	groundBodyDef.position.Set(0,0);


	b2Body *groundBody = _world->CreateBody(&groundBodyDef);
	b2EdgeShape groundEdge;
	
	b2FixtureDef boxShapeDef;
	boxShapeDef.shape = &groundEdge;

	//wall definitions
	//bottom
	groundEdge.Set(b2Vec2(0,0), b2Vec2(winSize.width /PTM_RATIO, 0));
	groundBody->CreateFixture(&boxShapeDef);
	
	// left
	groundEdge.Set(b2Vec2(0,0), b2Vec2(0,winSize.height/PTM_RATIO));
	groundBody->CreateFixture(&boxShapeDef);
	
	// top
	groundEdge.Set(b2Vec2(0, winSize.height/PTM_RATIO),
	b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
	groundBody->CreateFixture(&boxShapeDef);

	// right
	groundEdge.Set(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO),
	b2Vec2(winSize.width/PTM_RATIO, 0));
	groundBody->CreateFixture(&boxShapeDef);
	

	
	b2BodyDef ballBodyDef;
	ballBodyDef.type = b2_dynamicBody;
	ballBodyDef.position.Set(100/PTM_RATIO, 300/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);
	
	schedule(schedule_selector(HelloWorld::tick)); 	
	//schedule(schedule_selector(HelloWorld::kick), 5); 

	CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("background-music-aac.wav", true);
	return true;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
	CCDirector::sharedDirector()->end();

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

void HelloWorld::tick(float dt) 
{ 

	_world->Step(dt, 10, 10);
	for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {    
		if (b->GetUserData() != NULL) {
			CCSprite *ballData = (CCSprite *)b->GetUserData();
			ballData->setPosition(ccp(b->GetPosition().x * PTM_RATIO,
			b->GetPosition().y * PTM_RATIO));
			ballData->setRotation(-1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));
		}        
	}
}	


复制资源文件

把这些文件放到  you_project\Resources 下面


ball.png 我贴在下面, 别的文件都可以在 cocos2d-2.1rc0-x-2.1.2/samples/Cpp/SimpleGame 下找到



生成程序

先用NDK ./build_native.sh 编译重新生成 libgame.so

而后在 eclipse 重新生成 .apk, 并安装到设备上



你现在得到一个自动四处碰撞但却会慢慢停下来的球, 同时会播放背景音乐


加入触摸互动

HelloWorldScene.h 加入

class HelloWorld : public cocos2d::CCLayer {
...
	void kick(); 
	void registerWithTouchDispatcher();
	void ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event);};
...
}


HelloWorldScene.cpp

void HelloWorld::kick() 
{
	b2Vec2 force = b2Vec2(30, 30);
	_body->ApplyLinearImpulse(force,_body->GetPosition());
	
}


void HelloWorld::registerWithTouchDispatcher()
{
	// CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this,0,true);
	CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0);
}

void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event)
{
	// Choose one of the touches to work with
	CCTouch* touch = (CCTouch*)( touches->anyObject() );
	CCPoint location = touch->getLocation();

	CCLog("++++++++after  x:%f, y:%f", location.x, location.y);
	kick();
}

还要在  bool HelloWorld::init() enable touch

this->setTouchEnabled(true);


以后你每次触摸屏幕, 球将会强力弹一次.


最后你得到一个有音乐,有互动,画面平滑的简单游戏.





评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值