2012-04-26 7 views
5

संपादित करें: पर्याप्त पुनर्लेखन और टिप्पणी के साथ, मैं इसे चला रहा हूं, दूसरों के लिए अंतिम पोस्ट करेगा।cocos2d-x के लिए CCScrollView जो काम करता है?

EDIT2: मैं और अधिक मेरे अपने संस्करण को अद्यतन करने के लिए किया गया है, और मैंने सीखा है मूल सांकेतिक शब्दों में बदलनेवाला सी ++ के साथ बहुत अच्छा नहीं था, तो आप नीचे टिप्पणी की जाँच, उपयोग करने से पहले वहाँ में सब कुछ पर जाने के लिए चाहते हो सकता है पोस्ट

वर्तमान में उपलब्ध सीसीएसक्रॉल व्यू कोकोस 2 डी-एक्स के लिए एक प्रमुख दोष से पीड़ित है: यह गड़बड़ है।

विशेष रूप से, सीपीपी फ़ंक्शन हेडर .h फ़ाइल से मेल नहीं खाते हैं। सीपीपी फ़ाइल UIEvent और NSMutableArray को संदर्भित करती है, एक्स-प्लेटफ़ॉर्म नहीं।

स्क्रॉल व्यू में स्वयं एक सरणी जोड़ा जाना चाहिए, जो आपके मेनू आइटम (यदि मेनू आइटम बनाते हैं) बनाने के लिए लूप के लिए उपयोग करने की आपकी क्षमता को सीमित करता है, क्योंकि आप एक म्यूटेबल का उपयोग नहीं कर सकते हैं, और वे नहीं आईओएस की तरह एक-दूसरे को उप-वर्ग करें ताकि आप केवल एक म्यूटेबल का उपयोग न कर सकें, फिर CCArray * array = mutable-> getMutableArray() या इसी तरह के कहें।

इसके अलावा, परिभाषा पुरानी है (हालांकि cocos2d-x साइट पर अनुवाद नियमों के माध्यम से CCX_SAFE_DELETE जोड़ने के माध्यम से तय करने योग्य), और विभिन्न प्रकार के यादृच्छिक टेम्पलेट्स एनएस और यूआई ऑब्जेक्ट्स बनाने की कोशिश कर रहे हैं जो परियोजना के भीतर भी नहीं हैं मेरे पास कोई सुराग नहीं है क्यों।

मूल विचार यह है कि, परिवर्तनीय सरणी का उपयोग करने के अलावा, यह वर्ग सी ++ में अनुवाद करने के लिए परेशान है। मुझे विश्वास नहीं है कि लेखक ने अपने संस्करण को संकलित किया है, क्योंकि शीर्षलेख मेल नहीं खाते हैं। मैंने सोचा कि मैंने दोनों को डाउनलोड किया है (प्रत्येक में से 2) सीपीपी और। एच फाइलें हैं, और दोनों के पास अन्य मुद्दों के साथ सीसी के बजाय UIEvent है।

नोट: मैंने उपरोक्त में से कई प्रकार तय किए हैं, और विभिन्न स्थानों पर चयनकर्ता प्रोटोकॉल को सीसीओब्जेक्ट में बदल दिया है, लेकिन यह केवल भारी हो रहा है।

शायद मुझे इसके बजाय लंबैक्स नोड का उपयोग करना चाहिए?

यह सिर्फ हास्यास्पद है, शायद यह कोकोस 2 डी के लिए अच्छा है लेकिन यह c2d-X पर काम नहीं कर रहा है। वैसे भी, मैं रिकोडिंग और खोज कर रहा हूं, किसी भी सुझाव के लिए धन्यवाद!

संपादित करें: यहां कक्षाएं हैं जिनका मैं अब उपयोग कर रहा हूं। संपादन पूरा नहीं है, लेकिन इन किसी को भी मिल जाएगा शुरू कर दिया है, और अधिक बस iPhone

हैडर से पर संकलन:

#ifndef __CCSCROLLLAYER__ 
#define __CCSCROLLLAYER__ 
// CCScrollLayer.h 
// 
// Copyright 2010 DK101 
// http://dk101.net/2010/11/30/implementing-page-scrolling-in-cocos2d/ 
// 
// Copyright 2010 Giv Parvaneh. 
// http://www.givp.org/blog/2010/12/30/scrolling-menus-in-cocos2d/ 
// 
// Copyright 2011 Stepan Generalov 
// 
// Copyright 2011 Eli Yukelzon 
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy 
// of this software and associated documentation files (the "Software"), to deal 
// in the Software without restriction, including without limitation the rights 
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
// copies of the Software, and to permit persons to whom the Software is 
// furnished to do so, subject to the following conditions: 
// 
// The above copyright notice and this permission notice shall be included in 
// all copies or substantial portions of the Software. 
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
// THE SOFTWARE. 

