cocos2d-x JS(ScrollView)

这一节,我将实现游戏的选关界面,游戏50关,每一屏15关,总共4屏,左滑动可切换到上一屏,右滑动可切换到下一屏;

 

效果如下;

开始进入选关界面;

\

 

滑动到最后一屏;

\

 

点击关卡,进入主界面;

\

 

 

 

首先新建,GameSelectLayer.ccbx,GameSelectLayer.js,指定控制器让两个文件关联;设计选关场景如下,调节参数

\

 

创建每个单元格关卡GameSelectItemSprite.ccbx,这是一个小精灵层,如下图;

\

 

\

 

设计完成后,打开MainLayer.js,编写如下:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
GAME_LEVEL = 0 ;
var GameSelectLayer = function () {
     cc.log(GameSelectLayer);
     this .levelScrollPanel = this .levelScrollPanel || {};
     this .pageIndex = 0 ;
     this .pageMax = 4 ;
     this .dotNode = this .dotNode || {};
     this .start = false ;
 
};
 
GameSelectLayer.prototype.onDidLoadFromCCB = function () {
     if (sys.platform == 'browser' ) {
         this .onEnter();
     }
     else {
         this .rootNode.onEnter = function () {
             this .controller.onEnter();
         };
     }
 
     this .rootNode.onExit = function () {
         this .controller.onExit();
     };
 
 
     this .rootNode.onTouchesBegan = function (touches, event) {
         this .controller.onTouchesBegan(touches, event);
         return true ;
     };
 
     this .rootNode.onTouchesMoved = function (touches, event) {
         this .controller.onTouchesMoved(touches, event);
         return true ;
     };
     this .rootNode.onTouchesEnded = function (touches, event) {
         this .controller.onTouchesEnded(touches, event);
         return true ;
     };
     this .rootNode.setTouchEnabled( true );
};
 
GameSelectLayer.prototype.onEnter = function () {
     this .maxColumns = 3 ;
     this .columnSize = 177 ;
     this .maxRows = 5 ;
     this .rowSize = 104 ;
     cc.log(GameSelectLayer);
 
     this .itemDatas = [];
     var itemName = ;
     var levelPan = cc.Layer.create();
     var currentLevel = sys.localStorage.getItem(pb_level);
     if (currentLevel == null ) {
         currentLevel = 0 ;
     }
 
     sys.localStorage.setItem(pb_level_score_1, S);
     currentLevel = 3 ;
 
     for (var i = 0 ; i < 50 ; i++) {
         var xIndex = Math.floor(i % 3 ) + Math.floor(i / 15 ) * 3 ;
         var yIndex = Math.floor(i / 3 % 5 );
         var levelScore = A
         if (i < currentLevel) {
             levelScore = sys.localStorage.getItem(pb_level_score_ + i);
             if (levelScore == null || levelScore == '' ) {
                 levelScore = A;
             }
             if (S == levelScore) {
                 itemName = GameSelectItemScoreS;
             }
             else {
                 itemName = GameSelectItemScore;
             }
         }
         else if (i == currentLevel) {
             itemName = GameSelectItemLevel;
         }
         else {
             itemName = GameSelectItemLocked;
         }
         var itemData = {
             xIndex: xIndex,
             yIndex: yIndex,
             index: i,
             type: itemName,
             onItemAction: function () {
                 cc.log(onItemAction);
                 var action = cc.ScaleBy.create( 0.5 , 0.8 );
                 this .rootNode.runAction(cc.Sequence.create(
                     action, action.reverse(), cc.DelayTime.create( 2 )
                     //  cc.CallFunc.create(this.onItemClicked())
                 ));
             },
             onItemClicked: function () {
                 cc.log(onItemClicked);
                 GAME_LEVEL = ( this .index + 1 );
                 require(MainLayer.js);
                 cc.BuilderReader.runScene(, MainLayer);
             },
             onLockOpened: function () {
             }
         };
         var item = cc.BuilderReader.loadAsNodeFrom(, GameSelectItemSprite, itemData);
         if (item == null ) {
             continue ;
         }
         item.setPosition(cc.p( this .columnSize * xIndex, this .rowSize * ( 4 - yIndex)));
         item.setZOrder( 11 );
         itemData.rootNode = item;
         itemData.controller = this ;
         this .itemDatas.push(itemData);
         itemData.levelNum.setString( + (i + 1 ));
         levelPan.addChild(item);
     }
     levelPan.setContentSize(cc.size( this .columnSize * 12 , this .rowSize * 5 ));
     this .levelScrollPanel.setTouchEnabled( false );
     this .levelScrollPanel.setBounceable( true );
     this .levelScrollPanel.setContainer(levelPan);
     this .levelScrollPanel.setTouchPriority(- 99999 );
 
     this .pageDots( 0 );
}
 
