实现超级玛丽上下左右运动

实现超级玛丽上下左右运动,第1张

概述用Cocos2dx实现超级玛丽,首先用帧循环定时器判断方向,再在类中实现运行以及判断是否能运动。 方向控制 void LayerGame::moveMario(float dt){#ifdef WIN32 short key; key = GetKeyState('F'); if (key < 0) _marioDir = Common::RIGHT; ke 用Cocos2dx实现超级玛丽,首先用帧循环定时器判断方向,再在类中实现运行以及判断是否能运动。 方向控制
voID LayerGame::moveMario(float dt){#ifdef WIN32    short key;    key = GetKeyState('F');    if (key < 0) _marioDir = Common::RIGHT;    key = GetKeyState('D');    if (key < 0) _marioDir = Common::left;    key = GetKeyState('J');    if (key < 0)_mario->jump();#endif    if (_marioDir==Common::left)    {        //cclog("left\n");        _marioDir=Common::NONE;        _mario->moveleft(dt);    }else if(_marioDir==Common::RIGHT)    {        //cclog("right\n");        _marioDir=Common::NONE;        _mario->moveRight(dt);    }else    {        //cclog("stop\n");        _menuShow->setTexture(_textureDirNone);        _mario->stop();    }    _mario->moveUp(dt);    _mario->moveDown(dt);}
判断是否能向上,向左,向右,向下运动,利用碰撞检测
bool Mario::canMoveleft(float dt){    //judge if mario is out of the map    CCRect rcMario=this->boundingBox();    CCPoint ptMario=ccp(rcMario.getMinX(),rcMario.getMinY());    CCTMXTiledMap *map=getMap();    CCPoint ptMarioInWorld=map->convertToWorldspace(ptMario);    if (ptMarioInWorld.x-dt*_speed<0)       {        return false;    }    //judge if partition by a wall    CCPoint pt[3];    pt[0] = ccp(rcMario.getMinX() - dt*_speed,rcMario.getMIDY());    pt[1] = ccp(rcMario.getMinX() - dt*_speed,rcMario.getMinY());    pt[2] = ccp(rcMario.getMinX() - dt*_speed,rcMario.getMaxY());    //transform position,change pt into tile and get gID to decIDe whether be stoped by wall    for(int i=0;i<3;i++)    {        if (pt[i].y >= map->getContentSize().height)            continue;        CCPoint ptTile=Common::Point2Tile(map,pt[i]);        //wall water pipe and floor        static const char* layername[3] = { "block","pipe","land" };        for(int j=0;j<3;j++)        {            CCTMXLayer *layer=map->layernamed(layername[j]);            int gID=layer->tileGIDAt(ptTile);            if (gID!=0)            {                return false;            }        }    }    return true;}bool Mario::canMoveRight(float dt){    CCRect rcMario = boundingBox();    CCTMXTiledMaP* map = getMap();    CCPoint pt[3];    pt[0] = ccp(rcMario.getMaxX() + dt*_speed,rcMario.getMIDY());    pt[1] = ccp(rcMario.getMaxX() + dt*_speed,rcMario.getMinY());    pt[2] = ccp(rcMario.getMaxX() + dt*_speed,rcMario.getMaxY());    // 坐标转换,将pt转化成地图格子坐标,然后获取gID,判断gID是不是被阻挡    for (int i = 0; i < 3; ++i)    {        if (pt[i].y >= map->getContentSize().height)            continue;        CCPoint ptTile = Common::Point2Tile(map,pt[i]);        // 水管、砖头,地板        static const char* layername[3] = { "block","land" };        for (int j = 0; j < 3; ++j)        {            CCTMXLayer* layer = map->layernamed(layername[j]);            int gID = layer->tileGIDAt(ptTile);            if (gID != 0)            {                return false;            }        }    }    return true;}bool Mario::canMoveDown(float dt){    CCRect rcMario = boundingBox();    CCTMXTiledMaP* map = getMap();    CCPoint pt[3];    //dt*_speedDown    pt[0] = ccp(rcMario.getMIDX(),rcMario.getMinY() - dt*_speedDown);    pt[1] = ccp(rcMario.getMinX(),rcMario.getMinY() - dt*_speedDown);    pt[2] = ccp(rcMario.getMaxX(),rcMario.getMinY() - dt*_speedDown);    if (pt[0].y >= map->getContentSize().height)        return true;    // 坐标转换,将pt转化成地图格子坐标,然后获取gID,判断gID是不是被阻挡    for (int i = 0; i < 3; ++i)    {        CCPoint ptTile = Common::Point2Tile(map,"land" };        for (int j = 0; j < 3; ++j)        {            CCTMXLayer* layer = map->layernamed(layername[j]);            int gID = layer->tileGIDAt(ptTile);            if (gID != 0)            {                // 微调                CCPoint ptLB = Common::Tile2PointLB(map,ptTile+ccp(0,-1));                this->setpositionY(ptLB.y);                return false;            }        }    }    return true;}bool Mario::canMoveUp(float dt){    CCRect rcMario = boundingBox();    CCTMXTiledMaP* map = getMap();    CCPoint pt[3];    pt[0] = ccp(rcMario.getMIDX(),rcMario.getMaxY() + dt*_speedUp);    pt[1] = ccp(rcMario.getMinX(),rcMario.getMaxY() + dt*_speedUp);    pt[2] = ccp(rcMario.getMaxX(),rcMario.getMaxY() + dt*_speedUp);    if (pt[0].y >= map->getContentSize().height)        return true;    // 坐标转换,将pt转化成地图格子坐标,ptTile);                this->setpositionY(ptLB.y);                return false;            }        }    }    return true;}
运行实现
voID Mario::moveleft(float dt){    if (_dirRun!=Common::left)    {        _dirRun=Common::left;        _dirFace=Common::left;        updateStatus();    }    if (!canMoveleft(dt))        return;    Common::moveNode(this,ccp(-dt*_speed,0));    //scroll map    CCNode *map=this->getParent();    CCPoint ptWorld=map->convertToWorldspace(this->getposition());    if (ptWorld.x<winSize.wIDth/2&&map->getpositionX()<0)    {        Common::moveNode(map,ccp(dt*_speed,0));    }}voID Mario::moveRight(float dt){    if (_dirRun!=Common::RIGHT)    {        _dirRun=Common::RIGHT;        _dirFace=Common::RIGHT;        updateStatus();    }    if (!canMoveRight(dt))        return;    Common::moveNode(this,0));    //scroll map    CCNode *map=this->getParent();    CCPoint ptWorld=map->convertToWorldspace(this->getposition());    if (ptWorld.x>winSize.wIDth/2)    {        Common::moveNode(map,0));    }}voID Mario::moveUp(float dt){    if (_speedUp<=0)    {        return;    }    if (!canMoveUp(dt))    {        // 反d效果        _speedDown = _speedUp;        _speedUp = 0; // 不继续上升了        return;    }    Common::moveNode(this,ccp(0,dt*_speedUp));    _speedUp-=_speedAcc;    //_speedDown=_speedUp;}voID Mario::moveDown(float dt){    if(_speedUp<=0)    {    if (canMoveDown(dt))    {        if (_bFly==false)        {            _bFly=true;            updateStatus();        }        Common::moveNode(this,-dt*_speedDown));        _speedDown+=_speedAcc;    }    else    {        if (_bFly)        {            _bFly = false;            _speedDown = _speedAcc;            updateStatus();        }    }    }}voID Mario::stop(){    if (_dirRun!=Common::NONE)    {        _dirRun=Common::NONE;        updateStatus();    }}voID Mario::jump(){    static int i=0;    cclog("speed%d=%d",++i,_speedUp);    if (_bFly)        return;    _speedUp=300;    _bFly=true;    updateStatus();}
运行状态机,控制运行时的动画
voID Mario::updateStatus(){    stopAllActions();    if (_bFly)    {        if (_dirFace==Common::left)        {            setdisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByname("SmallJumpleft")); }else if(_dirFace==Common::RIGHT) { setdisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByname("SmallJumpRight")); } return; } if (_dirRun==Common::left) { this->runAction(CCRepeatForever::create(CCAnimate::create(CCAnimationCache::sharedAnimationCache()->animationByname("SmallWalkleft")))); }else if (_dirRun==Common::RIGHT) { this->runAction(CCRepeatForever::create(CCAnimate::create(CCAnimationCache::sharedAnimationCache()->animationByname("SmallWalkRight")))); }else { if (_dirFace==Common::left) { this->setdisplayFrameWithAnimationname("SmallWalkleft",0);        }else if(_dirFace==Common::RIGHT)        {            this->setdisplayFrameWithAnimationname("SmallWalkRight",0);        }    }}

注:在实现这个功能的时候,由于把等于写成赋值,发生了许多灵异的错误,要小心啊,等于与赋值。

运行结果 总结

以上是内存溢出为你收集整理的实现超级玛丽上下左右运动全部内容,希望文章能够帮你解决实现超级玛丽上下左右运动所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1078000.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存