CocosCreator引擎修改js源代码

来源:互联网 发布:java类的定义 编辑:程序博客网 时间:2024/06/06 05:42
最近在用CocosCreator引擎做项目,有个需求是一个卡牌同时执行两个动作,一个repearForever的跳着向前移动,还有一个卡牌翻转的动作。

有个需求是使用道具让卡牌停止跳着移动,而卡牌可以翻转。持续5s后,卡牌继续跳着向前。

试了一些方法,也百度了一些方法,并看了源码,没找到合适的引擎自带方法。

有说stopActionBrTag(TAG);然后重新runAction()该动作的,但实际并不是想要的效果,比如牌刚好跳在最高点时,再次runAction()的时候,会从最高点开始运动。

如果纪录position的话,会瞬移,而且有其他动作也在改变positoin的话,控制起来会很困难。

于是改了下源代码。

其中运行到真机时的c++代码和js-bindings代码已经写好(具体步骤在我的另一篇文章:http://blog.csdn.net/u010536615/article/details/52932454)。

发现在网页上调用时提示找不到该方法,于是发现CocosCreator在网页上运行和真机上运行用的是两套代码。

这里以我的需求为例子,说下怎么修改CocosCreator在网页上运行时的源代码。我用的是mac版的CocoCreator,Windows版的请找到对应目录及文件。

----1.应用程序->CocosCreator->右击->显示包内容->Contents/Resources/engine/bin/cocos2d-js-for-preview.js

----2.cc.Aciton类中添加变量(大概文件13954行):

            ctor: function() {
                this.originalTarget = null;
                this.target = null;
                this.tag = cc.Action.TAG_INVALID;
               
this.evanFlag = false;//新增evanFlag变量
            },

----3.cc.Aciton类中添加方法(我加在了setTag()方法之后,大概文件13997行):

           setEvanFlag:function (_evanFlag) {
                this.evanFlag = _evanFlag;
            },

----4.cc.ActionManager类中修改update()方法(大概文件21090行):

                            if (locCurrTarget.currentAction.evanFlag) {
                                continue;
                            }

修改后update方法为:

update: function(dt) {
                var locTargets = this._arrayTargets, locCurrTarget;
                for (var elt = 0; elt < locTargets.length; elt++) {
                    this._currentTarget = locTargets[elt];
                    locCurrTarget = this._currentTarget;
                    if (!locCurrTarget.paused) {
                        for (locCurrTarget.actionIndex = 0; locCurrTarget.actionIndex < (locCurrTarget.actions ? locCurrTarget.actions.length : 0); locCurrTarget.actionIndex++) {
                            locCurrTarget.currentAction = locCurrTarget.actions[locCurrTarget.actionIndex];
                            if (!locCurrTarget.currentAction) {
                                continue;
                            }
                            locCurrTarget.currentActionSalvaged = false;
                            //evanFlag
                            if (locCurrTarget.currentAction.evanFlag) {
                                continue;
                            }
                            locCurrTarget.currentAction.step(dt * (locCurrTarget.currentAction._speedMethod ? locCurrTarget.currentAction._speed : 1));
                            if (locCurrTarget.currentActionSalvaged) {
                                locCurrTarget.currentAction = null;
                            } else {
                                if (locCurrTarget.currentAction.isDone()) {
                                    locCurrTarget.currentAction.stop();
                                    var action = locCurrTarget.currentAction;
                                    locCurrTarget.currentAction = null;
                                    this.removeAction(action);
                                }
                            }
                            locCurrTarget.currentAction = null;
                        }
                    }
                    this._currentTargetSalvaged && 0 === locCurrTarget.actions.length && this._deleteHashElement(locCurrTarget) && elt--;
                }
            }

----5.调用:

        var action = cc.director.getActionManager().getActionByTag(9, this.node);
        action.setEvanFlag(true);//暂停

       action.setEvanFlag(true);//继续


0 0