cocos2d-x实现精灵的拖动

本文介绍了一个使用Cocos2d-x框架创建的HelloWorld项目,详细展示了如何通过修改HelloWorld.h和HelloWorld.cpp文件来实现触摸事件的监听与响应。包括了初始化场景、加载背景图片、创建触摸项及触摸开始、移动、结束的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值