1 // a selector callback
2 void menuCloseCallback(Object* pSender);
3
4 auto closeItem = MenuItemImage::create("CloseNormal.png",CloseSelected.png", 5 CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
6
7 void HelloWorld::menuCloseCallback(Object* pSender)
8 {
9 Director::getInstance()->end();
10
11 #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
12 exit(0);
13 #endif
14 }
Listener的加入,使得Sprite可以很方便的就可以拥有处理点击事件的能力。再也不用为了写个能够响应事件的Sprite而让去继承Layer了。总觉得class TestSprite : public CCLayer这种定义Sprite的方式是坑人的一种做法。终于可以将其摒弃了。
1
#ifndef __Touchable_Sprite_Test_H__
#define __Touchable_Sprite_Test_H__
4 #include
cocos2d.h"
5 USING_NS_CC;
class TouchableSpriteTest :
public Layer
9 public:
10 CREATE_FUNC(TouchableSpriteTest);
11 virtual void onEnter()
override;
12 void onExit()
13 };
14
15 #endif
1 #include
TouchableSpriteTest.h 2
3 void TouchableSpriteTest::onEnter()
4 {
5 Layer::onEnter();
6 Point origin = Director::getInstance()->
getVisibleOrigin();
7 Size size = Director::getInstance()->
getVisibleSize();
8
9 auto containerForSprite1 =
Node::create();
10 this->addChild(containerForSprite1,128); line-height:1.5!important">10
);
11 auto sprite1 = Sprite::create(
images/CyanSquare.png" );
12 sprite1->setPosition( origin + Point( size.width *
0.5,size.height *
0.5) + Point( -
80,128); line-height:1.5!important">80
) );
13 containerForSprite1->
addChild(sprite1);
15 auto sprite2 = Sprite::create(
images/MagentaSquare.png");
16 sprite2->setPosition(origin+Point(size.width/
2,size.height/
2));
17 addChild(sprite2,128); line-height:1.5!important">20
);
18
19 auto sprite3 = Sprite::create(
images/YellowSquare.png20 sprite3->setPosition(Point(
0,128); line-height:1.5!important">0
));
21 sprite2->addChild(sprite3,128); line-height:1.5!important">1
);
22 Make sprite1 touchable
23 auto listener1 =
EventListenerTouchOneByOne::create();
24
25 listener1->setSwallowTouches(
true);
26
27 listener1->onTouchBegan = [](Touch* touch,Event*
event){
28 auto target = static_cast<Sprite*>(
event->
getCurrentTarget());
29
30 Point locationInNode = target->convertToNodeSpace(touch->
getLocation());
31 Size s = target->
getContentSize();
32 Rect rect = Rect(
0,s.width,s.height);
33
34 if (rect.containsPoint(locationInNode))
35 {
36 log(
sprite began... x = %f,y = %f37 target->setOpacity(
180);
38 return true;
39 }
40 false;
41 };
42
43 listener1->onTouchMoved = [](Touch* touch,128); line-height:1.5!important">44 auto target = static_cast<Sprite*>(
45 target->setPosition(target->getPosition() + touch->
getDelta());
46 };
47
48 listener1->onTouchEnded = [=](Touch* touch,128); line-height:1.5!important">49 auto target = static_cast<Sprite*>(
50 log(
sprite onTouchesEnded.. 51 target->setOpacity(
255);
52 if (target ==
sprite2)
53 {
54 containerForSprite1->setLocalZOrder(
100);
55 }
56 else if(target ==
sprite1)
57 {
58 containerForSprite1->setLocalZOrder(
59 }
60 };
61
62 _eventDispatcher->
addEventListenerWithSceneGraPHPriority(listener1,sprite1);
63 _eventDispatcher->addEventListenerWithSceneGra
PHPriority(listener1->
clone(),sprite2);
64 _eventDispatcher->addEventListenerWithSceneGra
PHPriority(listener1->
clone(),sprite3);
65
66 Rect s_visibleRect = Director::getInstance()->getOpenGLView()->
getVisibleRect();
67 Point right = Point(s_visibleRect.origin.x+s_visibleRect.size.width,s_visibleRect.origin.y+s_visibleRect.size.height/
2);
68
69 auto removeAllTouchItem = MenuItemFont::create(
Remove All Touch Listeners sender ){
70 auto senderItem = static_cast<MenuItemFont*>
(sender);
71 senderItem->setString(
Only next item could be clicked 72
73 _eventDispatcher->
removeEventListenersForType( EventListener::Type::TOUCH_ONE_BY_ONE );
74
75 auto nextItem = MenuItemFont::create(
Next sender){
76 });
77
78 nextItem->setFontSizeObj(
16);
79 nextItem->setPosition(right + Point(-
100,-
30));
80
81 auto menu2 =
Menu::create(nextItem,NULL);
82 menu2->setPosition(Point(
83 menu2->setAnchorPoint(Point(
84 this->
addChild(menu2);
85 });
86 removeAllTouchItem->setFontSizeObj(
87 removeAllTouchItem->setPosition(right + Point(-
88
89 auto menu =
Menu::create(removeAllTouchItem,nullptr);
90 menu->setPosition(Point(
91 menu->setAnchorPoint(Point(
92 addChild(menu);
93 }
94
95 void TouchableSpriteTest::onExit()
96 {
97 Layer::onExit();
98 }