Total game is divided into four functional modules:
- to start the game ( MenuLayer )
- Level Select ( levelLayer )
- Game ( gameLayer )
- Game settlement ( gameOverLayer )
Creator inner assembly results are as follows:
Games start the default display menuLayer, game, by controlling the display and hide at all levels to achieve different switching modules. For example, to start the game, click Start later, triggering a callback function, to switch to the game level selection interface, binding relationship in the following figure:
Codes are as follows:
Other functional modules to achieve similar. The following points will be about four modules are implemented each module.
1. menuLayer start the game
to start the game module, the default display after the start of the game, other modules hidden, function relatively simple, after the interface layout is complete, add a button to start the game to respond to events, implementation code as above, in this interface to add a little animation so start the game constantly zoom button, the code is as follows:
Achieve effect after:
2. Select the level levelLayer
level selection in two steps: first, the interface displays, through the configuration file, load prefabricated files, display all levels; the second step, depending on the game situation, updated every level information.
2.1 The first step level display
all of the game levels ScrollView control placed on every level, using a pre-file (levelItem), by reading level configuration file, load all levels, load recalculated ScrollView content after the completion of height, loading level code is as follows:
FIG i.e. all lower levels prefabricated parent node:
Pre-scripted hang on to pre-:
2.2 第二步更新关卡
每一个levelItem预制上挂一个levelItem脚本组件,levelItem脚本组件负责更新信息,主要控制是否可点击、通关星数、关卡等级、点击进入,levelItem脚本组件更新UI代码如下:
玩家的通过的信息,通过配置存储文件,保存玩家通关信息,分为已通关、刚开启和未开启三种状态,具体实现如下:
最终的显示效果如下图:
3. 游戏 gameLayer
游戏也分为两步:第一步,显示界面;第二步,游戏操作判断
3.1 显示界面
游戏内使用levelConfig.json配置每一关卡信息,每个关卡游戏部分由多行多列的方格组成,每一个关卡信息包含content、allRow、allCol、heroRow、heroCol、allBox属性,allRow和allCol记录总共行数和列数,heroRow、heroCol记录英雄所在位置,allBox记录箱子的总数,content是核心,记录每个方格的属性,根据不同的属性显示不同的物体,如墙面、地面、物体、箱子,可以通过修改配置,增加任意关卡。
读取关卡所有数据,并根据每一个位置的属性,显示不同的实物。
根据配置创建关卡信息
根据类型创建元素:
游戏的所有元素,放置在下图中gameControlLayer的上:
游戏开始后,显示的效果如下(第一关,其他关类似)
3.2 游戏操作判断
路线计算好后,玩家移动,若玩家点击的是箱子区域,先检测箱子前方是否有障碍物,若没有则推动箱子,通过切换地图的图片和修改位置类型达到推动箱子的效果。
点击地图位置,获取最优路径,人物跑到指定点,实现如下:
获取最优路径算法:
4. settlement gameOverLayer game
after the end of the game, based on the number of boxes successfully pushed to determine whether a game is successful, the future success of the game, you can update the checkpoint information.
Analyzing logic is as follows:
Creator component layout is as follows:
This game provides the free game source code, please pay attention to the public need the source code numbers "a coolie" get