跨平台 – CCScrollView for cocos2d-x有效吗?

编辑:有足够的重写和评论,我已经运行,将在下面发布最终其他人.

编辑2:我一直在更新我自己的版本,而且我已经了解到原来的编码器对C不是很好,你可能想在使用它之前查看其中的所有内容,请查看下面的评论

目前可用的CCScrollView for cocos2d-x存在一个主要缺陷:它搞砸了.

具体来说,cpp函数头与.h文件不匹配.
cpp文件引用UIEvent和NSMutableArray,而不是X-Platform.

滚动视图本身必须添加一个数组,这限制了您使用for循环创建菜单项(如果创建菜单项)的能力,因为您不能使用可变的,并且它们不会像iOS所以你不能只使用一个mutable然后说CCArray * array = mutable-> getMutableArray()或类似的.

此外,定义已过时(虽然通过cocos2d-x网站上的转换规则添加CCX_SAFE_DELETE可以修复),以及各种随机模板试图创建甚至不在项目中的NS和UI对象我也没有线索为什么.

基本的想法是,除了使用可变数组之外,这个类太乱了,无法转换为C语言.我不相信作者编译了他的版本,因为标题不匹配.我以为我下载了两个(每个都有2个)cpp和.h文件,并且都有UIEvent而不是CC以及其他问题.

注意:我已经修复了上面的各种内容,并在各个地方将SelectorProtocol更改为CCObject,但它只是变得势不可挡.

也许我应该使用Parallax节点?

这太荒谬了,也许它对Cocos2d来说很棒,但它不适用于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

CPP

//  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,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);        
    }   

}
*/
对于通过搜索引擎找到此内容的任何人来说,恢复旧线程以添加新信息: CCScrollView现在是GUI扩展的一部分,并且似乎运行得相当好.不再需要上面提到的实现.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


    本文实践自 RayWenderlich、Ali Hafizji 的文章《How To Create Dynamic Textures with CCRenderTexture in Cocos2D 2.X》,文中使用Cocos2D,我在这里使用Cocos2D-x 2.1.4进行学习和移植。在这篇文章,将会学习到如何创建实时纹理、如何用Gimp创建无缝拼接纹
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@163.com微信公众号:HopToad 欢迎转载,转载标注出处:http://blog.csdn.netotbaron/article/details/424343991.  软件准备 下载地址:http://cn.cocos2d-x.org/download 2.  简介2.1         引用C
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从Cocos2D-x官网上下载,进入网页http://www.cocos2d-x.org/download,点击Cocos2d-x以下的Download  v3.0,保存到自定义的文件夹2:从python官网上下载。进入网页https://www.python.org/downloads/,我当前下载的是3.4.0(当前最新
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发引擎,易学易用,支持多种智能移动平台。官网地址:http://cocos2d-x.org/当前版本:2.0    有很多的学习资料,在这里我只做为自己的笔记记录下来,错误之处还请指出。在VisualStudio2008平台的编译:1.下载当前稳
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《最强大脑》娱乐节目。将2048改造成一款挑战玩家对数字记忆的小游戏。邮箱:appdevzw@163.com微信公众号:HopToadAPK下载地址:http://download.csdn.net/detailotbaron/8446223源码下载地址:http://download.csdn.net/
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试以QtCreatorIDE来进行CMake构建。Cocos2d-x3.X地址:https://github.com/cocos2d/cocos2d-x1.打开QtCreator,菜单栏→"打开文件或项目...",打开cocos2d-x目录下的CMakeLists.txt文件;2.弹出CMake向导,如下图所示:设置
 下载地址:链接:https://pan.baidu.com/s/1IkQsMU6NoERAAQLcCUMcXQ提取码:p1pb下载完成后,解压进入build目录使用vs2013打开工程设置平台工具集,打开设置界面设置: 点击开始编译等待编译结束编译成功在build文件下会出现一个新文件夹Debug.win32,里面就是编译
