addChild(boxC,30, kBoxC_Tag); ⑥ php
returntrue; html
} 函數
咱們在HelloWorld::init()函數中初始化了場景中的背景和三個方塊精靈。代碼第①~④行是建立並添加背景,圖8-3所示的背景是由一個128x128紋理圖片(BackgroundTile.png)反覆貼圖上,這樣能夠減小內存消耗,在第①行代碼中建立背景精靈對象,注意背景的大小仍然是整個屏幕。第②行代碼是設置貼圖的紋理的參數,Texture2D::TexParams類型是一個結構體。第③行代碼是將參數設置到背景精靈的紋理上。第④行代碼是添加背景精靈到當前層。測試
代碼第⑤~⑥行是建立了三個方塊精靈,在添加它到當前層的時候咱們使用三個參數的addChild(Node* child,int localZOrder,int tag)函數,這樣能夠經過localZOrder參數指定精靈的顯示順序。網站
[html] view plaincopythis
HelloWorldScene.ccp中的HelloWorld::onEnter()代碼以下: spa
void HelloWorld::onEnter() .net
{ code
Layer::onEnter(); orm
log("HelloWorldonEnter");
autolistener = EventListenerTouchOneByOne::create(); ①
listener->setSwallowTouches(true); ②
listener->onTouchBegan= CC_CALLBACK_2(HelloWorld::touchBegan, this); ③
listener->onTouchMoved= CC_CALLBACK_2(HelloWorld::touchMoved,this); ④
listener->onTouchEnded= CC_CALLBACK_2(HelloWorld::touchEnded,this); ⑤
//添加監聽器
EventDispatcher*eventDispatcher = Director::getInstance()->getEventDispatcher(); ⑥
eventDispatcher->addEventListenerWithSceneGraphPriority(listener,
getChildByTag(kBoxA_Tag)); ⑦
eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(),
getChildByTag(kBoxB_Tag)); ⑧
eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(),
getChildByTag(kBoxC_Tag)); ⑨
}
上述代碼第①行是建立一個單點觸摸事件監聽器對象。第②行代碼是設置是否吞沒事件,若是設置爲true,那麼在onTouchBegan函數返回 true 時吞沒事件,事件不會傳遞給下一個Node對象。第③行代碼是設置監聽器的onTouchBegan屬性回調函數。第④行代碼是設置監聽器的onTouchMoved屬性回調函數。第⑤行代碼是設置監聽器的onTouchEnded屬性回調函數。
代碼第⑥~⑨行是添加監聽器,其中第⑦行使用精靈顯示優先級添加事件監聽器,其中參數getChildByTag(kBoxA_Tag)是經過精靈標籤Tag實現得到精靈對象。第⑧行和第⑨行代碼是爲另外兩精靈添加事件監聽器,其中listener->clone()得到listener對象,使用clone()函數是由於每個事件監聽器只能被添加一次,addEventListenerWithSceneGraphPriority和addEventListenerWithFixedPriority會在添加事件監聽器時設置一個註冊標識,一旦設置了註冊標識,該監聽器就不能再用於註冊其它事件監聽了,所以咱們須要使用listener->clone()克隆一個新的監聽器對象,把這個新的監聽器對象用於註冊。
HelloWorldScene.ccp中的觸摸事件回調函數代碼以下:
[html] view plaincopy
bool HelloWorld::touchBegan(Touch*touch, Event* event) ①
{
//獲取事件所綁定的 target
autotarget = static_cast<Sprite*>(event->getCurrentTarget()); ②
PointlocationInNode = target->convertToNodeSpace(touch->getLocation()); ③
Sizes = target->getContentSize(); ④
Rectrect = 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(ScaleBy::create(0.06f,1.06f)); ⑦
returntrue; ⑧
}
returnfalse;
}
void HelloWorld::touchMoved(Touch*touch, Event *event) ⑨
{
log("onTouchMoved");
autotarget = static_cast<Sprite*>(event->getCurrentTarget());
target->setPosition(target->getPosition()+ touch->getDelta()); ⑩
}
void HelloWorld::touchEnded(Touch*touch, Event *event) ⑪
{
log("onTouchEnded");
autotarget = static_cast<Sprite*>(event->getCurrentTarget());
log("spriteonTouchesEnded.. ");
PointlocationInNode = target->convertToNodeSpace(touch->getLocation());
Sizes = target->getContentSize();
Rectrect = 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));
}
}
上代碼第①行是定義回調函數touchBegan。第②行代碼是獲取事件所綁定的精靈對象,其中event->getCurrentTarget()語句返回值是Node對象,static_cast<Sprite*>是強制類型轉換爲Sprite對象。第③行代碼是獲取當前觸摸點相對於target對象的本地座標。第④行代碼是得到target對象的尺寸。第⑤行代碼是經過target對象的尺寸建立Rect變量。第⑥行代碼rect.containsPoint(locationInNode)是判斷是否觸摸點在target對象範圍。第⑦行代碼是放大target對象。第⑧行代碼返回true,表示能夠回調第⑨行touchMoved函數和第⑪行touchEnded函數。第⑩行代碼是移動target對象的位置。
HelloWorldScene.ccp中的HelloWorld::onExit()代碼以下:
[html] view plaincopy
void HelloWorld::onExit()
{
Layer::onExit();
log("HelloWorldonExit");
Director::getInstance()->getEventDispatcher()->removeAllEventListeners();
}
上述HelloWorld::onExit()函數是退出層時候回調,咱們在這個函數中註銷全部的監聽事件。
提示 多點觸摸事件是與具體的平臺有關係的,在Win32平臺下咱們沒法測試多點觸摸。事實上多點觸摸和單點觸摸開發流程基本類似,這裏咱們就再也不贅述了。
更多內容請關注Cocos2d-x系列圖書《Cocos2d-x實戰(卷Ⅰ):C++開發》
本書交流討論網站:http://www.cocoagame.net
歡迎加入cocos2d-x技術討論羣:25776038六、327403678