cocos2d-x实现精灵的拖动

cocos2d-x实现精灵的拖动,第1张

概述在HelloWorld项目中修改 1. HelloWorld.h #ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"USING_NS_CC;class HelloWorld : public Scene{public: static Scene* createScen 在HelloWorld项目中修改 1. HelloWorld.h
#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"USING_NS_CC;class HelloWorld : public Scene{public:    static Scene* createScene();    virtual bool init();    voID menuCloseCallback(cocos2d::Ref* pSender);    virtual bool ontouchBegan(touch *touch,Event *unused_event);    virtual voID ontouchmoved(touch *touch,Event *unused_event);    virtual bool ontouchend(touch *touch,Event *unused_event);    CREATE_FUNC(HelloWorld);private:    Sprite *m_touchItem;    bool m_btouchedItem;};#endif // __HELLOWORLD_SCENE_H__
2.HelloWorld.cpp
#include "HelloWorldScene.h"#include "SimpleAudioEngine.h"USING_NS_CC;Scene* HelloWorld::createScene(){    Scene *scene = Scene::create();    HelloWorld *layer = HelloWorld::create();    scene->addChild(layer);    return scene;}static voID problemloading(const char* filename){    printf("Error while loading: %s\n",filename);    printf("Depending on how you compiled you might have to add 'Resources/' in front of filenames in HelloWorldScene.cpp\n");}bool HelloWorld::init(){    if ( !Scene::init() )    {        return false;    }    EventListenertouchOneByOne *Listener = EventListenertouchOneByOne::create();    Listener->ontouchBegan = CC_CALLBACK_2(HelloWorld::ontouchBegan,this);    Listener->ontouchmoved = CC_CALLBACK_2(HelloWorld::ontouchmoved,this);    Listener->ontouchended = CC_CALLBACK_2(HelloWorld::ontouchend,this);    _eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener,this);    this->setcolor(color3B(0,255,255));    auto visibleSize = Director::getInstance()->getVisibleSize();    Vec2 origin = Director::getInstance()->getVisibleOrigin();    Size winSize = Director::getInstance()->getWinSize();    Sprite *pBg = Sprite::create("white.jpeg");    pBg->setposition(Vec2(winSize.wIDth/2,winSize.height/2));    pBg->setScale(4.5f);    addChild(pBg,0);    m_touchItem = Sprite::create();    m_touchItem->initWithfile("Closenormal.png");    m_touchItem->setposition(Vec2(winSize.wIDth/2,winSize.height/2));    addChild(m_touchItem,1);    return true;}voID HelloWorld::menuCloseCallback(Ref* pSender){}bool HelloWorld::ontouchBegan(cocos2d::touch *touch,cocos2d::Event *unused_event){    Size Spritesize = m_touchItem->getContentSize();    Point pt = m_touchItem->getposition();    Rect spriteRect = CCRectMake(pt.x - Spritesize.wIDth/2,pt.y - Spritesize.height/2,Spritesize.wIDth,Spritesize.height);    if (spriteRect.containsPoint(touch->getLocation()))    {        m_btouchedItem = true;    }    return true;}voID HelloWorld::ontouchmoved(cocos2d::touch *touch,cocos2d::Event *unused_event){    if (m_btouchedItem)    {        Point pos = touch->getLocation();        m_touchItem->setposition(pos);    }}bool HelloWorld::ontouchend(cocos2d::touch *touch,cocos2d::Event *unused_event){    if (m_btouchedItem)    {        m_btouchedItem = false;    }    return false;}
总结

以上是内存溢出为你收集整理的cocos2d-x实现精灵拖动全部内容,希望文章能够帮你解决cocos2d-x实现精灵的拖动所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1085447.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存