PuzzleSDK
Paddle.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#include "Paddle.h"
26
28
30{
31}
32
34{
35}
36
38{
39 auto s = getTexture()->getContentSize();
40 return Rect(-s.width / 2, -s.height / 2, s.width, s.height);
41}
42
43Paddle* Paddle::createWithTexture(Texture2D* aTexture)
44{
45 Paddle* pPaddle = new (std::nothrow) Paddle();
46 pPaddle->initWithTexture(aTexture);
47 pPaddle->autorelease();
48
49 return pPaddle;
50}
51
52bool Paddle::initWithTexture(Texture2D* aTexture)
53{
54 if( Sprite::initWithTexture(aTexture) )
55 {
57 }
58
59 return true;
60}
61
63{
64 Sprite::onEnter();
65
66 // Register Touch Event
67 auto listener = EventListenerTouchOneByOne::create();
68 listener->setSwallowTouches(true);
69
70 listener->onTouchBegan = CC_CALLBACK_2(Paddle::onTouchBegan, this);
71 listener->onTouchMoved = CC_CALLBACK_2(Paddle::onTouchMoved, this);
72 listener->onTouchEnded = CC_CALLBACK_2(Paddle::onTouchEnded, this);
73
74 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
75}
76
78{
79// auto director = Director::getInstance();
80// director->getTouchDispatcher()->removeDelegate(this);
81 Sprite::onExit();
82}
83
85{
86 return getRect().containsPoint(convertTouchToNodeSpaceAR(touch));
87}
88
89bool Paddle::onTouchBegan(Touch* touch, Event* event)
90{
91 CCLOG("Paddle::onTouchBegan id = %d, x = %f, y = %f", touch->getID(), touch->getLocation().x, touch->getLocation().y);
92
93 if (_state != kPaddleStateUngrabbed) return false;
94 if ( !containsTouchLocation(touch) ) return false;
95
97 CCLOG("return true");
98 return true;
99}
100
101void Paddle::onTouchMoved(Touch* touch, Event* event)
102{
103 // If it weren't for the TouchDispatcher, you would need to keep a reference
104 // to the touch from touchBegan and check that the current touch is the same
105 // as that one.
106 // Actually, it would be even more complicated since in the Cocos dispatcher
107 // you get Sets instead of 1 UITouch, so you'd need to loop through the set
108 // in each touchXXX method.
109
110 CCLOG("Paddle::onTouchMoved id = %d, x = %f, y = %f", touch->getID(), touch->getLocation().x, touch->getLocation().y);
111
112 CCASSERT(_state == kPaddleStateGrabbed, "Paddle - Unexpected state!");
113
114 auto touchPoint = touch->getLocation();
115
116 setPosition( Vec2(touchPoint.x, getPosition().y) );
117}
118
120{
121 Paddle* ret = Paddle::createWithTexture(_texture);
122 ret->_state = _state;
123 ret->setPosition(getPosition());
124 ret->setAnchorPoint(getAnchorPoint());
125 return ret;
126}
127
128void Paddle::onTouchEnded(Touch* touch, Event* event)
129{
130 CCASSERT(_state == kPaddleStateGrabbed, "Paddle - Unexpected state!");
131
133}
USING_NS_CC
Definition: Paddle.cpp:27
@ kPaddleStateUngrabbed
Definition: Paddle.h:34
@ kPaddleStateGrabbed
Definition: Paddle.h:33
Definition: Paddle.h:38
bool initWithTexture(cocos2d::Texture2D *aTexture) override
Definition: Paddle.cpp:52
cocos2d::Rect getRect()
Definition: Paddle.cpp:37
void onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event)
Definition: Paddle.cpp:128
bool containsTouchLocation(cocos2d::Touch *touch)
Definition: Paddle.cpp:84
virtual void onEnter() override
Definition: Paddle.cpp:62
virtual Paddle * clone() const override
Definition: Paddle.cpp:119
void onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event)
Definition: Paddle.cpp:101
PaddleState _state
Definition: Paddle.h:39
Paddle()
Definition: Paddle.cpp:29
virtual void onExit() override
Definition: Paddle.cpp:77
virtual ~Paddle()
Definition: Paddle.cpp:33
bool onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event)
Definition: Paddle.cpp:89
static Paddle * createWithTexture(cocos2d::Texture2D *aTexture)
Definition: Paddle.cpp:43