GameSelectLayer.prototype.onUpdate = function () {
 
}
 
GameSelectLayer.prototype.onExit = function () {
 
}
 
GameSelectLayer.prototype.onTouchesBegan = function (touches, event) {
     cc.log(onTouchesBegan);
     this .beganPosition = touches[ 0 ].getLocation();
}
 
GameSelectLayer.prototype.onTouchesMoved = function (touches, event) {
     this .movePosition = touches[ 0 ].getLocation();
}
 
GameSelectLayer.prototype.onTouchesEnded = function (touches, event) {
     cc.log(onTouchesEnded);
     var loc = touches[ 0 ].getLocation();
     var distanceX = this .beganPosition.x - loc.x;
 
     var x = this .levelScrollPanel.getContentOffset().x;
     var y = this .levelScrollPanel.getContentOffset().y;
     this .levelScrollPanel.unscheduleAllCallbacks();
 
     if (distanceX > 50 ) {
         if ( this .pageIndex < 4 ) {
             this .pageIndex += 1 ;
         }
         this .pageDots( this .pageIndex);
     }
     else if (distanceX < - 50 ) {
         if ( this .pageIndex > 0 ) {
             this .pageIndex -= 1 ;
         }
         this .pageDots( this .pageIndex);
     }
     else {
         this .onItemClicked(loc);
     }
     this .levelScrollPanel.setContentOffsetInDuration(cc.p(- this .columnSize * 3 * this .pageIndex, y), 0.5 );
}
 
GameSelectLayer.prototype.onItemClicked = function (location) {
     var x = location.x;
     var y = location.y;
 
     if (!isInScroll(location)) {
         cc.log(out);
         return ;
     }
 
     var scrollPanelRect = this .levelScrollPanel.getBoundingBox();
     var xIndex = Math.floor((x - 110 ) / this .columnSize) + this .pageIndex * 3 ;
     var yIndex = 4 - Math.floor((y - 385 ) / this .rowSize);
     cc.log(scrollX== + scrollPanelRect.x + ,scrollY== + scrollPanelRect.y);
     cc.log(xIndex== + xIndex + ,yIndex== + yIndex);
 
     for (var i = 0 ; i < this .itemDatas.length; i++) {
         if ( this .itemDatas[i].xIndex == xIndex && this .itemDatas[i].yIndex == yIndex) {
             cc.log(click i= + i);
             this .itemDatas[i].onItemClicked();
             break ;
         }
     }
}
 
GameSelectLayer.prototype.pageDots = function (position) {
     this .dotNode.removeAllChildren();
     for (var i = 0 ; i < 4 ; i++) {
         var dots = [s_point.png, s_point_s.png];
         var type = 0 ;
         if (position == i) {
             type = 1 ;
         }
         var dotSprite = cc.Sprite.createWithSpriteFrameName(dots[type]);
         dotSprite.setAnchorPoint(cc.p( 0 , 1 ));
         dotSprite.setPosition(cc.p( 30 * i, 60 ));
         dotSprite.setZOrder( 100 );
         this .dotNode.addChild(dotSprite);
     }
}
 
function isInScroll(location) {
     var x = location.x;
     var y = location.y;
     if (x > 110 && x < ( 110 + 510 ) && y > 385 && y < ( 385 + 500 )) {
         return true ;
     }
     return false
}

同时修改Main.js,加入,require(GameSelectLayer.js);;

 

然后修改StartLayer.js,让点击开始后跳转到GameSelectLayer.js;

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// CleanerScoreScene class
//
var StartLayer = function () {
     cc.log(StartLayer)
};
 
StartLayer.prototype.onDidLoadFromCCB = function () {
//    this.rootNode.onUpdate = function (dt)
//    {
//        this.controller.onUpdate();
//    };
//    this.rootNode.schedule(this.rootNode.onUpdate);
 
     if (sys.platform == 'browser' ) {
         this .onEnter();
     }
     else {
         this .rootNode.onEnter = function () {
             this .controller.onEnter();
         };
     }
 
     this .rootNode.onExit = function () {
         this .controller.onExit();
     };
};
 
