C语言
函数名: abs
功 能: 求整数的绝对值
头文件:stdlib.h
函数原型:int abs(int i);
2// 碰撞检测
bool GameMain::isCollion(CCPoint p1, CCPoint p2, int w1, int h1, int w2, int h2)
{
//判断两个矩形是否碰撞
if (abs(p1.x - p2.x) < w1 + w2 && abs(p1.y - p2.y) < h1 + h2)
{
return true;
}
return false;
}
{
//判断两个矩形是否碰撞
if (abs(p1.x - p2.x) < w1 + w2 && abs(p1.y - p2.y) < h1 + h2)
{
return true;
}
return false;
}
void GameMain::update(float time)
{
CCPoint hpos = hero->getPosition();
// 碰撞检测
for (int i = 0; i < 3; i++)
{
GameObjEnemy *enemy = (GameObjEnemy*)enemys->objectAtIndex(i); // sjt
CCPoint epos = enemy->getPosition();
if(enemy->islife) // 敌人是否活着
{
for(int i = 0; i < bullets->capacity(); i++) // bullets(主角子弹)
{
if(((GameHeroBullet*)bullets->objectAtIndex(i))->getIsvisble())
{
if(isCollion(((GameHeroBullet*)bullets->objectAtIndex(i))->getPosition(), epos, 5, 13, 21, 28)) // 敌人和主角子弹碰撞检测
{
enemy->setdie();
gamemark->addnumber(200); // ahui
break;
}
}
}
}// endif(enemy->islife) // 敌人是否活着
if (!isreduce && enemy->islife && isCollion(hpos, epos, 21, 22.5, 21, 28)){
// 敌人和主角碰撞检测
enemy->setdie();
setherohurt();
}
}
//主角和敌人子弹碰撞
if (!isreduce){
for (int i = 0; i < enemybullets->capacity(); i++){
if (isCollion(hpos, ((GameEnemyBullet*)enemybullets->objectAtIndex(i))->getPosition(), 21, 22.5, 5, 13)){
setherohurt();
}
}
}
}
{
CCPoint hpos = hero->getPosition();
// 碰撞检测
for (int i = 0; i < 3; i++)
{
GameObjEnemy *enemy = (GameObjEnemy*)enemys->objectAtIndex(i); // sjt
CCPoint epos = enemy->getPosition();
if(enemy->islife) // 敌人是否活着
{
for(int i = 0; i < bullets->capacity(); i++) // bullets(主角子弹)
{
if(((GameHeroBullet*)bullets->objectAtIndex(i))->getIsvisble())
{
if(isCollion(((GameHeroBullet*)bullets->objectAtIndex(i))->getPosition(), epos, 5, 13, 21, 28)) // 敌人和主角子弹碰撞检测
{
enemy->setdie();
gamemark->addnumber(200); // ahui
break;
}
}
}
}// endif(enemy->islife) // 敌人是否活着
if (!isreduce && enemy->islife && isCollion(hpos, epos, 21, 22.5, 21, 28)){
// 敌人和主角碰撞检测
enemy->setdie();
setherohurt();
}
}
//主角和敌人子弹碰撞
if (!isreduce){
for (int i = 0; i < enemybullets->capacity(); i++){
if (isCollion(hpos, ((GameEnemyBullet*)enemybullets->objectAtIndex(i))->getPosition(), 21, 22.5, 5, 13)){
setherohurt();
}
}
}
}
3// 游戏结束时,回掉“back to menu",操作方法
bool GameMain::init(){
//初始化游戏结束弹板及按钮
gameover = CCSprite::create("gameover.png");
gameover->setAnchorPoint(ccp(0.5, 0.5));
gameover->setPosition(ccp(0, 0));
gameover->setPosition(ccp(size.width / 2, size.height / 2 + 70));
gameover->setVisible(false);
gameover->setScale(0.5);
addChild(gameover, 5);
gameover = CCSprite::create("gameover.png");
gameover->setAnchorPoint(ccp(0.5, 0.5));
gameover->setPosition(ccp(0, 0));
gameover->setPosition(ccp(size.width / 2, size.height / 2 + 70));
gameover->setVisible(false);
gameover->setScale(0.5);
addChild(gameover, 5);
CCMenuItemImage
*pCloseItem = CCMenuItemImage::create("back.png", "back.png",
this, menu_selector(GameMain::menuBackCallback));
//正常状态的图片 //按下去时的图片
pCloseItem->setPosition(ccp(size.width / 2, size.height / 2 - 50));
pCloseItem->setScale(0.5);
CCMenu *pMenu = CCMenu::create(pCloseItem, NULL);
this->addChild(pMenu, 5, 25); // void addChild(CCNode * child, int zOrder, int tag)
pMenu->setVisible(true);
pMenu->setEnabled(false);
this, menu_selector(GameMain::menuBackCallback));
//正常状态的图片 //按下去时的图片
pCloseItem->setPosition(ccp(size.width / 2, size.height / 2 - 50));
pCloseItem->setScale(0.5);
CCMenu *pMenu = CCMenu::create(pCloseItem, NULL);
this->addChild(pMenu, 5, 25); // void addChild(CCNode * child, int zOrder, int tag)
pMenu->setVisible(true);
pMenu->setEnabled(false);
}
void GameMain::setover(){
//设置游戏结束
CCMenu *pMenu = (CCMenu*)this->getChildByTag(25);
pMenu->setVisible(true);
pMenu->setEnabled(true);
gameover->setVisible(true);
gameover->setScale(0);
pMenu->setScale(0);
pMenu->runAction(CCScaleTo::create(0.5, 1));
gameover->runAction(CCScaleTo::create(0.5, 0.5));
}
//设置游戏结束
CCMenu *pMenu = (CCMenu*)this->getChildByTag(25);
pMenu->setVisible(true);
pMenu->setEnabled(true);
gameover->setVisible(true);
gameover->setScale(0);
pMenu->setScale(0);
pMenu->runAction(CCScaleTo::create(0.5, 1));
gameover->runAction(CCScaleTo::create(0.5, 0.5));
}
4//
CCScaleTo与CCScaleBy比较
CCScaleTo的create函数:
1
2
3
4
5
6
7
8
9
|
static
CCScaleTo* CCScaleTo::create(
float
duration ,
float
sx ,
float
sy);
/*
* duration是动作执行持续时间,单位为秒;
* sx是X方向的拉伸值(注意,是拉伸值!);
* sy是Y方向的拉伸值;
*/
|
CCScaleBy的create函数:
1
2
3
4
5
6
7
8
9
|
<span></span> Static CCScaleBy* CCScaleBy::create(
float
duration ,
float
sx ,
float
sy);
/*
* duration是动作执行持续时间
* sx是X方向的拉伸倍数(注意,现在是倍数!);
* sy是Y方向的拉伸倍数;
*/
|
error LNK2001: 无法解析的外部符号 "class cocos2d::Vec2 const cocos2d::CCPointZero" (?CCPointZero@cocos2d@@3VV
//pMenu->setPosition(CCPointZero);
//sjt
pMenu->setPosition(ccp(0, 0));
pMenu->setPosition(ccp(0, 0));
6//
CCPointZero
= CCPointMake(0,0);
7//
- CCBlink
- // 作用 :创建一额闪烁的动作
- // 参数1:闪烁完成的时间
- // 参数2:闪烁的次数
- // CCActionInterval * blink = CCBlink ::create(3, 10);
- // sp->runAction(blink);
7// AppDelegate::applicationDidFinishLaunching()
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
// glview = GLViewImpl::create("My Game");
glview = GLViewImpl::createWithRect("MyMiaoxinWar", Rect(0, 0, 320, 480));// 初始化界面
director->setOpenGLView(glview);
}
// turn on display FPS
// director->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
// director->setAnimationInterval(1.0 / 60);
// create a scene. it's an autorelease object
// auto scene = HelloWorld::createScene();
auto scene = GameMenu::scene();// GameMenuScene Classs
// run
director->runWithScene(scene);
return true;
}
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
// glview = GLViewImpl::create("My Game");
glview = GLViewImpl::createWithRect("MyMiaoxinWar", Rect(0, 0, 320, 480));// 初始化界面
director->setOpenGLView(glview);
}
// turn on display FPS
// director->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
// director->setAnimationInterval(1.0 / 60);
// create a scene. it's an autorelease object
// auto scene = HelloWorld::createScene();
auto scene = GameMenu::scene();// GameMenuScene Classs
// run
director->runWithScene(scene);
return true;
}