// Original source: https://github.com/cocos2d/cocos2d-iphone-extensions/tree/master/Extensions/CCScrollLayer 
// Last updated: October 1, 2011 

#include "cocos2d.h" 

namespace cocos2d { 


    class CCScrollLayer; 


    class CCScrollLayerDelegate 
    { 
    public: 
     /** Called when scroll layer begins scrolling. 
     * Usefull to cancel CCTouchDispatcher standardDelegates. 
     */ 
     virtual void scrollLayerScrollingStarted(CCScrollLayer* sender) {} 

     /** Called at the end of moveToPage: 
     * Doesn't get called in selectPage: 
     */ 
     virtual void scrollLayerScrolledToPageNumber(CCScrollLayer* sender, unsigned int page) {} 
    }; 

    /* 
    It is a very clean and elegant subclass of CCLayer that lets you pass-in an array 
    of layers and it will then create a smooth scroller. 
    Complete with the "snapping" effect. You can create screens with anything that can be added to a CCLayer. 

    */ 
    class CCScrollLayer : public CCLayer 
    { 

     int currentScreen; //added 
     int totalScreens; 
     float scrollWidth; 
     float scrollHeight; 
     float startWidth; 
     float startHeight; 
     int startSwipe; 

    public: 
     //CCScrollLayer(); 
     ~CCScrollLayer(); 

     static CCScrollLayer* nodeWithLayers(CCArray* layers, int widthOffset); 

     bool initWithLayers(CCArray* layers, int widthOffset); 

     /** Updates all pages positions & adds them as children if needed. 
     * Can be used to update position of pages after screen reshape, or 
     * for update after dynamic page add/remove. 
     */ 
     void updatePages(); 

     /** Adds new page and reorders pages trying to set given number for newly added page. 
     * If number > pages count - adds new page to the right end of the scroll layer. 
     * If number <= 0 - adds new page to the left end of the scroll layer. 
     * @attention Designated addPage method. 
     */ 
     void addPage(CCLayer* aPage, unsigned int pageNumber); 

     /** Adds new page to the right end of the scroll layer. */ 
     void addPage(CCLayer* aPage); 

     /** Removes page if it's one of scroll layers pages (not children) 
     * Does nothing if page not found. 
     */ 
     void removePage(CCLayer* aPage); 

     /** Removes page with given number. Doesn nothing if there's no page for such number. */ 
     void removePageWithNumber(unsigned int pageNumber); 

     /* Moves scrollLayer to page with given number & invokes delegate 
     * method scrollLayer:scrolledToPageNumber: at the end of CCMoveTo action. 
     * Does nothing if number >= totalScreens or < 0. 
     */ 
     void moveToPage(unsigned int pageNumber); 

     /* Immedeatly moves scrollLayer to page with given number without running CCMoveTo. 
     * Does nothing if number >= totalScreens or < 0. 
     */ 
     void selectPage(unsigned int pageNumber); 

     CC_SYNTHESIZE(CCScrollLayerDelegate*, m_pDelegate, Delegate); 

     /** Calibration property. Minimum moving touch length that is enough 
     * to cancel menu items and start scrolling a layer. 
     */ 
     CC_SYNTHESIZE(CGFloat, m_fMinimumTouchLengthToSlide, MinimumTouchLengthToSlide); 

     /** Calibration property. Minimum moving touch length that is enough to change 
     * the page, without snapping back to the previous selected page. 
     */ 
     CC_SYNTHESIZE(CGFloat, m_fMinimumTouchLengthToChangePage, MinimumTouchLengthToChangePage); 

     /** If YES - when starting scrolling CCScrollLayer will claim touches, that are 
     * already claimed by others targetedTouchDelegates by calling CCTouchDispatcher#touchesCancelled 
     * Usefull to have ability to scroll with touch above menus in pages. 
     * If NO - scrolling will start, but no touches will be cancelled. 
     * Default is YES. 
     */ 
     CC_SYNTHESIZE(bool, m_bStealTouches, StealTouches); 

     /** Whenever show or not white/grey dots under the scroll layer. 
     * If yes - dots will be rendered in parents transform (rendered after scroller visit). 
     */ 
     CC_SYNTHESIZE(bool, m_bShowPagesIndicator, ShowPagesIndicator); 

     /** Position of dots center in parent coordinates. 
     * (Default value is screenWidth/2, screenHeight/4) 
     */ 
     CC_SYNTHESIZE_PASS_BY_REF(CCPoint, m_tPagesIndicatorPosition, PagesIndicatorPosition); 

     /** Total pages available in scrollLayer. */ 
     unsigned int getTotalScreens() const; 

