cocos2d-x中矩形的碰撞检测
来源:未知 责任编辑:责任编辑 发表时间:2013-12-06 08:43 点击:次
#ifndef __HELLOWORLD_SCENE_H__
p>#define __HELLOWORLD_SCENE_H__
p>
p>#include "cocos2d.h"
p>using namespace cocos2d;
p>
p>class HelloWorld : public cocos2d::CCLayer
p>{
p>public:
p>
p> virtual bool init();
p> static cocos2d::CCScene* scene();
p> CREATE_FUNC(HelloWorld);
p>
p> virtual void onEnter();
p> virtual void onExit();
p> virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
p> virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
p> virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
p>
p> bool isRectCollision(CCRect rect1,CCRect rect2);
p>};
p>
p>#endif // __HELLOWORLD_SCENE_H__
p>
p>
p>
p>#include "HelloWorldScene.h"
p>#include "SimpleAudioEngine.h"
p>using namespace CocosDenshion;
p>
p>CCScene* HelloWorld::scene()
p>{
p> CCScene *scene = CCScene::create();
p> HelloWorld *layer = HelloWorld::create();
p> scene->addChild(layer);
p> return scene;
p>}
p>
p>bool HelloWorld::init()
p>{
p>
p> if ( !CCLayer::init() )
p> {
p> return false;
p> }
p> CCLayerColor*colorLayer = CCLayerColor::create(ccc4(0, 0, 255, 255), 320, 480);
p> addChild(colorLayer);
p>
p> CCLabelTTF* pLabel = CCLabelTTF::create("~还米有碰撞~", "Thonburi", 24);
p> pLabel->setColor(ccc3(255, 0, 0));
p> CCSize size = CCDirector::sharedDirector()->getWinSize();
p> pLabel->setPosition( ccp(size.width*0.5, size.height - 50) );
p> this->addChild(pLabel, 1,900);
p>
p> CCSprite*sp1 =CCSprite::create("rect1.png");
p> sp1->setPosition(ccp(100,230));
p> addChild(sp1,0,921);
p>
p> CCSprite*sp2 =CCSprite::create("rect2.png");
p> sp2->setPosition(ccp(240,140));
p> addChild(sp2,0,922);
p> return true;
p>}
p>
p>bool HelloWorld::isRectCollision(CCRect rect1,CCRect rect2){
p> float x1 = rect1.origin.x;
p> float y1 = rect1.origin.y;
p> float w1 = rect1.size.width;
p> float h1 = rect1.size.height;
p> float x2 = rect2.origin.x;
p> float y2 = rect2.origin.y;
p> float w2 = rect2.size.width;
p> float h2 = rect2.size.height;
p> if(x1+w1*0.5
p> return false;
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>