【cocos2d-x IOS游戏开发-城市跑酷12】男一号上场,大家鼓掌!

来源:互联网 发布:苹果6的4g网络慢怎么办 编辑:程序博客网 时间:2024/04/30 02:43

尊重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/17324797


<捕鱼达人>回顾

【cocos2d-x IOS游戏开发-捕鱼达人1】内容介绍

上节回顾

【cocos2d-x IOS游戏开发-城市跑酷11】设置菜单层及新的开始


创建一个主角:

//创建主角类_player = Player::create();_gameBatchNode->addChild(_player, kBackground);

Player * Player::create () {Player * player = new Player();if (player && player->initWithSpriteFrameName("player_1.png")) {player->autorelease();player->setSize();player->initPlayer();return player;}CC_SAFE_DELETE(player);return NULL;}

初始化主角:

//初始化主角void Player::initPlayer () {//设置锚点this->setAnchorPoint(ccp(0.5f, 1.0f));this->setPosition(ccp(_screenSize.width * 0.2f, _nextPosition.y));//_height = 228;//_width = 180;_height = 50;_width = 50;    CCAnimation* animation;//创建一个空白的序列帧动画信息    animation = CCAnimation::create();//CCSpriteFrame对应的就是帧,将CCSpriteFrame添加到CCAnimation生成动画数据,//用CCAnimation生成CCAnimate(就是最终的动画动作),最后可以用CCSprite执行这个动作。    CCSpriteFrame * frame;    int i;//共有3帧,这里用for循环将对应的序列图加入到动画中    for(i = 1; i <= 3; i++) {        char szName[100] = {0};        sprintf(szName, "player_%i.png", i);        frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(szName);        animation->addSpriteFrame(frame);    }    //设置每两帧间时间间隔    animation->setDelayPerUnit(0.2f / 3.0f);//设置动画结束后是否保留动画帧信息    animation->setRestoreOriginalFrame(false);//设置循环播放次数 (-1:无限循环)    animation->setLoops(-1);//由这个动画信息创建一个序列帧动画    _rideAnimation = CCAnimate::create(animation);//保存这个动画    _rideAnimation->retain();    //CCFiniteTimeAction是所有有限次执行类(或者也可以叫顺序执行类吧)的基类//CCSequence创建一个动作序列//CCEaseInOut创建对应匀速动画的变速动画,参数一为一个匀速动画。参数二为速度    CCFiniteTimeAction* easeSwing = CCSequence::create(           CCEaseInOut::create(CCRotateTo::create(0.8f, -10), 2),           CCEaseInOut::create(CCRotateTo::create(0.8f, 10), 2),           NULL);    _floatAnimation = CCRepeatForever::create( (CCActionInterval*) easeSwing );    _floatAnimation->retain();        this->runAction(_rideAnimation);}

主角状态更新:

void Player::update (float dt) {//加速度到最大,然后匀速if (_speed + ACCELERATION <= _maxSpeed) {_speed += ACCELERATION;} else {_speed = _maxSpeed;}_vector.x = _speed;//CCLog("play state:%d",_state);//主角状态switch (_state) {//在移动case kPlayerMoving:_vector.y -= GRAVITY;if (_hasFloated) _hasFloated = false;break;//在下降case kPlayerFalling:if (_floating ) {_vector.y -= FLOATNG_GRAVITY;_vector.x *= FLOATING_FRICTION;} else {_vector.y -= GRAVITY;_vector.x *= AIR_FRICTION;_floatingTimer = 0;}break;//死亡case kPlayerDying:_vector.y -= GRAVITY;_vector.x = -_speed;this->setPositionX(this->getPositionX() + _vector.x);break;}if (_jumping) {_state = kPlayerFalling;_vector.y += PLAYER_JUMP * 0.25f;if (_vector.y > PLAYER_JUMP ) _jumping = false;}if (_vector.y < -TERMINAL_VELOCITY) _vector.y = -TERMINAL_VELOCITY;_nextPosition.y = this->getPositionY() + _vector.y;if (_vector.x * _vector.x < 0.01) _vector.x = 0;if (_vector.y * _vector.y < 0.01) _vector.y = 0;if (_floating) {_floatingTimer += dt;if (_floatingTimer > _floatingTimerMax) {_floatingTimer = 0;this->setFloating(false);}}}

2 1
原创粉丝点击