zoukankan      html  css  js  c++  java
  • cocos2d横版游戏之摇杆控制

    以上是一个控制摇杆,分为一个底座和摇杆小圈圈,我们的目的是通过算出摇杆小圈跟底座中心的偏移来控制任务的走向,我们计算出一个偏移量来,然后在场景update(foat dt)...每一帧的位置上加上这个偏移量,不停的播放行走动画(当然人物的朝向要对),就可以达到控制任务行走的效果了,这一点你可能一下子就会想到高中学过的向量的加减处理了......坐标象限....之类的。

    void DirectionControlButton::controlTargetMove(CCTouch *pTouch){
        /*****以摇杆中心为坐标系原点,计算出原点到触摸点的向量在坐标系中的角度0~180,-180~0***/
        CCPoint location = convertTouchToNodeSpace(pTouch); //转换成节点坐标系,因为pTouch是世界坐标系(也就是屏幕的坐标系),转换成当前摇杆底座节点的坐标系
        this->getTinyCircleSprite()->setPosition(location);//将摇杆小圈设置到该位置
        CCPoint _direction = CCPoint(ccp(0,0));
    
        if(global->hero->getAllowMove())  
        {      
            float radians = ccpToAngle(ccpSub(location, this->getPosition()));//算出的是弧度
            float degrees =  CC_RADIANS_TO_DEGREES(radians);//,这里转换成角度0~180,-180~0
            float Speed = 2;//速度
            float sqrtSpeed = sqrt(Speed);//速度的平方根 为了让斜着走45度也能保持相同的速度
    
            if (degrees <= 22.5 && degrees >= -22.5) 
            {
                //
                _direction = ccp(Speed, 0.0);
            }
            else if (degrees > 22.5 && degrees < 67.5)
            {
                //右上
                _direction = ccp(sqrtSpeed, sqrtSpeed);
            }
            else if (degrees >= 67.5 && degrees <= 112.5)
            {
                //
                _direction = ccp(0.0, Speed);
            }
            else if (degrees > 112.5 && degrees < 157.5)
            {
                //左上
                _direction = ccp(-sqrtSpeed, sqrtSpeed);
            }
            else if (degrees >= 157.5 || degrees <= -157.5)
            {
                //
                _direction = ccp(-Speed, 0.0);
            }
            else if (degrees < -22.5 && degrees > -67.5)
            {
                // 右下
                _direction = ccp(sqrtSpeed, -sqrtSpeed);
            }
            else if (degrees <= -67.5 && degrees >= -112.5)
            {
                // 下方
                _direction = ccp(0.0, -Speed);
            }
            else if (degrees < -112.5 && degrees > -157.5)
            {
                // 左下
                _direction = ccp(-sqrtSpeed, -sqrtSpeed);
            }
    
            //判断本次是否需要反转(因为我们的帧动画只有一个朝向),这是给后续动画用的
    //另外global是个全局的单例对象,该对象保存了当前所控制角色的指针 if((degrees >= 0 && (degrees <= 90)) || (degrees >= -90 && degrees < 0)){ if(global->hero->getRoleDirection() == RolelTurnLeft){ global->hero->setFlipX(false); global->hero->setRoleDirection(RolelTurnRight); } }else{ if(global->hero->getRoleDirection() == RolelTurnRight){ global->hero->setFlipX(true); global->hero->setRoleDirection(RolelTurnLeft); } } //设置偏移量 this->setDirection(_direction); //判断下一次移动点是否还在瓦片地图的floor层上,防止精灵移动出地图 if(false == tileAllowMove(global->hero->getPosition()+_direction)){ global->hero->setAllowMove(false); } } }

    好了这里就计算出主角下一帧所需要加上的偏移量了,连精灵是否需要反转都设置出来了了

  • 相关阅读:
    python 格式化 json输出
    python
    回顾2013
    C扩展Python
    C扩展Python
    Python
    Python interview preparing
    Python用smtplib发送邮件
    Python 安装路径, dist-packages 和 site-packages 区别
    nginx worker_cpu_affinity使用方法
  • 原文地址:https://www.cnblogs.com/zouly/p/3841830.html
Copyright © 2011-2022 走看看