创建飞机类
添加飞机到游戏场景
处理飞机的动画
处理飞机的移动
创建子弹
实现子弹的定时任务
移动子弹
完成子弹的碰撞检测
player.h
#ifndef __PLAYER_SCENE_H__
#define __PLAYER_SCENE_H__
#include "cocos2d.h"
#include "SimpleAudioEngine.h"
using namespace cocos2d;
class Player :public CCNode
{
public:
Player(void);
~Player(void);
static Player *player();
bool init();
void update();
void addPlayer(bool isUp);
void acceptTouchesBegan( cocos2d::CCTouch *touch, cocos2d::CCEvent *pEvent);
void acceptTouchesMoved( cocos2d::CCTouch *touch, cocos2d::CCEvent *pEvent);
void acceptTouchesEnded( cocos2d::CCTouch *touch, cocos2d::CCEvent *pEvent);
CREATE_FUNC(Player);
private:
CCSprite *pSprite;
float tempx,tempy;
public:
int hp;
float hx,hy;
int spriteW,spriteH;
float x,y;
int type;
int level;
int bulletSum[2][4];
bool isDie;
};
#endif
player.cpp
#include "Player.h"
#include "Global.h"
Player::Player(void)
{
x=size.width/2;
y=size.height/4;
tempx=tempy=0;
hp=3;
type = 1;
level = 0;
for(int i=0;i<2;i++)
for(int j=0;j<4;j++)
bulletSum[i][j] =j*2+1;
isDie = false;
}
Player::~Player(void)
{
}
Player *Player::player()
{
Player *player = new Player();
if (player && player->init())
{
player->autorelease();
return player;
}
CC_SAFE_DELETE(player);
return NULL;
}
bool Player::init()
{
bool bRet = false;
do
{
bRet = true;
} while (0);
return bRet;
}
void Player::addPlayer(bool isUp){
this->removeChild(pSprite,true);
CCTexture2D *texture;
if (isUp)
{
pSprite = CCSprite::create("player2.png");
texture =CCTextureCache::sharedTextureCache()->addImage("player2.png");
}else{
pSprite = CCSprite::create("player.png");
texture =CCTextureCache::sharedTextureCache()->addImage("player.png");
}
spriteW = pSprite->getContentSize().width/4;
spriteH = pSprite->getContentSize().height;
//¥¥Ω®÷°–Ú¡–
CCAnimation *animation = CCAnimation::create();
animation->setDelayPerUnit(0.08f);
for(int i=0;i<4;i++)
animation->addSpriteFrameWithTexture(texture,CCRectMake(i*47,0,47,56));
pSprite=CCSprite::createWithTexture(texture,CCRectMake(0,0,47,56));
pSprite->runAction(CCRepeatForever::create(CCAnimate::create(animation)));
this->addChild(pSprite);
}
void Player::acceptTouchesBegan(CCTouch *touch,CCEvent *pEvent)
{
//ªÒ»°¥•µ„
//CCPoint location = touch->locationInView(touch->view());
CCPoint location =touch->getLocation();// CCDirector::sharedDirector()->convertToGL(location);
tempx = location.x;
tempy = location.y;
}
void Player::acceptTouchesMoved(CCTouch *touch,CCEvent *pEvent)
{
//CCPoint location = touch->locationInView(touch->view());
//location = CCDirector::sharedDirector()->convertToGL(location);
CCPoint location =touch->getLocation();
x += location.x-tempx;
y += location.y-tempy;
tempx=location.x;
tempy=location.y;
if (x<spriteW/2)
x = spriteW/2;
else if (x>size.width-spriteW/2)
x = size.width-spriteW/2;
else if(y<spriteH/2)
y=spriteH/2;
else if(y>size.height-spriteH/2)
y=size.height-spriteH/2;
}
void Player::acceptTouchesEnded(CCTouch *touch, CCEvent *pEvent)
{
///CCPoint location = touch->locationInView(touch->view());
//location = CCDirector::sharedDirector()->convertToGL(location);
CCPoint location =touch->getLocation();
}
void Player::update()
{
pSprite->setPosition(ccp(x,y));
if (hp<0)
{
isDie = true;
}
}