     /** Current page number, that is shown. Belongs to the [0, totalScreen] interval. */ 
     CC_SYNTHESIZE_READONLY(unsigned int, m_uCurrentScreen, CurrentScreen); 

     /** Offset, that can be used to let user see next/previous page. */ 
     CC_SYNTHESIZE(CGFloat, m_fPagesWidthOffset, PagesWidthOffset); 

     /** Offset that can be used to let user see empty space over first or last page. */ 
     CC_SYNTHESIZE(CGFloat, m_fMarginOffset, MarginOffset); 

     /** Array of pages CCLayer's */ 
     CC_SYNTHESIZE_READONLY(CCArray*, m_pLayers, Pages); 
    protected: 
     // The x coord of initial point the user starts their swipe. 
     CGFloat m_fStartSwipe; 

     // Internal state of scrollLayer (scrolling or idle). 
     int m_iState; 
     bool m_bStealingTouchInProgress; 
     // Holds the touch that started the scroll 
     CCTouch* m_pScrollTouch; 

     //void visit(); 
     //void moveToPageEnded(); 
     unsigned int pageNumberForPosition(const CCPoint& position); 
     CCPoint positionForPageWithNumber(unsigned int pageNumber); 
     void claimTouch(CCTouch* pTouch); 
     void cancelAndStoleTouch(CCTouch* pTouch, CCEvent* pEvent); 

     //void registerWithTouchDispatcher(); 
     bool ccTouchBegan(CCTouch* pTouch, CCEvent* pEvent); 
     void ccTouchMoved(CCTouch* pTouch, CCEvent* pEvent); 
     void ccTouchEnded(CCTouch* pTouch, CCEvent* pEvent); 
     //void ccTouchCancelled(CCTouch* pTouch, CCEvent* pEvent); 
     }; 
} //end namespace 

#endif 

सीपीपी

// CCScrollLayer.cpp 
// Museum 
// 
// Created by GParvaneh on 29/12/2010. 
// Copyright 2010. All rights reserved. 
// Ported to C++ by Lior Tamam on 03/04/2011 
#include "CCScrollLayer.h" 

using namespace cocos2d; 

CCScrollLayer* CCScrollLayer::nodeWithLayers(CCArray* layers, int widthOffset) 
{ 
    CCScrollLayer *pRet = new CCScrollLayer(); 
    if (pRet && pRet->initWithLayers(layers, widthOffset)) 
    { 
     pRet->autorelease(); 
     return pRet; 
    } 
    CCX_SAFE_DELETE(pRet); 
    return NULL; 
} 

bool CCScrollLayer::initWithLayers(CCArray* layers, int widthOffset) 
{ 
    if (CCLayer::init()) 
    {  
     // Make sure the layer accepts touches 
     CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this,0,true); 

     // Set up the starting variables 
     //if(!widthOffset) 
     { 
     // widthOffset = 0; 
     } 
     currentScreen = 1; 

     // offset added to show preview of next/previous screens 
     scrollWidth = (int)CCDirector::sharedDirector()->getWinSize().width - widthOffset; 
     scrollHeight = (int)CCDirector::sharedDirector()->getWinSize().height; 
     startWidth = scrollWidth; 
     startHeight = scrollHeight; 

     // Loop through the array and add the screens 
     unsigned int i; 
     for (i=0; i<layers->count(); i++) 
     { 
      CCLayer* l = (CCLayer*)layers->objectAtIndex(i); 
      //l->setAnchorPoint(ccp(0,0)); 
      //l->setPosition(ccp((i*scrollWidth),0)); 
      addChild(l);    
     } 

     // Setup a count of the available screens 
     totalScreens = layers->count(); 
     return true;  
    } 
    else 
    { 
     return false; 
    } 
} 

void CCScrollLayer::setMaximumScrollHeight(float maxHeight) 
{ 
    //Make the offset match expected pixels (include the current screen if at ccp(0,0) 
    maxHeight -= CCDirector::sharedDirector()->getWinSize().height; 
    maximumScrollHeight = maxHeight; 
} 

CCScrollLayer::~CCScrollLayer() 
{ 
    CCTouchDispatcher::sharedDispatcher()->removeDelegate(this); 
    CCLayer::onExit(); 
} 

bool CCScrollLayer::ccTouchBegan(CCTouch *touch, CCEvent *withEvent) 
{ 
// 
// CCPoint touchPoint = touch->locationInView(); 
// touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint); 
// 
// startSwipe = (int)touchPoint.y; 
    return true; 
} 

