cocos2dx 发射子弹2dx飞机大战怎么添加两列子弹

Cocos2d-x--全民飞机大战弹窗回弹效果
用HelloCpp工程修改代码:
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayer
virtual bool init();
static cocos2d::CCScene* scene();
CREATE_FUNC(HelloWorld);
void createWindow();
void destoryWindow(CCObject* pSender);
void popWindow(CCObject* pSender);
#include "HelloWorldScene.h"
#include "AppMacros.h"
USING_NS_CC;
CCScene* HelloWorld::scene()
CCScene *scene = CCScene::create();
HelloWorld *layer = HelloWorld::create();
scene->addChild(layer);
bool HelloWorld::init()
if ( !CCLayer::init() )
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
CCMenuItemImage *pPopWindowItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
menu_selector(HelloWorld::popWindow));
pPopWindowItem->setPosition(ccp(origin.x + visibleSize.width - pPopWindowItem->getContentSize().width/2 ,
origin.y + pPopWindowItem->getContentSize().height/2));
CCMenu* pPopWindowMenu = CCMenu::create(pPopWindowItem, NULL);
pPopWindowMenu->setPosition(CCPointZero);
this->addChild(pPopWindowMenu, 1);
CCMenuItemImage *pDestoryWindowItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
menu_selector(HelloWorld::destoryWindow));
pDestoryWindowItem->setPosition(ccp(50.0f, 50.0f));
CCMenu* pDestoryWindowMenu = CCMenu::create(pDestoryWindowItem, NULL);
pDestoryWindowMenu->setPosition(CCPointZero);
this->addChild(pDestoryWindowMenu, 1);
void HelloWorld::popWindow(CCObject* pSender)
createWindow();
void HelloWorld::createWindow()
CCSprite* pWindow = CCSprite::create("HelloWorld.png");
pWindow->setScale(0.2f);
pWindow->setPosition(ccp(240.0f, 140.0f));
pWindow->setTag(10);
this->addChild(pWindow);
CCScaleTo *pScaleTo = CCScaleTo::create(1.0f, 0.7f, 0.7f);
CCActionInterval *pAction = CCEaseElasticOut ::create(pScaleTo);
pWindow->runAction(pAction);
void HelloWorld::destoryWindow( CCObject* pSender )
this->getChildByTag(10)->removeFromParent();
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'谁有单,双打各关的子弹伤害表?_全民飞机大战吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0可签7级以上的吧50个
本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:375,526贴子:
谁有单,双打各关的子弹伤害表?
谁有单,双打各关的子弹伤害表?麻烦发一下,谢谢!
首先注明本贴转,给吧友...
觉醒技能吃药才这伤害,...
可怜的卡那疯,被su152h...
对【法医学】【解剖学】...
子弹九做出来一段时间,...
有人告诉我,单技能固伤...
镇楼图 1.雷明顿是子弹9...
贴吧热议榜
使用签名档&&
保存至快速回贴2590人阅读
游戏开发(23)
由于课程需要,然后又水平有限,所以写了个飞机大战,加上不会画画,所以图片资源也是从微信apk解压出来的,设计思路参考的。
闲话不说,先讲一下设计,大体上一共分为3个场景,场景下面是Layer
开始场景:WelcomeScene --&WelcomeLayer &类似欢迎界面
游戏主场景:GameScene &--& GameLayer &游戏元素添加 和 碰撞检测
结束场景:GameOverScene --&GameOverLayer
然后是游戏的元素,在GameLayer添加,一个分为4个Layer:
PlaneLayer:主角层&
EnemyLayer:敌机层 ,分为3种,每种敌机生命值不一样
UFOLayer: 道具层,bullet和加bomb
BulletLayer:子弹层 bullet 和mutiBullet
然后简单讲一下碰撞检测的实现:
if(bullet-&boundingBox().intersectsRect(enemy1-&getBoundingBox())){
if(enemy1-&getLife() == 1){
enemy1-&loseLife();
bulletsToDelete-&addObject(bullet);
enemy1ToDelete-&addObject(enemy1);
score += ENEMY1_SCORE;
this-&mControlLayer-&updateScore(score);
}Rect Enemy::getBoundingBox(){
Rect rect = this-&m_sprite-&boundingBox();
Point pos = this-&convertToWorldSpace(rect.origin);
return Rect(pos.x, pos.y, rect.size.width, rect.size.height);
还有再来就是子弹层的处理:
子弹发射需要步骤:1.添加子弹,2.移动子弹,3移除子弹
由于需要源源不断的发射子弹,所以将子弹添加到一个SpriteBatchNode节点中,然后在这个SpriteBatchNode节点对子弹进行添加,移动,和移除,这样子做的好处是只需要渲染一次,降低内存消耗
auto texture = TextureCache::getInstance()-&textureForKey(&shoot.png&);
bulletBatchNode = SpriteBatchNode::createWithTexture(texture);
this-&addChild(bulletBatchNode);
然后再讲一下图片资源的处理:
下载这个工具:
将微信需要的资源解析出来,然后可以用photoshop处理一下,哈哈,可以将微信的图片去掉,随便p上自己的名字
然后用TexturePacker打包,恩,大概就像这样子
最后再讲一下编译成android工程的步骤和细节:
1.先在Android.mk文件中添加所有必须的cpp
2.记得在AppDelegate cpp 中添加分辨率适应
glview-&setDesignResolutionSize(480,800,ResolutionPolicy::SHOW_ALL); 3.在proj.android文件夹下面修改AndroidManifest.xml 竖屏属性:
android:screenOrientation=&portrait&
4.cmd到工程目录,命令:cocos compile -p android -j 4
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:115421次
积分:1898
积分:1898
排名:第16380名
原创:62篇
评论:86条
(7)(1)(3)(12)(7)(5)(8)(5)(6)(1)(8)(1)cocos2d-x(26)
cocos2d-x 3.2 |碰撞与分数
前情提要:飞机大战第五篇 实现碰撞与分数
思路:游戏里物体与物体的碰撞每帧都会检测 这里我们利用前面讲过的贪吃蛇移动原理 利用update方法来实时检测
如下:新建类-----&Game
第一步:Game类(实现主要的游戏逻辑模块)
//引入需要用到的类#include &stdio.h&
#include &cocos2d.h&
#include &cocos-ext.h&
#include &cocosGUI.h&
#include &Bullet.h&
#include &Enemy.h&
#include &Boom.h&
#include &Tool.h&
USING_NS_CC;
using namespace cocos2d;
class Game:Layer
Vector&Bullet *& allB//集合:子弹
float startX,startY;
CREATE_FUNC(Game);
static Scene * CreateScene();
bool init();
virtual bool onTouchBegan(Touch *touch, Event *unused_event);//触摸检测 按下
virtual void onTouchMoved(Touch *touch, Event *unused_event);&span style=&font-family: Arial, Helvetica, sans-&&//触摸检测 移动&/span&
void newBullet(float t);//产生子弹
void moveBullet(float t);//移动子弹
Vector&Enemy *& allE//&span style=&font-family: Arial, Helvetica, sans-&&集合:&/span&&span style=&font-family: Arial, Helvetica, sans-&&敌机&/span&
void newEnemy(float t);//产生敌机
void moveEnemy(float t);//移动敌机
void update(float t);//游戏逻辑 碰撞检测
void reCallBack(Ref * obj);
Vector&Tool *& allT//集合:道具
};Game.cpp
#include &Game.h&
#include &BackGround.h&
#include &Plane.h&
#include &MainMenu.h&
#include &SimpleAudioEngine.h&
#include &cocosGUI.h&
#include &GameOver.h&
#include &Tool.h&
#include &Enemy.h&
using namespace CocosD
USING_NS_CC;
Scene * Game::CreateScene()
auto scene=Scene::create();
auto layer=Game::create();
scene-&addChild(layer);
bool Game::init()
if (!Layer::init())
//创建 返回 按钮
auto menurt=MenuItemLabel::create(Label::createWithSystemFont(&返回&, &&, 32),CC_CALLBACK_1(Game::reCallBack, this));
menurt-&setAnchorPoint(Vec2::ZERO);
menurt-&setPosition(600-30,Director::getInstance()-&getWinSize().height-36);
menurt-&setColor(Color3B::GREEN);
//创建菜单
auto menu=Menu::create(menurt,nullptr);
menu-&setPosition(Vec2::ZERO);
this-&addChild(menu,2);
//添加背景
auto bk=BackGround::create();
this-&addChild(bk);
//添加飞机
auto fly1=Plane::create();
this-&addChild(fly1,1);
fly1-&moveTo(100,100);
fly1-&setTag(10);
//处理飞机的HP
for (int i=0; i&fly1-& i++)
auto sphp=Sprite::create(&hp_1.png&);
sphp-&setTag(1000+i);
this-&addChild(sphp);
sphp-&setAnchorPoint(Vec2(0,1));
sphp-&setPosition(Vec2(i*sphp-&getContentSize().width+3*i,Director::getInstance()-&getWinSize().height));
//控制飞机飞行
auto listener=EventListenerTouchOneByOne::create();//创建侦听
listener-&onTouchBegan=CC_CALLBACK_2(Game::onTouchBegan, this);//定义侦听的回调
listener-&onTouchMoved=CC_CALLBACK_2(Game::onTouchMoved, this);
Director::getInstance()-&getEventDispatcher()-&addEventListenerWithSceneGraphPriority(listener, this);//将侦听添加到事件分发器中
//产生子弹 计划任务
this-&schedule(schedule_selector(Game::newBullet),0.2);//每隔200毫秒产生一次子弹
this-&schedule(schedule_selector(Game::moveBullet),0.01);
//产生敌机
this-&schedule(schedule_selector(Game::newEnemy),1);
this-&schedule(schedule_selector(Game::moveEnemy),0.01);
//实现游戏逻辑
this-&scheduleUpdate();
//分数显示器1
this-&m_score=0;
auto tscore=TextAtlas::create(&0000&, &img_num_dis.png&,17 , 22, &0&);
this-&addChild(tscore,12);
tscore-&setTag(210);
tscore-&setPosition(Vec2(Director::getInstance()-&getWinSize().width/2,
Director::getInstance()-&getWinSize().height-64));
//分数显示2
auto scoreLb = Label::createWithSystemFont(StringUtils::format(&分数:%d&,score), &&, 32);
scoreLb-&setAnchorPoint(Vec2::ZERO);
scoreLb-&setColor(Color3B::GREEN);
scoreLb-&setPosition(0,Director::getInstance()-&getWinSize().height-36);
scoreLb-&setTag(123);
this-&addChild(scoreLb,2);
//返回主菜单
void Game::reCallBack(Ref * obj)
auto scene=MainMenu::CreateScene();
Director::getInstance()-&replaceScene(scene);
bool Game::onTouchBegan(Touch *touch, Event *unused_event)
this-&startX=touch-&getLocation().x;
this-&startY=touch-&getLocation().y;
void Game::onTouchMoved(Touch *touch, Event *unused_event)
float mx=touch-&getLocation().x-startX;
float my=touch-&getLocation().y-startY;
auto fly1=(Plane *)this-&getChildByTag(10);
fly1-&moveTo(fly1-&px+mx, fly1-&py+my);
this-&startX=touch-&getLocation().x;
this-&startY=touch-&getLocation().y;
void Game::newBullet(float t)//产生子弹
auto fly1=(Plane *)this-&getChildByTag(10);
Bullet * bt=Bullet::newBullet(1, 0,fly1-&px,fly1-&py);
allBullet.pushBack(bt);
this-&addChild(bt);
//子弹音效
SimpleAudioEngine::getInstance()-&playEffect(&launch_1.wav&);
void Game::moveBullet(float t)//移动子弹
for (int i=0;i&allBullet.size();i++)
Bullet * nowB=allBullet.at(i);
nowB-&moveTo(nowB-&px, nowB-&py+15);
if (nowB-&py&Director::getInstance()-&getWinSize().height)
allBullet.erase(i);
this-&removeChild(nowB);
void Game::newEnemy(float t)
int type=random()%10;
if (type & 3)
else if (type & 8)
int ex=random()%(int)Director::getInstance()-&getWinSize().
Enemy * news=Enemy::createEnemy(type, ex, Director::getInstance()-&getWinSize().height+200);
allEnemy.pushBack(news);//将新产生的敌机添加到集合
this-&addChild(news);
//在当前GAME图层添加敌机层
void Game::moveEnemy(float t)
for (int i=0; i&allEnemy.size(); i++)
//获取第i架敌机
Enemy * nowE=allEnemy.at(i);
nowE-&moveTo(nowE-&ex, nowE-&ey-5);
if (nowE-&ey&-nowE-&eSprite-&getContentSize().height)
allEnemy.erase(i);
this-&removeChild(nowE);
void Game::update(float t)
//创建一个Label层 获取计数器标签
//auto scoreLb = (Label *)this-&getChildByTag(123);
//碰撞检测
if (allEnemy.size()==0 || allBullet.size()==0)
auto fly1=(Plane *)this-&getChildByTag(10);
//判断飞机和道具
for (int j = 0; j&allTool.size(); j++)
//得到第j架敌机
Tool * nowTool=allTool.at(j);
Rect br(fly1-&px,
fly1-&getChildByTag(10)-&getContentSize().width,
fly1-&getChildByTag(10)-&getContentSize().height);
//敌机的矩形大小
Rect er(nowTool-&tx,
nowTool-&ty,
nowTool-&sp-&getContentSize().width,
nowTool-&sp-&getContentSize().height);
if (br.intersectsRect(er))
{//碰撞检测到两个矩形相交
//判断吃到哪种道具
switch (nowTool-&type) {
case 1://hp
fly1-&hp++;
int i=fly1-&hp-1;
auto sphp=Sprite::create(&hp_1.png&);
sphp-&setTag(1000+i);
this-&addChild(sphp);
sphp-&setAnchorPoint(Vec2(0,1));
sphp-&setPosition(Vec2(i*sphp-&getContentSize().width+3*i,Director::getInstance()-&getWinSize().height));
case 2://炸雷
//所有敌机都销毁
while (allEnemy.size()&0)
Enemy * nowEnemy=allEnemy.at(0);
Boom * boom=Boom::createBoom(2, nowEnemy-&ex, nowEnemy-&ey);
this-&addChild(boom,10);
//爆炸一次 总增加分数
this-&m_score+=300;
allEnemy.eraseObject(nowEnemy);
nowEnemy-&removeFromParentAndCleanup(true);
//播放音效
SimpleAudioEngine::getInstance()-&playEffect(&launch_1.wav&);
//播放爆炸粒子
boom=Boom::createBoom(2,
Director::getInstance()-&getWinSize().width/2,
Director::getInstance()-&getWinSize().height/2);
this-&addChild(boom,15);
case 3://激光
case 4://散弹
//敌机消失
nowTool-&unscheduleAllSelectors();
nowTool-&removeFromParentAndCleanup(true);
allTool.eraseObject(nowTool);
for (int j = 0; j&allEnemy.size(); j++)
//得到第j架敌机
Enemy * nowEnemy=allEnemy.at(j);
//子弹的矩形大小
Rect br(fly1-&px,
fly1-&getChildByTag(10)-&getContentSize().width,
fly1-&getChildByTag(10)-&getContentSize().height);
//敌机的矩形大小
Rect er(nowEnemy-&ex,
nowEnemy-&ey,
nowEnemy-&eSprite-&getContentSize().width,
nowEnemy-&eSprite-&getContentSize().height);
if (br.intersectsRect(er))
{//碰撞检测到两个矩形相交
//敌机消失
allEnemy.eraseObject(nowEnemy);
nowEnemy-&removeFromParentAndCleanup(true);
//掉血音效
SimpleAudioEngine::getInstance()-&playEffect(&skill_1.wav&);
//我方Hp--
if (fly1-&hp&0)
this-&removeChildByTag(1000+fly1-&hp-1);
fly1-&hp=fly1-&hp-1;
if(fly1-&hp&=0)
{//Game Over
//创建一个场景
auto scene=GameOver::createScene();
//将场景覆盖到Game Over 并释放之前场景
Director::getInstance()-&replaceScene(scene);
//判断子弹 是否碰到敌机
for (int i=0; i&allBullet.size(); i++)
//得到第i颗子弹
Bullet * nowBullet=allBullet.at(i);
for (int j = 0; j&allEnemy.size(); j++)
//得到第j架敌机
Enemy * nowEnemy=allEnemy.at(j);
//子弹的矩形大小
Rect br(nowBullet-&px,
nowBullet-&py,
nowBullet-&bt-&getContentSize().width,
nowBullet-&bt-&getContentSize().height);
//敌机的矩形大小
Rect er(nowEnemy-&ex,
nowEnemy-&ey,
nowEnemy-&eSprite-&getContentSize().width,
nowEnemy-&eSprite-&getContentSize().height);
if (br.intersectsRect(er))
//检测到两个矩形相交 子弹消失
allBullet.eraseObject(nowBullet);
nowBullet-&removeFromParentAndCleanup(true);
nowEnemy-&hp=nowEnemy-&hp-1;
if(nowEnemy-&hp&=0)
if(nowEnemy-&type==1)
Boom * boom=Boom::createBoom(1, nowEnemy-&ex, nowEnemy-&ey);
this-&addChild(boom);
//scoreLb-&setString(StringUtils::format(&分数:%d&,score+=40));
//增加分数
this-&m_score+=40;
Boom * boom=Boom::createBoom(2, nowEnemy-&ex, nowEnemy-&ey);
this-&addChild(boom);
//scoreLb-&setString(StringUtils::format(&分数:%d&,score+=60));
this-&m_score+=60;
//分数增加器
auto score1=(TextAtlas *)this-&getChildByTag(210);
score1-&setString(StringUtils::format(&%d&,m_score));
//爆炸音效
SimpleAudioEngine::getInstance()-&playEffect(&launch_2.wav&);
//道具处理
if (nowEnemy-&toolID&0) {
//掉落道具
auto tool=Tool::newTool(nowEnemy-&toolID,nowEnemy-&ex,nowEnemy-&ey);
this-&addChild(tool);
allTool.pushBack(tool);
allEnemy.eraseObject(nowEnemy);
nowEnemy-&removeFromParentAndCleanup(true);
//退出这次循环
总结:至此利用5个篇章简单实现了一下飞机大战的主要核心内容,希望对读者有帮
后期还有两个番外篇:游戏分数保存+安卓导出调试(因为安卓实在是让人蛋疼)
ps:游戏中用到的素材与音效请自行替换。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:45384次
积分:1142
积分:1142
排名:千里之外
原创:69篇
游戏研发从业者
三维动画设计、脚本工具方向
(6)(3)(2)(12)(14)(1)(1)(1)(2)(3)(1)(3)(2)(1)(20)(7)

我要回帖

更多关于 coco2dx 的文章

 

随机推荐