分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net前言上次用象棋演示了cocos2dx的基本用法,但是对cocos2dx并没有作深入的讨论,这次以超级马里奥的源代码为线索,我们一起来学习超级马里奥的实
1. 圆形音量button事实上作者的本意应该是叫做“电位计button”。可是我觉得它和我们的圆形音量button非常像,所以就这么叫它吧~先看效果:好了,不多解释,本篇到此为止。(旁白: 噗。就这样结束了?)啊才怪~我们来看看代码:[cpp] viewplaincopyprint?CCContro
原文链接:http://www.cnblogs.com/physwf/archive/2013/04/26/3043912.html为了进一步深入学习贯彻Cocos2d,我们将自己写一个场景类,但我们不会走的太远,凡是都要循序渐进,哪怕只前进一点点,那也至少是前进了,总比贪多嚼不烂一头雾水的好。在上一节中我们建
2019独角兽企业重金招聘Python工程师标准>>>cocos2d2.0之后加入了一种九宫格的实现,主要作用是用来拉伸图片,这样的好处在于保留图片四个角不变形的同时,对图片中间部分进行拉伸,来满足一些控件的自适应(PS: 比如包括按钮,对话框,最直观的形象就是ios里的短信气泡了),这就要求图
原文链接:http://www.cnblogs.com/linji/p/3599478.html1.环境和工具准备Win7VS2010/2012,至于2008v2版本之后似乎就不支持了。 2.安装pythonv.2.0版本之前是用vs模板创建工程的,到vs2.2之后就改用python创建了。到python官网下载版本2.7.5的,然后
环境:ubuntu14.04adt-bundle-linux-x86_64android-ndk-r9d-linux-x86_64cocos2d-x-3.0正式版apache-ant1.9.3python2.7(ubuntu自带)加入环境变量exportANDROID_SDK_ROOT=/home/yangming/adt-bundle-linux/sdkexportPATH=${PATH}:/$ANDROID_SDK_ROOTools/export
1开发背景游戏程序设计涉及了学科中的各个方面,鉴于目的在于学习与进步,本游戏《FlappyBird》采用了两个不同的开发方式来开发本款游戏,一类直接采用win32底层API来实现,另一类采用当前火热的cocos2d-x游戏引擎来开发本游戏。2需求分析2.1数据分析本项目要开发的是一款游
原文链接:http://www.cnblogs.com/linji/p/3599912.html//纯色色块控件(锚点默认左下角)CCLayerColor*ccc=CCLayerColor::create(ccc4(255,0,0,128),200,100);//渐变色块控件CCLayerGradient*ccc=CCLayerGradient::create(ccc4(255,0,0,
原文链接:http://www.cnblogs.com/linji/p/3599488.html//载入一张图片CCSprite*leftDoor=CCSprite::create("loading/door.png");leftDoor->setAnchorPoint(ccp(1,0.5));//设置锚点为右边中心点leftDoor->setPosition(ccp(240,160));/
为了答谢广大学员对智捷课堂以及关老师的支持,现购买51CTO学院关老师的Cocos2d-x课程之一可以送智捷课堂编写图书一本(专题可以送3本)。一、Cocos2d-x课程列表:1、Cocos2d-x入门与提高视频教程__Part22、Cocos2d-x数据持久化与网络通信__Part33、Cocos2d-x架构设计与性能优化内存优
Spawn让多个action同时执行。Spawn有多种不同的create方法,最终都调用了createWithTwoActions(FiniteTimeAction*action1,FiniteTimeAction*action2)方法。createWithTwoActions调用initWithTwoActions方法:对两个action变量初始化:_one=action1;_two=action2;如果两个a
需要环境:php,luajit.昨天在cygwin上安装php和luajit环境,这真特么是一个坑。建议不要用虚拟环境安装打包环境,否则可能会出现各种莫名问题。折腾了一下午,最终将环境转向linux。其中,luajit的安装脚本已经在quick-cocos2d-x-develop/bin/中,直接luajit_install.sh即可。我的lin
v3.0相对v2.2来说,最引人注意的。应该是对触摸层级的优化。和lambda回调函数的引入(嗯嗯,不枉我改了那么多类名。话说,每次cocos2dx大更新。总要改掉一堆类名函数名)。这些特性应该有不少人研究了,所以今天说点跟图片有关的东西。v3.0在载入图片方面也有了非常大改变,仅仅只是