zoukankan      html  css  js  c++  java
  • 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的函数中

    然后先放图~

    image

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

    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);
     
                                              //加入Began
        touchListenr->onTouchMoved = CC_CALLBACK_2(PlayerLayer::onTouchMoved, this);
    
    
                                             //加入Moved
        Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListenr, this);
                                             //加入监听器~这个详情就得去看官网的教程啦~

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

  • 相关阅读:
    JQuery基础知识--方便忘记时查看
    关于jquery.validate.js的用法
    JQuery ajax提交表单及表单验证
    thinkphp rabc权限总结
    关于jquery ajax项目总结
    中国剩余定理及其拓展 CRT&EXGCD
    bzoj 4899 记忆的轮廓 题解(概率dp+决策单调性优化)
    bzoj3307 雨天的尾巴题解及改题过程(线段树合并+lca+树上差分)
    20190614考试心态爆炸记
    fhq Treap(无旋Treap)
  • 原文地址:https://www.cnblogs.com/BlueMountain-HaggenDazs/p/3931014.html
Copyright © 2011-2022 走看看