void CCScrollLayer::ccTouchMoved(CCTouch *touch, CCEvent *withEvent) 
{ 
    CCPoint touchPoint = touch->locationInView(); 
    CCPoint prevPoint = touch->previousLocationInView(); 

    touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint); 
    prevPoint = CCDirector::sharedDirector()->convertToGL(prevPoint); 

    CCPoint difference = ccp(touchPoint.x - prevPoint.x , touchPoint.y - prevPoint.y); 
    CCPoint currentPos = this->getPosition(); 

    currentPos = ccp(currentPos.x, currentPos.y+difference.y); 

    if (currentPos.y > maximumScrollHeight) 
    { 
     currentPos.y = maximumScrollHeight; 
     //this->setPositionY(maximumScrollHeight); 
    } 
    else if (currentPos.y < 0) 
    { 
     currentPos.y = 0; 
     // this->setPositionY(0); 
    } 
    this->setPosition(currentPos); 
} 

/* 
void CCScrollLayer::ccTouchEnded(CCTouch *touch, CCEvent *withEvent) 
{ 

    //CCPoint touchPoint = touch->locationInView(); 
    //touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint); 

    int newX = (int)touchPoint.x; 

    if ((newX - startSwipe) < -scrollWidth/3 && (currentScreen+1) <= totalScreens) 
    { 
    // this->moveToNextPage(); 
    } 
    else if ((newX - startSwipe) > scrollWidth/3 && (currentScreen-1) > 0) 
    { 
    // this->moveToPreviousPage(); 
    } 
    else 
    { 
    // this->moveToPage(currentScreen);   
    } 

} 
*/ 
+0

मैं अभी तक अपने प्रश्न का उत्तर नहीं दे सकता, जब संभव हो तो उत्तर ले जाएगा। –

+0

अपने सीसीएमएक्रोस पर http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Rules_of_translating_objc_to_cpp से CCX_SAFE_DELETE() के लिए परिभाषित करने के लिए याद रखें।एच क्षेत्र –

+0

सीपीपी फ़ाइल में, initWithLayers() के तहत, आप // l-> setAnchorPoint (ccp (0,0)) को मिटाना या टिप्पणी करना चाहते हैं; और // एल-> सेटपोजिशन (सीसीपी ((i * scrollWidth), 0)); काम करने के लिए स्थिति पाने के लिए। साथ ही, if! WidthOffset को हटाएं, यह या तो खुद को 0 पर रीसेट करता है या यह है कि चेक के पास कोई मूल्य नहीं है। शायद ओबीजे से पोर्टिंग के साथ जारी करें> इसके बाद, यह सही दिखने लगेगा। साथ ही, पेजिंग सामान को मिटाना अगर आप सिर्फ स्क्रॉलिंग व्यू –

उत्तर

4

चेक आउट Cocos2d-x की साइट पर मंचों नवीनतम एक इस पर आधारित लेखक द्वारा उन्हें अभी पुन: कार्य किया जा रहा है। http://www.cocos2d-x.org/boards/18/topics/1090?r=10835#message-10835

+0

एक आखिरी बात: 2 डी-एक्स स्क्रॉल व्यू वास्तव में पृष्ठों की एक सरणी (शीर्ष स्तर नोड्स) की अपेक्षा करता है। इसे ध्यान में रखें यदि आप इसे फिर से लिखते हैं या साइट पर जो कुछ भी है (मेरे संस्करण के अलावा) का उपयोग करें। हालांकि मेरा संस्करण इस तरह काम करेगा, यह जटिलता –

9

नई जानकारी जोड़ने के लिए, किसी को भी, जो एक खोज इंजन के माध्यम से इस पाया के लिए एक पुराने धागा को फिर से जीवित: CCScrollView अब जीयूआई विस्तार का हिस्सा है और काफी अच्छी तरह से काम करने के लिए लगता है। ऊपर उल्लिखित कार्यान्वयन के लिए और आवश्यकता नहीं होनी चाहिए।

+0

आह के लिए उपयुक्त नहीं है, अच्छा। धन्यवाद! –

+0

@StephenJ क्या आप इसे स्वीकृत उत्तर के रूप में चिह्नित कर सकते हैं? यह आपके द्वारा उल्लिखित समस्या हल करता है। इसके अलावा यह 2 डी-एक्स के मूल निवासी से जुड़ा हुआ है। – Waseem

+0

इसके साथ कई समस्याएं हैं भले ही आप वर्तमान में सही हैं। पहला, मुझे बस इतना पहले गिरा दिया गया है क्योंकि "नया उत्तर" अब सही है। दूसरा, मैं समय पर किसी भी समय, मृत्यु, चोट, या बस चलने से सदस्य बन सकता हूं या नहीं। कोई भी कर सकता है, इसलिए मेरा मानना ​​है कि इस तरह की चीजें होने पर मॉडरेटर को एक और उपयुक्त समाधान मिलना चाहिए। हालांकि मैं यहां कहूंगा, यह वर्तमान सही जवाब है! –