cocos2dx 《忍着飞镖射幽灵》小结
源码教程请移步:http://blog.****.net/iamlazybone/article/details/19612941
感谢懒骨头提供了这么多的Demo教程,对于初学者的我而言,帮助真的十分大。
注:本节仅仅记录博主自身学习《忍着飞镖射幽灵》的心得体会。
【游戏截图】
【学习心得】
1、遍历CCArray
1
2
3
4
5
6
|
// CCARRAY_FOREACH(CCArray, object)
{
//......
}
// |
2、更改背景颜色
使用颜色布景层CCLayerColor。
1
2
3
4
5
|
// //1.继承CCLayerColor
//2.然后在init里,将 if(!CCLayer::init()) 替换为
if ( !CCLayerColor::initWithColor( ccc4(255,255,255,255) ) )
// |
3、碰撞检测
使用boundingBox()、intersectsRect()两个函数,进行矩形碰撞检测。
1
2
3
4
5
6
7
8
|
// //获得图片矩阵 boundingBox()
CCRect rect_bullet = bullet->boundingBox();
CCRect rect_enemy = enemy->boundingBox();
//碰撞检测intersectsRect()
rect_enemy.intersectsRect(rect_bullet);
// |
4、获得触摸点坐标
1
2
3
4
|
// CCTouch* touch
CCPoint location = touch->getLocation();
// |
5、帧动画
直接使用图片资源创建Animation/Animate动画。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
// CCAnimation* animation = CCAnimation::create();
for ( int i = 9; i <= 23; i += 2) {
char str[50];
sprintf (str, "hero/20005_%2d.png" , i);
animation->addSpriteFrameWithFileName(str);
}
animation->setDelayPerUnit(1.0f / 16.0f); //每帧间隔,单位秒
animation->setRestoreOriginalFrame( true ); //是否还原为原始帧
CCFiniteTimeAction* ac = CCAnimate::create(animation);
CCCallFunc* finished = CCCallFunc::create( this , callfunc_selector(GameOver::actionDone));
CCFiniteTimeAction* act = CCSequence::create(ac, finished, NULL);
sp->runAction(act);
// |