complete_project source code analysis knowledge summary

 

Project link: https://github.com/cocos-creator/tutorial-first-game/tree/master/complete_project

 

 

1.this use global variables and let local variables

The revised Code

1  // Use let local variable 
2 let jumpAction = the this .setJumpAction ();
 . 3  the this .node.runAction (jumpAction);

 

 

2. The script file attributes and methods should be strictly classified as good

If Game.js inside getNewStarPosition () method writes Star.js them, will be better understood and easy to maintain

 

In Star.js which, according to common sense, is the protagonist pick up the stars, the stars rather than waiting for the main character to come, so I think pickRadius property and getPlayerDistance and onPicked methods can be written in Player.js

 

 

3. If it is not frequently modified property, try not explicitly exposed

 

Usually numerical system planning Well, do not arbitrarily change the value, the script property is exposed, it is easy to accidentally free to modify the value, but wrote code data is not easy to be free to modify

 

 

4. prefabricated resource usage occasions

What will be used when pre resources? Often used when required to be generated by a program node resources dynamically

 

 

5. Scratch Game objects on the star component references

1 newStar.getComponent('Star').game = this;

通过Node对象调用getComponent()方法,返回的是一个Component类型的对象,组件对象也称脚本对象。巧妙之处,通过脚本对象调用game属性,从而为Star脚本文件添加了一个全局变量game

 

6.停止并且移除所有正在运行的动作列表

1 //停止 player 节点的跳跃动作
2 this.player.stopAllActions();

 

 

7.通过回调函数来执行音频播放

1 // 添加一个回调函数,用于在动作结束时调用我们定义的其他方法
2 var callback = cc.callFunc(this.playJumpSound, this);
3 
4 // 不断重复,而且每次完成落地动作后调用回调来播放声音
5 cc.repeatForever(cc.sequence(jumpUp, jumpDown, callback));

 

 

 

8.不同脚本组件调用onload方法时互不影响

Game.js执行生成星星操作与Player.js执行主角跳跃动作互补干扰

 

 

9.初始化键盘输入监听

 1 // 初始化键盘输入监听
 2 cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
 3 cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);  
 4 
 5 onDestroy () {
 6 // 结束游戏之后,取消键盘输入监听
 7 cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
 8 cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
 9 },  

cc.systemEvent.on();注册事件目标的特定事件类型回调
cc.systemEvent.off();删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器

 

 

10.event参数传递

通过event参数传递,可以获得有关键盘相关按键的信息

 

 

11.通过数学运算来判断正负值

巧妙之处this.xSpeed / Math.abs(this.xSpeed),通过数学运算方法来判断速度为正还是为负,就不需要通过this.accLeft和this.accRight属性来判断速度的正负值,节省代码

 

 

12.return的作用

巧妙使用return语句,当if条件成立时,执行return语句,结束当前update操作。return与break的不同之处,break只能结束当前循环,而return结束整个函数,return作用比break更强大些

 

 

13.计算两点之间位置的距离

1 // 根据 player 节点位置判断距离
2 var playerPos = this.game.player.getPosition();
3 // 根据两点位置计算两点之间距离
4 var dist = this.node.position.sub(playerPos).mag();

vec2.sub();向量减法
vec2.mag();返回该向量的长度
通过sub向量减法来计算两点之间位置的距离,比物理碰撞算法还要简单方便

 

 

14.cc.repeatForever()

cc.repeatForever();永远地重复一个动作,有限次数内重复一个动作请使用 repeat 动作,由于这个动作不会停止,所以不能被添加到 cc.sequence 或 cc.spawn 中

 

 

15.cc.macro.KEY

cc.macro.KEY;键盘事件的按键值

 

 

16.播放声音

1 // 调用声音引擎播放声音
2 cc.audioEngine.playEffect(this.jumpAudio, false);

 

 

17.runAction

1 // 初始化跳跃动作
2 let jumpAction = this.setJumpAction();
3 this.node.runAction(jumpAction);

node.runAction();执行并返回该执行的动作。该节点将会变成动作的目标。调用 runAction 时,节点自身处于不激活状态将不会有任何效果。
注意:你不应该修改 runAction 后的动作,将无法发挥作用,如果想进行修改,请在定义 action 时加入

 

 

18.moveBy和moveTo的区别

你能注意到,每一个动作都会有两个方法 By 和 To。两种方法方便你在不同的情况使用,By 算的是相对于节点对象的当前位置,To 算的是绝对位置,不考虑当前节点对象在哪。如果你想动作的表现是相对于 Node 当前位置的,就用 By,相对的想让动作的表现是按照坐标的绝对位置就用 To

 

Guess you like

Origin www.cnblogs.com/LayaBox/p/11409744.html