Here is a complete solution, it looks cumbersome, but works.
HelloWorldScene.h as follows:
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
using namespace cocos2d;
extern EventMouse* mouseDownEvent=nullptr;
extern int x=0;
class HelloWorld : public cocos2d::Scene
{
private:
int i,j,ii;
cocos2d::Sprite* node2;
cocos2d::Sprite* node3;
cocos2d::Sprite* node4;
Scheduler* _scheduler1;
EventListenerMouse* _mouseListener;
EventListenerMouse* _mouseListener1;
EventDispatcher* _eventDispatcher;
public:
HelloWorld::HelloWorld(){
ii=0;
i=0;
node2=nullptr;
node3=nullptr;
node4=nullptr;
_scheduler1=Director::getInstance()->getScheduler();
_mouseListener=EventListenerMouse::create();
_mouseListener1=EventListenerMouse::create();
_eventDispatcher = _director->getEventDispatcher();
}
static cocos2d::Scene* createScene();
virtual bool init();
void onMouseDown(cocos2d::Event* event);
void onMouseUp(cocos2d::Event* event);
void onMouseMove(cocos2d::Event *event);
CREATE_FUNC(HelloWorld);
void popScene(float t);
};
#endif
HelloWorldScene.cpp as follows:
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include "SimpleAudioEngine.h"
#include "cocos2d.h"
#include "TheLayer.h"
#include "AppDelegate.h"
#define MOUSE_BUTTON_LEFT 0
#define MOUSE_BUTTON_RIGHT 1
#define MOUSE_BUTTON_MIDDLE 2
using namespace cocos2d;
using namespace CocosDenshion;
using namespace std;
USING_NS_CC;
Scene* HelloWorld::createScene()
{
return HelloWorld::create();
}
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;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
if (label == nullptr)
{
problemLoading("'fonts/Marker Felt.ttf'");
}
else
{
label->setPosition(Vec2(origin.x + visibleSize.width / 2,
origin.y + visibleSize.height - label->getContentSize().height));
this->addChild(label, 1);
}
auto sprite = Sprite::create("HelloWorld.png");
if (sprite == nullptr)
{
problemLoading("'HelloWorld.png'");
}
else
{
sprite->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
this->addChild(sprite, 0);
}
mouseDownEvent=nullptr;
node2=Sprite::create("ty.png");
node2->setPosition(Vec2(200,200));
this->addChild(node2);
this->_mouseListener->onMouseDown=CC_CALLBACK_1(HelloWorld::onMouseDown,this);
this->_mouseListener->onMouseUp=CC_CALLBACK_1(HelloWorld::onMouseUp,this);
this->_mouseListener->onMouseMove=CC_CALLBACK_1(HelloWorld::onMouseMove,this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(_mouseListener,this);
SimpleAudioEngine::getInstance()->playBackgroundMusic("yingyue.mp3",true);
return true;
}
void HelloWorld::onMouseDown(Event* event){
node3=Sprite::create("ty.png");
node3->setPosition(node2->getPosition());
this->addChild(node3);
SimpleAudioEngine::getInstance()->playEffect("yingxiao.mp3", false, 1.0, 1.0, 1.0);
mouseDownEvent=(EventMouse*)event;
auto moveTo = MoveTo::create(1, Vec2(mouseDownEvent->getCursorX(),mouseDownEvent->getCursorY()));
node3->runAction(moveTo);
_scheduler1->schedule(CC_SCHEDULE_SELECTOR(HelloWorld::popScene),this,1.0f,false);
}
void HelloWorld::onMouseUp(Event* event){
mouseDownEvent=nullptr;
_scheduler1->unscheduleAllForTarget(this);
}
void HelloWorld::onMouseMove(Event* event){
if(mouseDownEvent){
_scheduler1->unscheduleAllForTarget(this);
SimpleAudioEngine::getInstance()->playEffect("yingxiao.mp3", false, 1.0, 1.0, 1.0);
this->node4=Sprite::create("ty.png");
this->node4->setPosition(node2->getPosition());
this->addChild(node4);
EventMouse* p=(EventMouse*)event;
auto moveTo = MoveTo::create(1,Vec2(p->getCursorX(),p->getCursorY()));
node4->runAction(moveTo);
auto pDelayTime=DelayTime::create(1.0f);
node4->runAction(pDelayTime);
_eventDispatcher->removeEventListener(_mouseListener);
Node* pNode=this->getChildByTag(1);
if(pNode)
return;
else{
Layer* pLayer=TheLayer::createLayer();
this->addChild(pLayer,3,1);
}
}
}
void HelloWorld::popScene(float t)
{
if(mouseDownEvent){
SimpleAudioEngine::getInstance()->playEffect("yingxiao.mp3", false, 1.0, 1.0, 1.0);
Sprite* node4=Sprite::create("ty.png");
node4->setPosition(node2->getPosition());
this->addChild(node4);
auto moveTo = MoveTo::create(1, Vec2(node3->getPosition()));
node4->runAction(moveTo);
}
}
TheLayer.h as follows:
#ifndef __THE_LAYER_H__
#define __THE_LAYER_H__
#include "cocos2d.h"
extern int y,k;
class TheLayer : public cocos2d::Layer{
private:
int kk;
int i,m,n,x,y,nn,mm;
cocos2d::Sprite* node6;
Node* node5;
cocos2d::Sprite* node2;
cocos2d::Sprite* node4;
Scheduler* _scheduler11;
Scheduler* _scheduler22;
EventListenerMouse* _mouseListener;
EventListenerMouse* _mouseListener1;
EventDispatcher* _eventDispatcher;
EventDispatcher* _eventDispatcher1;
EventMouse* p;
public:
TheLayer::TheLayer(){
kk=0;
i=0;
m=0;
n=0;
x=0;
y=0;
nn=0;
mm=0;
_scheduler22=Director::getInstance()->getScheduler();
_scheduler11=Director::getInstance()->getScheduler();
_mouseListener=EventListenerMouse::create();
_mouseListener1=EventListenerMouse::create();
_eventDispatcher = _director->getEventDispatcher();
_eventDispatcher1 = _director->getEventDispatcher();
node4=nullptr;
node2=nullptr;
p=nullptr;
node5=Node::create();
node6=nullptr;
}
static cocos2d::Layer* createLayer();
virtual bool init();
CREATE_FUNC(TheLayer);
void function();
void popScene1(float t);
void onMouseMove(cocos2d::Event *event);
void onMouseUp(cocos2d::Event *event);
};
#endif
TheLayer.cpp as follows:
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include "SimpleAudioEngine.h"
#include "cocos2d.h"
#include "TheLayer.h"
#include "AppDelegate.h"
#define MOUSE_BUTTON_LEFT 0
#define MOUSE_BUTTON_RIGHT 1
#define MOUSE_BUTTON_MIDDLE 2
using namespace cocos2d;
using namespace CocosDenshion;
USING_NS_CC;
Layer* TheLayer::createLayer()
{
return TheLayer::create();
}
bool TheLayer::init()
{
this->addChild(node5,5);
node2=Sprite::create("ty.png");
node2->setPosition(Vec2(200,200));
this->addChild(node2);
this->_mouseListener->onMouseMove=CC_CALLBACK_1(TheLayer::onMouseMove,this);
this->_eventDispatcher->addEventListenerWithSceneGraphPriority(this->_mouseListener,node2);
this->_mouseListener1->onMouseUp=CC_CALLBACK_1(TheLayer::onMouseUp,this);
this->_eventDispatcher1->addEventListenerWithSceneGraphPriority(this->_mouseListener1,node5);
return true;
}
void TheLayer::popScene1(float t){
if((i==0)&&(x==m)&&(y==n)){
SimpleAudioEngine::getInstance()->playEffect("yingxiao.mp3", false, 1.0, 1.0, 1.0);
node6=Sprite::create("ty.png");
node6->setPosition(node2->getPosition());
this->addChild(node6,7);
auto moveTo = MoveTo::create(1,Vec2(m,n));
node6->runAction(moveTo);
mm=1;
}
if((i==0)&&(mm==0)){
SimpleAudioEngine::getInstance()->playEffect("yingxiao.mp3", false, 1.0, 1.0, 1.0);
auto moveTo = MoveTo::create(1,Vec2(m,n));
node4->runAction(moveTo);
}
x=m;
y=n;
nn=0;
}
void TheLayer::onMouseMove(cocos2d::Event *event){
if(i==0){
node4=Sprite::create("ty.png");
node4->setPosition(node2->getPosition());
this->addChild(node4);
this->p=(EventMouse*)event;
m=p->getCursorX();
n=p->getCursorY();
log("qqqqq");
if(nn==0){
_scheduler11->schedule(CC_SCHEDULE_SELECTOR(TheLayer::popScene1),this,1.0f,false);
nn=1;
mm=0;
}
}
}
void TheLayer::onMouseUp(cocos2d::Event *event){
i=1;
Scene* pScene = HelloWorld::createScene();
Director::getInstance()->replaceScene(pScene);
}