触摸响应程序问题,调用onTouchBegan就出错

写一个触摸响应测试程序遇到的问题:只要调用onTouchBegan的Event就会出错

只要调用target就会出错,请帮忙看下这个单点触摸的程序哪里写错了?
auto targetTouch = touch->getLocation(); log("targetTouch x=%f y=%f",targetTouch.x,targetTouch.y); //本行出错 Vec2 locationInNode = target->convertToNodeSpace(targetTouch); //上行报错 log("convertToNodeSpace");

==========
代码如下:
HelloWorld.h
`#ifndef HELLOWORLD_SCENE_H
#define HELLOWORLD_SCENE_H

#include “cocos2d.h”
using namespace cocos2d;
/*
typedef enum
{
KLayer1_Tag = 102,
KLayer2_Tag ,
KLayer3_Tag
}LayerTags;
*/

class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();

virtual bool init();

// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);

// implement the "static create()" method manually

virtual  void onEnter() override;
virtual void onExit() override;
// 
virtual bool onTouchBegan(Touch *touch, Event *unused_event);

virtual void onTouchMoved(Touch *Ptouch, Event *Pevent);

virtual void onTouchEnded(Touch *Ptouch, Event *Pevent);

Node* pLayer1, *pLayer2, *pLayer3;

CREATE_FUNC(HelloWorld);

};

#endif // HELLOWORLD_SCENE_H`

HelloWorld.cpp
`#include “HelloWorldScene.h”

USING_NS_CC;

Scene* HelloWorld::createScene()
{
// ‘scene’ is an autorelease object
auto scene = Scene::create();

// 'layer' is an autorelease object
auto 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 ( !Layer::init() )
{
return false;
}

Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();

/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
//    you may modify it.

// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
                                       "CloseNormal.png",
                                       "CloseSelected.png",
                                       CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                            origin.y + closeItem->getContentSize().height/2));

// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);

/////////////////////////////
// 3. add your codes below...

// add a label shows "Hello World"
// create and initialize a label

auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);

// position the label on the center of the screen
label->setPosition(Vec2(origin.x + visibleSize.width/2,
                        origin.y + visibleSize.height - label->getContentSize().height));

// add the label as a child to this layer
this->addChild(label, 1);

// add "HelloWorld" splash screen"
//auto sprite = Sprite::create("HelloWorld.png");

// position the sprite on the center of the screen

// sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

// add the sprite as a child to this layer

// this->addChild(sprite, 0);
auto sprite = Sprite::create(“ArchMageTwister_0011.png”);
sprite->setPosition(Vec2(visibleSize.width / 4, visibleSize.height / 4));
this->addChild(sprite);

//
//auto pLayer1 = LayerColor::create(Color4B::RED, 100, 100);
pLayer1 = LayerColor::create(Color4B::RED, 100, 100);
 //pLayer1 = Sprite::create("CloseSelected.png");
pLayer1->setPosition(Vec2(visibleSize.width / 2 - 100, visibleSize.height / 2 + 100));
addChild(pLayer1);

//auto pLayer2 = LayerColor::create(Color4B::GREEN, 100, 100);
pLayer2 = LayerColor::create(Color4B::GREEN, 100, 100);
// pLayer2 = Sprite::create("CloseNormal.png");
pLayer2->setPosition(Vec2(visibleSize.width / 2 - 60, visibleSize.height / 2 + 60));
addChild(pLayer2);

//auto pLayer3 = LayerColor::create(Color4B::BLUE, 100, 100);
// pLayer3 = Sprite::create("CloseNormal.png");
pLayer3 = LayerColor::create(Color4B::BLUE, 100, 100);
pLayer3->setPosition(Vec2(visibleSize.width / 2 - 20, visibleSize.height / 2 + 20));
addChild(pLayer3);



return true;

}

void HelloWorld::onEnter()
{
Layer::onEnter();
//
log(“HelloWorld::onEnter()”);
auto listener1 = EventListenerTouchOneByOne::create();
listener1->setSwallowTouches(true);

listener1->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
listener1->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
listener1->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);

//


_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, pLayer1);
//log("sprite1 ok");
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), pLayer2);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), pLayer3);

}

void HelloWorld::onExit()
{
Layer::onExit();
log(“HelloWorld::onExit”);
//Director::getInstance()->getEventDispatcher()->removeAllEventListeners();

}

void HelloWorld::menuCloseCallback(Ref* pSender)
{
Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}

bool HelloWorld::onTouchBegan(Touch touch, Event unused_event)
{
log(“onTouchBegan”);
//auto target = static_cast<Sprite
>(unused_event->getCurrentTarget());
auto target = static_cast<Layer
>(unused_event->getCurrentTarget());
if (target = nullptr)
{
return true;
}
//make a test here
Vec2 testPoint;
testPoint.x = 300;
testPoint.y = 150;
Vec2 testPointLocal = target->convertToNodeSpace(testPoint);
log(“testPointLocal x= %f y=%f”, testPointLocal.x, testPointLocal.y);
//
auto targetTouch = touch->getLocation();
log(“targetTouch x=%f y=%f”,targetTouch.x,targetTouch.y);

//Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
Vec2 locationInNode = target->convertToNodeSpace(targetTouch);

log("convertToNodeSpace");
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);

// 点击范围判断检测
if (rect.containsPoint(locationInNode))
{
	log("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y);
	target->setOpacity(180);
	return true;
}
return false;

}

void HelloWorld::onTouchMoved(Touch* Ptouch, Event* Pevent)
{
log(“onTouchMoved”);
auto target = static_cast<Layer*>(Pevent->getCurrentTarget());
target->setPosition(target->getPosition() + Ptouch->getDelta());

}

void HelloWorld::onTouchEnded(Touch* Ptouch, Event* Pevent)
{
log(“onTouchEnded”);
auto target = static_cast<Layer*>(Pevent->getCurrentTarget());

Point locationInNode = target->convertToNodeSpace(Ptouch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);

if (rect.containsPoint(locationInNode))
{
	log("spritex = %f, y = %f ", locationInNode.x, locationInNode.y);
	log("spritetag = %d", target->getTag());
	target->runAction(ScaleTo::create(0.06f, 1.0f));
}

}`