cocos2dx游戏开发——微信打飞机学习笔记(六)——PlayerLayer的搭建

编程入门 行业动态 更新时间:2024-10-09 13:30:15

cocos2dx<a href=https://www.elefans.com/category/jswz/34/1770034.html style=游戏开发——微信打飞机学习笔记(六)——PlayerLayer的搭建"/>

cocos2dx游戏开发——微信打飞机学习笔记(六)——PlayerLayer的搭建

一、创建文件~

               PlayerLayer.h

               PlayerLayer.cpp

     一般类名都会和文件名有关系的~(在这里当然是一样)

二、How to do?

1、首先就是放一个飞机~

CC_SYNTHESIZE(bool, _isAlive, isAlive);Sprite *_playerplane;
 
void createPlayerPlane();

(1)额,首先就是先定义一个_isAlive变量,用来表示飞机是否活着。

PlayerLayer::PlayerLayer()
{_isAlive = false;                //首先初始化~
}

(2)完善createPlayerPlane()

void PlayerLayer::createPlayerPlane()
{auto visibleSize = Director::getInstance()->getVisibleSize();auto origin = Director::getInstance()->getVisibleOrigin();//首先就是获得屏幕的初始点以及屏幕的大小
 
_playerplane = Sprite::createWithSpriteFrameName("hero1.png");_playerplane->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + _playerplane->getContentSize().height / 2));this->addChild(_playerplane, 0, PLAYER_TAG);
           //创建飞机的图片,然后设置下图片Blink *blink = Blink::create(1, 3);//出来的时候闪一闪,比较帅~Animation* animation = Animation::create();animation->setDelayPerUnit(0.08f);animation->addSpriteFrame(SpriteFrameCache::getInstance()->spriteFrameByName("hero1.png"));animation->addSpriteFrame(SpriteFrameCache::getInstance()->spriteFrameByName("hero2.png"));Animate* animate = Animate::create(animation);//然后加入一个帧动画,就是不断的喷火~
 
_playerplane->runAction(blink);_playerplane->runAction(RepeatForever::create(animate));  //这个就是重复的播放喷火的动画。
_isAlive = true;             //表示飞机活着~
}

(3)加入场景

        a、 首先在PlayerLayer中调用

this->createPlayerPlane();

        b、然后把这个层加入到GameScene,就是加入到那个initLayer的函数中

然后先放图~

 

         但是你会发现根本动不了~所以呢,不要急,等等就加入一下触摸的控制,当然我承认我的这个层做的很不完善,当在做子弹层的时候就会明白的~。

2、加入触摸控制

(1)完成触摸函数

virtual bool onTouchBegan(Touch *touch, Event *unused_event);
virtual void onTouchMoved(Touch *touch, Event *unused_event);

        这里就需要使用那个cocos封装好的触摸控制,所以我们要先了解一下,在这里我们主要就是使用上面的两个继承下来的函数,从名字来看,Began就是监测是否点到屏幕上,然后Moved就是现在滑动屏幕的作用,然后还有那个结束触摸的那一点之类的。

        现在就让我们实现下这两个函数吧~

bool PlayerLayer::onTouchBegan(Touch *touch, Event *unused_event)
{return true;
    //当监测到触摸时(鼠标就是点击),然后返回true后,才会继续执行Moved的函数。
}void PlayerLayer::onTouchMoved(Touch *touch, Event *unused_event)
{if (_isAlive)                  //活着的时候才能进行滑动~挂掉的话,你划个屁屁~{Vec2 beginPoint = touch->getLocation();   //得到刚触摸到的点Rect planeRect = _playerplane->boundingBox();     //得到飞机所在的区域planeRect.origin.x -= 15;        planeRect.origin.y -= 15;planeRect.size.width += 30;planeRect.size.height += 30;               //把区域加大点,方便手的触摸if (planeRect.containsPoint(beginPoint))      //如果摸到飞机,就可以进行滑动~{Vec2 endPoint = touch->getPreviousLocation();Vec2 offSet = ccpSub(beginPoint, endPoint);Vec2 toPoint = ccpAdd(_playerplane->getPosition(), offSet);this->MoveTo(toPoint);}}
}

(2)将触摸加入监听~

    auto touchListenr = EventListenerTouchOneByOne::create();
                                          //touchListenr就是监听触摸的cocos封装的功能touchListenr->onTouchBegan = CC_CALLBACK_2(PlayerLayer::onTouchBegan, this);
 
                                          //加入BegantouchListenr->onTouchMoved = CC_CALLBACK_2(PlayerLayer::onTouchMoved, this);
 
                                         //加入MovedDirector::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListenr, this);
                                         //加入监听器~这个详情就得去看官网的教程啦~

然后放不了图了,大家就默默的自己暗爽吧~

更多推荐

cocos2dx游戏开发——微信打飞机学习笔记(六)——PlayerLayer的搭建

本文发布于:2024-03-15 04:54:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1738143.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:游戏开发   学习笔记   打飞机   cocos2dx   PlayerLayer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!