标签:cocos2d-x3.4 游戏
多角度的子弹:
这个的实现可以仍然用MoveBy动作来完成,设定一个子弹偏移的角度,计算出到出屏幕,子弹在x轴和y轴上移动的距离,移动就OK了。
上代码:
void GameLayer::createBulletByAngle(float angle) { Size screenSize = Director::getInstance()->getWinSize() ; Vec2 crePos = m_pPlayerPlane->getPosition() ;//子弹的初始位置 float flyTime = 1.0f ; Sprite * pBullet = Sprite::create("bullet3.png") ; pBullet->setPosition(crePos) ; addChild(pBullet,ZORDER_BULLET_PLAYER) ; pBullet->setRotation(angle) ; float varX = Director::getInstance()->getWinSize().width - crePos.x ; if (angle < 0) { varX = crePos.x ; } float varY = varX * atan(angle) ; float moveDis = sqrt(varX * varX + varY * varY) ; flyTime = moveDis / Director::getInstance()->getWinSize().height ;//以屏幕高为标准 if (angle < 0) { varY = -varY ; varX = -varX ; } pBullet->runAction( Sequence::create( MoveBy::create(flyTime,Vec2(varX,varY)), CallFuncN::create(CC_CALLBACK_1(GameLayer::playerBulletMoveEndCallback,this)), RemoveSelf::create(true),nullptr) ) ; m_pPlayerBullet.pushBack(pBullet) ; }
追尾子弹特效:
类似流星,子弹飞出去有一条长长的尾巴。好像在暴龙战机上见过这种特效,很绚丽,模仿实现了个,传入终点,就OK。起始点是飞机的位置。没有精确的控制弧度。
上代码:
void GameLayer::createBulletByBezier(Vec2 endPos) { //两个控制点应该在起始点和重点连线的同一侧,这样会形成只有一个弧的路径 Size screenSize = Director::getInstance()->getWinSize() ; Vec2 crePos = m_pPlayerPlane->getPosition() ;//子弹的初始位置 Vec2 cp_1 = crePos ;//接近出发点的控制点 Vec2 cp_2 = endPos ;//接近结束点的控制点 float var_x = 100.0f ; float a = 0 ; float b = 0 ; if (crePos.x <= endPos.x)//1,4象限 { cp_1.x += var_x ; cp_2.x += var_x ; } else//2,3象限 { cp_1.x -= var_x ; cp_2.x -= var_x ; } a = -(crePos.x - endPos.x) / (crePos.y - endPos.y) ; b = crePos.y - crePos.x * a ; cp_1.y = a * cp_1.x + b ; b = endPos.y - endPos.x * a ; cp_2.y = a * cp_2.x + b ; ccBezierConfig bezier; bezier.controlPoint_1 = cp_1; bezier.controlPoint_2 = cp_2; bezier.endPosition = endPos; float flyTime = 0.2f ; auto bezierForward = BezierTo::create(flyTime, bezier); MotionStreak * pBullet = MotionStreak::create(0.5f, 1.0f, 10.0f, Color3B(255, 255, 0), "bullet1.png") ; addChild(pBullet,ZORDER_BULLET_PLAYER) ; pBullet->setPosition(crePos) ; pBullet->runAction( Sequence::create( bezierForward, DelayTime::create(0.5f), RemoveSelf::create(true),nullptr) ) ; }
标签:cocos2d-x3.4 游戏
原文地址:http://blog.csdn.net/c_boy_lu/article/details/44983341