StartLayer.prototype.onEnter = function () {
}
 
StartLayer.prototype.onUpdate = function () {
 
}
 
StartLayer.prototype.onPlayClicked = function () {
     cc.BuilderReader.runScene(, GameSelectLayer);
}
 
StartLayer.prototype.onExit = function () {
 
}

完成后运行,ok,效果最上面;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: cocos2d-x js是一个开源的游戏开发引擎,用于开发跨平台的2D游戏,它使用JavaScript作为脚本语言。以下是对cocos2d-x js的回答。 cocos2d-x jscocos2d-x引擎的JavaScript版本,它继承了cocos2d-x引擎强大的功能和性能,同时提供了使用JavaScript编写游戏的便利性。 cocos2d-x js通过使用JavaScript脚本语言进行游戏开发,使得开发者可以更加方便地编写游戏逻辑和动作表现。JavaScript是一种简单易用的脚本语言,对于不熟悉复杂编程语言的开发者来说,使用JavaScript进行游戏开发会更加容易上手。 cocos2d-x js提供了丰富的游戏功能和API,包括精灵动画、物理引擎、碰撞检测、场景管理等,这些功能可以帮助开发者快速构建2D游戏。同时,cocos2d-x js还提供了跨平台的支持,可以将游戏轻松移植到不同的平台上,如iOS、Android、Windows等。 除了基本的功能和API,cocos2d-x js还支持插件和扩展,开发者可以根据自己的需求进行扩展和定制,以满足游戏的特殊需求。 总结来说,cocos2d-x js是一个强大而灵活的游戏开发引擎,它提供了使用JavaScript进行游戏开发的便利性,同时具备跨平台的支持和丰富的功能和API。无论是初学者还是有经验的开发者,都可以通过cocos2d-x js来快速构建出高质量的2D游戏。 ### 回答2: Cocos2d-x是一款强大的跨平台游戏开发引擎,具有许多独特而且强大的功能。cocos2d-x js篇是Cocos2d-x引擎的JavaScript版本,它可以让开发者使用JavaScript编写游戏逻辑和界面。 使用Cocos2d-x js篇,开发者可以轻松地创建2D游戏,并在多个平台上运行,如iOS、Android、Windows和Web。这意味着开发者只需编写一次代码,就可以发布到多个平台,大大提高了开发效率。 cocos2d-x js篇提供了丰富的API和功能,使得开发复杂的游戏变得轻松。它包括图形渲染、动画系统、物理引擎、碰撞检测、音频管理等功能,开发者可以利用这些功能创建出丰富多样的游戏体验。 此外,cocos2d-x js篇还支持现代开发工具和技术,如可视化编辑器Cocos Creator,可实时调试和热更新等。开发者可以使用Cocos Creator创建游戏场景、编辑动画、创建粒子效果等。同时,Cocos2d-x js篇还集成了调试工具,方便开发者进行错误排查和优化。 总之,Cocos2d-x js篇是一个非常强大且灵活的游戏开发引擎。它提供了广泛的功能和工具,使得开发者能够快速创建高质量的2D游戏,并在多个平台上发布。无论是初学者还是有经验的开发者,都可以通过Cocos2d-x js篇轻松实现独特而丰富的游戏体验。 ### 回答3: Cocos2d-x js是一种使用Javascript编写的跨平台游戏开发框架。它是基于Cocos2d-x引擎的扩展,可以在多个平台上运行,包括iOS、Android、Windows和Mac等。 Cocos2d-x js提供了丰富的游戏开发工具和功能,可以帮助开发人员轻松创建高质量的游戏应用。它具有优秀的性能和灵活性,并且能够快速部署到各种平台上。 使用Cocos2d-x js,开发人员可以使用熟悉的Javascript语言进行游戏开发,而无需学习其他编程语言。它提供了一系列API和组件,例如精灵、动作、节点和场景等,以简化游戏的创建和管理过程。 此外,Cocos2d-x js还提供了多种功能,如物理引擎、碰撞检测、音效播放和动画效果等,可以帮助开发人员实现各种游戏特效和交互效果。 Cocos2d-x js还支持一些常用的开发模式和工具,如游戏UI编辑器、游戏调试器和性能分析器等,方便开发人员进行游戏开发和调试。 总之,Cocos2d-x js是一款功能强大、易于使用并且跨平台的游戏开发框架。无论是新手还是经验丰富的开发人员,都可以借助它来创建出令人印象深刻的游戏应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值