圆形碰撞
来源:互联网 发布:钢珠自动上弹器在淘宝 编辑:程序博客网 时间:2024/05/17 02:45
新建工程,CircleCollision
修改HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
using namespace cocos2d;
class HelloWorld :public cocos2d::CCLayer
{
public:
// Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
virtual bool init();
// there's no 'id' in cpp, so we recommend to return the class instance pointer
static cocos2d::CCScene* scene();
// preprocessor macro for "static create()" constructor ( node() deprecated )
CREATE_FUNC(HelloWorld);
virtual bool ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent);
virtual void onEnter();
virtual void onExit();
bool isCircleCollision(CCPoint pos1,float radius1,CCPoint pos2,float radius2);
};
#endif // __HELLOWORLD_SCENE_H__
修改HelloWorldScene.cpp
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
using namespace cocos2d;
using namespace CocosDenshion;
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer =HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
CCLayerColor *color=CCLayerColor::create(ccc4(255,255, 255, 255),480,320);
addChild(color);
CCSize size=CCDirector::sharedDirector()->getWinSize();
CCLabelTTF *pLabel=CCLabelTTF::create("没相撞^.^","Helvetica", 24);
pLabel->setColor(ccRED);
pLabel->setPosition(ccp(size.width*0.5,size.height-50));
this->addChild(pLabel,0,920);
CCSprite *sp1=CCSprite::create("circle1.png");
sp1->setPosition(ccp(100,170));
addChild(sp1,0,921);
CCSprite *sp2=CCSprite::create("circle2.png");
sp2->setPosition(ccp(350,170));
addChild(sp2,0,922);
return true;
}
boolHelloWorld::ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent)
{
CCPoint touchPoint=pTouch->getLocation();
CCSprite *sp1=(CCSprite*)this->getChildByTag(921);
sp1->setPosition(touchPoint);
return true;
}
voidHelloWorld::ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent)
{
CCPoint touchPoint=pTouch->getLocation();
CCSprite *sp1=(CCSprite*)this->getChildByTag(921);
CCSprite *sp2=(CCSprite*)this->getChildByTag(922);
CCLabelTTF *ttf=(CCLabelTTF*)this->getChildByTag(920);
if (this->isCircleCollision(sp1->getPosition(), sp1->getContentSize().width*0.5, sp2->getPosition(), sp2->getContentSize().width*0.5)) {
ttf->setString("相撞啦 =w=||");
}
else
{
ttf->setString("没相撞^.^");
}
sp1->setPosition(touchPoint);
}
voidHelloWorld::ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent)
{
}
voidHelloWorld::onEnter()
{
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0, false);
CCLayer::onEnter();
}
voidHelloWorld::onExit()
{
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
CCLayer::onExit();
}
boolHelloWorld::isCircleCollision(CCPoint pos1,float radius1,CCPoint pos2,float radius2)
{
if (sqrt(pow(pos1.x-pos2.x,2)+pow(pos1.y-pos2.y,2))>radius1+radius2) {
return false;
}
return true;
}
- 圆形碰撞
- 圆形碰撞
- 实现圆形碰撞函数
- 圆形碰撞算法
- 圆形碰撞判断
- android 圆形碰撞
- 矩形圆形碰撞检查
- 碰撞检测算法:点和矩形碰撞、点和圆形碰撞、矩形碰撞、圆形碰撞
- 碰撞检测算法:点和矩形碰撞、点和圆形碰撞、矩形碰撞、圆形碰撞
- 碰撞检测算法:点和矩形碰撞、点和圆形碰撞、矩形碰撞、圆形碰撞
- 碰撞检测算法:点和矩形碰撞、点和圆形碰撞、矩形碰撞、圆形碰撞
- 碰撞检测算法:点和矩形碰撞、点和圆形碰撞、矩形碰撞、圆形碰撞
- 圆形与矩形碰撞算法
- cocos creator 圆形碰撞代码
- 物理仿真中的圆形碰撞
- Android碰撞检测——圆形检查
- 圆形和长方形的碰撞检测!
- 圆形与多边形之间的碰撞检验!
- 人应该“懒”一些
- J-LINK V8固件烧录指导
- 如何截取Http请求
- jQuery案例_@注册时选择阅读条款_@左右移动
- Eclipse用法和技巧四:生成说明文档1
- 圆形碰撞
- 371. Respect yourself, or no one will respect you. 要人尊重,必须自重
- Delphi Socket编程(1)
- linux对i386保护模式的选择性使用
- 通过SQL绘制杨辉三角
- Winform版计算器
- 基于JCFXBL与GSM短信猫的短信中心建设方案
- Android ZXing 二维码、条形码扫描介绍
- java经典面试题(2)