
过了将近一个月了,终于之前说的主机与敌机,子d与敌机,子d与主机的碰撞问题解决,太开心了,写出自己的东西就是开心,等下把那块代码贴上来,顺带上一些我写的游戏的图片。
我定义了个子d类,用来主机发射子d用的,因为敌机与主机的方向不同,所以,敌机的子d写到敌机类中,由于子d类中需要用到主机的位置并且主机只有一个,所以我将主机定义成了一个单列变量,以便读取其位置。说到子d,由于子d有很多,会无数的发射,所以我首先用CCSpriteBatchNode,CCSpriteBatchNode是cocos2d-x为了降低渲染批次而建立的一个专门管理精灵的类。所以用他管理子d,然后定义数组,将CCSpriteBatchNode中图片保存在CCArray中,循环10次,重复用这10张图片,敌机也是同样的理。(由于刚学,所以可能会有很多不够优化,或是方法有问题,还请大家海涵)
子d类,主机类,敌机类,都是层,最终加入GameSence场景中,在该场景中实现所谓的碰撞检测逻辑。实现这一步,大概整个游戏差不多了。另外背景层的话,我是单独定义一个背景层用来实现背景的移动。好吧,下面给出一些代码~~
子d的创建:
voID BulletSprite::Create()
{
batch=CCSpriteBatchNode::create("bullet2.png");
batch->getTexture()->setAliasTexParameters();
addChild(batch);
bulletArr=CCArray::create();
bulletArr->retain();
num=0;
for(int i=0;i<10;i++)
{
bSprite=CCSprite::createWithTexture(batch->getTexture());
bulletArr->addobject(bSprite);
}
}
voID BulletSprite::moving(float dt)
{
auto PlanePos =FlySprite::sharedplane->getChildByTag(AIRFLY)->getposition();
bulletX=PlanePos.x;
bulletY=PlanePos.y+40;
Sprite=(CCSprite* )bulletArr->objectAtIndex(num);
//根据索引获得对象,所以这里是根据Num得到数组中的元素
Sprite->setposition(ccp(bulletX,bulletY));
this->addChild(Sprite,1);
//当前对象要移动的位置
move=CCMoveto::create(0.5f,ccp(bulletX,visibleSize.height));
CCActionInstant *funcN=CCCallFuncN::create(this,callfuncN_selector(BulletSprite::removeBullet));
Sprite->runAction(CCSequence::create(move,funcN,NulL));
num++;
if(num>=10)
num=0;
}
voID BulletSprite::removeBullet(CCNode *p)
{
CCSprite *sp=(CCSprite*)p;
this->removeChild(sp);
}
//敌军和主机的碰撞,主机减分,敌机死亡
voID GameScene::update(float dt)
{
sp1=(CCSprite*)eSprite->enemyArr->objectAtIndex(eynum);
sp1->setVisible(true);
if(fSprite->getBoundingBox().intersectsRect(sp1->boundingBox()))
{
//爆炸效果
kill(sp1);
this->removeChild(sp1);
sp1->setVisible(false);
move=CCMoveto::create(0.001f,ccp(-1,-1));
sp1->runAction(move);
fSprite->life--;
if(fSprite->life==0)
{
//this->removeChild(fSprite);
CCScene *s=EndScene::scene();
CCDirector::sharedDirector()->replaceScene(s);
}
}
eynum++;
if(eynum>=10)
eynum=0;
}
//碰撞爆炸效果,图片资源来自cocos2d-x中的samples\JavaScript\Shared\games\MoonWarriors
voID GameScene::kill(CCSprite *sp)
{
CCSpriteFrameCache *cache=CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithfile("explosion.pList");
CCSprite *ex=CCSprite::createWithSpriteFramename("explosion_01.png");
ex->setposition(ccp(sp->getpositionX(),sp->getpositionY()));
this->addChild(ex);
CCArray *arr=CCArray::createWithCapacity(14);
char str[50]={0};
for(int i=1;i<15;i++)
{
sprintf(str,"explosion_%02d.png",i);
CCSpriteFrame *frame=cache->spriteFrameByname(str);
arr->addobject(frame);
}
CCAnimation *animation=CCAnimation::createWithSpriteFrames(arr,0.02f);
CCActionInstant *funcN=CCCallFuncN::create(this,callfuncN_selector(GameScene::remove));
ex->runAction(CCSequence::create(CCAnimate::create(animation),NulL));
}
voID GameScene::remove(CCNode *p)
{
CCSprite *sp=(CCSprite*)p;
this->removeChild(sp);
}
主要代码大概是这样,路漫漫~~~继续学习吧~~~
总结以上是内存溢出为你收集整理的cocos2dx学习之路(二)全部内容,希望文章能够帮你解决cocos2dx学习之路(二)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)