飞机 plane类继承Node节点
首先在飞机类里面 创建创建一个然后开始 创建飞机图片以及设置初始位置以及
飞机的图片想用不一样的飞机 可以在 大厅页面里 设置一个静态路径变量 就可以了 创建 飞机的时候直接 用 静态变量创建就可以的
Player1 = Sprite::create(Planes::aaa);
就是这样子的
因为飞机回移动 并且 回 受玩家控制 这样的话有三种控制方法
第一种 触摸控制 第二种 键盘控制 第三种 鼠标 控制在这个里我都会写出来
第一种 触摸控制
首先创建一个触摸控制的函数
这是单触摸控制
_ auto Listener=EventListenerTouchOneByOne ::create()//创建一个监听器
_ listener ->onTouchBegan=[&](Touch* touch,Eventevent){
_ //手机的坐标和电脑的不一向首先下转换一下 变成换手机坐标的位置
_ Vec2 touch1=this->convertTouchNodeSpace(touch);
_ //下面是 成员变量Vec2
_ OffsetPos=touch1-plaer1->getPosition()//这个是设置触摸偏移量的
_ return player1->getBoundingBox().containsPoint(touch1);
_}
_ listener->onTouchMove=[&](Touch toucf,Event event){
Vec2 touch1=this->convertTouchNodeSpace(touch);
auto visible=Director::getInstance()->VisibleSize();
auto Pos=touch1-offsetPos;
auto size= player1->getContentSize();
if(pos.x<50)
{
pos.x=50;
//防止越界
}else if( pos.x>visible.width-50 )
{
pos.x=visible.width-50
}else if (pos.y < size.height / 2)
{
pos.y = size.height / 2;
}
else if (pos.y > visibleSize.height - size.height / 2)
{
pos.y = visibleSize.height - size.height / 2;
}
Player1->setPosition(pos);
//这是设置鼠标的便宜量
_}
listener->onTouchEnded = [=](Touch* touch, Event* event) {
// your code
};
这个是松下的函数
audio = SimpleAudioEngine::getInstance();
audio->playEffect("Music/bullet.wav");
//加入到 监听器当中
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
cuurentTime = 0;
endTime = 0.3f;
}
以上是 触摸的控制
下面做鼠标的控制
auto muouse=EnentListenerMouse::create();
//生命一个鼠标控制 指针 然后下面打开鼠标控制 然后又四个 回调函数
mouse->setEnabled(true);
mouse->onMouseUp=CC_CALLBACK_1(类名::MouseUp,this);
mouse->onMouseDOWN=CC_CALLBACK_1(类名::MouseDOWN,this);
mouse->onMouseMove=CC_CALLBACK_1(类名::Mousemove,this);
mouse->onMouseScroll=CC_CALLBACK_1(类名::MouseScroll,this);
//去下面去实现这四个类的 void Game::MuoseUp(cocos2d::Event* enet){
EventMouse* e = (EventMouse*)enet;
log("shang");
std::string str = "Mouse Down detected, Key: ";
//str += tostr(e->getMouseButton())
;}
void Game::MuoseDown(cocos2d::Event* enet)
//
{EventMouse* e = (EventMouse*)enet;
log("xia");
}void Game::MuoseMove(cocos2d::Event* enet){
EventMouse* e = (EventMouse*)enet;
log("dong");
ShopPaga->setPosition(e->getCursorX(), e->getCursorY());
}
void Game::MuoseScroll(cocos2d::Event* enet)
{
EventMouse* e = (EventMouse*)enet;
log("lun");
}
下面是添加 键盘控制 键盘空时是连个回调函数控制的 一个是监听按下 和松开 键盘的信息
bool Game::isKey(EventKeyboard::KeyCode keyCode)
{
if (keys[keyCode]) {
return true;
}
else {
return false;
}
}
void Game::Playermove(EventKeyboard::KeyCode type)
{
float movex = 0, movey = 0;
switch (type)
{
case cocos2d::EventKeyboard::KeyCode::KEY_LEFT_ARROW:
movex = -15;
break;
case cocos2d::EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
movex = +15;
break;
case cocos2d::EventKeyboard::KeyCode::KEY_UP_ARROW:
movey = +15;
break;
case cocos2d::EventKeyboard::KeyCode::KEY_DOWN_ARROW:
movey = -15;
break;
default:
movex = movey = 0;
break;
}
float nextx = ShopPaga->getPositionX() + movex;
float nexty = ShopPaga->getPositionY() + movey;
if (nextx > Director::getInstance()->getVisibleSize().width - ShopPaga->getContentSize().width / 4 || nextx<0 + ShopPaga->getContentSize().width / 4 ||
nexty>Director::getInstance()->getVisibleSize().height - ShopPaga->getContentSize().height / 4 - 10 || nexty < 0 + ShopPaga->getContentSize().height / 4 + 10)
{
}
else {
ShopPaga->setPosition(nextx, nexty);;
}
}
void Game::Shot(){
bulletNum++;
if (bulletNum = bulletMaxNum)
{
bulletNum = 1;
}
//for (int i = 0; i < bulletNum; i++)
//{
//}
}
void Game::MuoseUp(cocos2d::Event* enet)
{
EventMouse* e = (EventMouse*)enet;
log("shang");
std::string str = "Mouse Down detected, Key: ";
//str += tostr(e->getMouseButton());
}键盘的前边调用的函数又点忘了 下次在写吧
下面写敌人的生成实现