云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > chapter8 实验手册

chapter8 实验手册

  • 62 次阅读
  • 3 次下载
  • 2025/12/11 1:03:15

3、初始化瞄准区域(物理世界大小) // init aim rect _aimRect = Rect(0, 0, visibleSize.width - 60, visibleSize.height); 4、初始化物理世界 // init physics world this->initPhysics(); 5、初始化桌球 // 白球 _mainBilliards = BilliardSprite::create(\false, true, _world, \this->addChild(_mainBilliards, 10, MAIN_BILLIARDS_TAG); for (int i=0;i<5;i++) { } auto ballSprite = BilliardSprite::create(\this->addChild(ballSprite); Vec2(200,visibleSize.height/2)); 6、初始化添加瞄准线、瞄准图片和球杆 // 球杆 auto aim_gan = Sprite::create(\this->addChild(aim_gan, 10, AIM_GAN_TAG); aim_gan->setAnchorPoint(Vec2(0.5, 0)); aim_gan->setScale(0.6f); aim_gan->setVisible(false); // aim icon auto aim_icon = Sprite::create(\this->addChild(aim_icon, 10, AIM_ICON_TAG); aim_icon->setScale(0.4f); aim_icon->setVisible(false); // dash line auto dashLine = Sprite::create(\this->addChild(dashLine, 2, DASH_LINE_TAG); dashLine->setAnchorPoint(Vec2(0.5, 0)); dashLine->setVisible(false); (八)能量条的控制

1、能量条区域触摸响应事件的设置

设置触摸开始时响应的事件:

bool PhysicsBox2dScene::onTouchBegan(Touch *touch, Event *unused_event) { } return true; if (_aimRect.containsPoint(touch->getLocation()) && _canAim) { } _powerEnd = false; _aim = true; auto start_p = changePos(_mainBilliards->getPosition()); auto end_p = touch->getLocation(); updateLine(start_p, end_p);

设置触摸移动时响应的事件,分为两种情况,一种是当触摸移动的区域的是非物理世界时,改变的是能量条的状态;另一种情况是当触摸移动的区域的是物理世界区域时,改变的是球杆瞄准的方向: void PhysicsBox2dScene::onTouchMoved(Touch *touch, Event *unused_event) { } } else if (_aimRect.containsPoint(touch->getStartLocation()) && _canAim) { } if (_aimRect.containsPoint(touch->getLocation())) { } auto start_p = changePos(_mainBilliards->getPosition()); auto end_p = touch->getLocation(); updateLine(start_p, end_p); if (_curPower<0) { } else if( _curPower > slider_bg->getContentSize().height ) { } updatePowerSlider(_curPower); _curPower = slider_bg->getContentSize().height; _curPower = 0; auto slider_bg = (Sprite*)getChildByTag(SLIDER_BG_TAG); if (_powerRect.containsPoint(touch->getStartLocation()) && _aim) { _curPower = touch->getStartLocation().y - touch->getLocation().y; 设置触摸结束时响应的事件:

void PhysicsBox2dScene::onTouchEnded(Touch *touch, Event *unused_event) { } } _powerEnd = true; // 进度条可以消除 // 击球运动后,在所有物体都静止之前无法再进行瞄准和击球 _canAim = false; _aim = false; auto dash_line = (Sprite*)getChildByTag(DASH_LINE_TAG); dash_line->setVisible(false); auto aim_icon = (Sprite*)getChildByTag(AIM_ICON_TAG); aim_icon->setVisible(false); auto aim_gan = (Sprite*)getChildByTag(AIM_GAN_TAG); aim_gan->setVisible(false); // 设置白球速度 _mainBilliards->SetLinearVelocity(b2Vec2(v.x * q * MAX_SPEED, v.y * q * // 击球 auto start_p = changePos(_mainBilliards->getPosition()); auto end_p = _aimPos; auto v = changePos((end_p - start_p).getNormalized()); float q = _curPower / slider_bg->getContentSize().height; if (_powerRect.containsPoint(touch->getLocation()) && _aim) { auto slider_bg = (Sprite*)getChildByTag(SLIDER_BG_TAG); _sliderSpeed = _curPower / slider_bg->getContentSize().height * 50.0f; MAX_SPEED)); 2、为控制条添加监听

在PhysicsBox2dScene.cpp文件中init函数中 ); // add touch listener auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = CC_CALLBACK_2(PhysicsBox2dScene::onTouchBegan, this); listener->onTouchMoved = CC_CALLBACK_2(PhysicsBox2dScene::onTouchMoved, this); listener->onTouchEnded = CC_CALLBACK_2(PhysicsBox2dScene::onTouchEnded, this); Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,this

(九)改变球杆方向

b2Vec2 PhysicsBox2dScene::changePos(Vec2 pos) { } cocos2d::Vec2 PhysicsBox2dScene::changePos(b2Vec2 pos) { }

return Vec2(pos.x*PTM_RATIO, pos.y*PTM_RATIO); return b2Vec2(pos.x/PTM_RATIO, pos.y/PTM_RATIO); (十)更新瞄准线的位置和球杆位置

void PhysicsBox2dScene::updateLine(Vec2 start_p, Vec2 end_p) { } auto aim_gan = (Sprite*)getChildByTag(AIM_GAN_TAG); aim_gan->setPosition(aim_gan_pos); aim_gan->setRotation(CC_RADIANS_TO_DEGREES(-_angle) - 90.0f); aim_gan->setVisible(true); // 计算球杆位置 auto aim_gan_pos = Vec2( (((end_p - start_p).getLength() + 45) * start_p.x - 45 * end_p.x) / (end_p - (((end_p - start_p).getLength() + 45) * start_p.y - 45 * end_p.y) / (end_p - // update aim icon auto aim_icon = (Sprite*)getChildByTag(AIM_ICON_TAG); aim_icon->setPosition(_aimPos); aim_icon->setVisible(true); // update line _angle = (_aimPos - start_p).getNormalized().getAngle() ; auto dashLine = (Sprite*)getChildByTag(DASH_LINE_TAG); dashLine->setPosition(start_p); dashLine->setTextureRect(Rect(0,0,2,abs((end_p - start_p).getLength()))); dashLine->setRotation(CC_RADIANS_TO_DEGREES(-_angle) + 90.0f); dashLine->setVisible(true); _aimPos = end_p; start_p).getLength(), start_p).getLength()); (十一)实现碰撞检测(物理模拟) 1、碰撞检测(物理模拟)的实现函数 void PhysicsBox2dScene::update(float delta) {

搜索更多关于: chapter8 实验手册 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

3、初始化瞄准区域(物理世界大小) // init aim rect _aimRect = Rect(0, 0, visibleSize.width - 60, visibleSize.height); 4、初始化物理世界 // init physics world this->initPhysics(); 5、初始化桌球 // 白球 _mainBilliards = BilliardSprite::create(\false, true, _world, \this->addChild(_mainBilliards, 10, MAIN_BILLIARDS_TAG); for (int i=0;iaddChild(ballSprite); Vec2(

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com