[超级英雄跳]
http://bbs.9ria.com/thread-176968-1-1.html
游戏的重点:操作的手感,跳跃的速度感
页面一
INCLUDEPICTURE "http://bbs.9ria.com/forum.php?mod=attachment&aid=OTg5MTV8ODdlYWY4Yzd8MTM3MjEyNzA3MHwwfDE3Njk2OA%3D%3D&noupdate=yes" \* MERGEFORMATINET
页面二
2013-3-11 22:29:39 上传
下载附件 (72.07 KB)
游戏页面二
页面三
简单的说下游戏规则:
空格键:跳跃,(一直按住,可不停的跳)
左右键:左右移动,(左右键是小键盘上的上下左右键中的左右键,左右移动是有加速度的)
左右移动的速度,决定跳起的高度
左右移动的越快,跳跃的高度就越高,碰到距离比较大台阶时,就要先左右移动,达到一定速度后,再起跳!
台阶功能的实现,先看效果:
这节课先讲下台阶功能的实现:
在flash cs5.5中,台阶资源是下图的样子,台阶有5帧,台阶宽度由短到长,分五种
注意两点:1,台阶的注册点
2,导出的连接名
台阶类
设计
领导形象设计圆作业设计ao工艺污水处理厂设计附属工程施工组织设计清扫机器人结构设计
Main.as:用于创建测试台阶
StepsControl.as:台阶的管理类
Step.as:单个台阶
简单的
分析
定性数据统计分析pdf销售业绩分析模板建筑结构震害分析销售进度分析表京东商城竞争战略分析
下类的作品和功能吧,为什么要这样子设计,是根据功能而定的:
StepsControl.as,现在功能只是创建7个台阶,没有其它操作,其它操作我们可以想到的有:暂停所有台阶的移动,加速下移等
1. package com.steps
2. {
3. import flash.display.Sprite;
4.
5. /**
6. * ...
7. * @author Ragged
8. */
9. public class StepsControl extends Sprite
10. {
11. /**
12. * 台阶数组,用于保存每一个台阶
13. */
14. private var _stepsArr:Array = [];
15.
16.
17.
18. public function StepsControl()
19. {
20. this.createSteps();
21. }
22. /**
23. * 创建7个小台阶
24. */
25. private function createSteps():void
26. {
27. var i:uint;
28. var end:uint = 7;
29.
30. for (i = 0; i < end;i++ )
31. {
32. this._stepsArr[i] = new Step(i);
33. this.addChild(this._stepsArr[i]);
34. }
35. }
36. }
37. }
复制代码
台阶类:
_id用于记录当前台阶是多少层,比如到100层了,_type表示台阶的宽度,_sp表示速度
还有用Event做发动机,发动机的开,关方法
有一个要点就是,设计台阶的个数,每个台阶的距离,这里我舞台高是560,然后7个台阶,循环就可以实现效果
其实功能就这些
1. package com.steps
2. {
3. import flash.display.MovieClip;
4. import flash.display.Sprite;
5. import flash.events.Event;
6.
7.
8.
9. /**
10. * ...
11. * @author Ragged
12. */
13. public class Step extends Sprite
14. {
15. /**
16. * 台阶id,记录当前台阶级数
17. */
18. private var _id:uint;
19. /**
20. * 速度
21. */
22. private var _sp:uint = 5;
23. /**
24. * 台阶的种类:台阶按长度分,有5种
25. */
26. private var _type:uint;
27. /**
28. * 台阶显示对象
29. */
30. private var _step:MovieClip;
31.
32.
33.
34.
35.
36. public function Step($id:uint)
37. {
38. this._id = $id;
39.
40. this.y = 300 - this._id * 100;
41.
42. this.createStep();
43.
44. this.startEvent();
45. }
46. /**
47. * 开
48. */
49. public function startEvent():void
50. {
51. this.addEventListener(Event.ENTER_FRAME, enterframe);
52. }
53. /**
54. * 关
55. */
56. public function stopEvent():void
57. {
58. this.removeEventListener(Event.ENTER_FRAME, enterframe);
59. }
60. /**
61. * 发动机
62. * @param $evt
63. */
64. private function enterframe($evt:Event):void
65. {
66. this.y += this._sp;
67.
68. if (this.y >= 600)
69. {
70. this.y -= 700;
71. }
72. }
73.
74. /**
75. * 创建一种随机台阶
76. */
77. private function createStep():void
78. {
79. this._step = new StepResources();
80. //随机一种长度
81. this._type = uint(Math.random() * 5 + 1)
82. this._step.gotoAndStop(this._type);
83.
84. this.addChild(this._step);
85. }
86. }
87. }
复制代码
在上一节的基础上,增加些功能,完善了台阶的基本功能:
1. /**
2. * 重新随机一次
3. *
4. * 注意两句话:
5. * this._step.gotoAndStop(this._type);
6. * this._wood = this._step.woodMC;
7. *
8. * player 9,这样子是会出错,
9. * 原因是:使用跳帧语句gotoAndStop,跳到某一帧时,woodMC没有生成出来
10. * 如果下一句马上访问woodMC,就会报空对象
11. * (要解决这个问题,还有其它办法的,这里先放着)
12. * player 10以后的播放器就不会出错了,adobe改进了这句bug
13. */
14. private function resetRandom():void
15. {
16. //随机一种长度
17. this._type = uint(Math.random() * 5 + 1);
18. //跳到相应帧
19. this._step.gotoAndStop(this._type);
20. //更新woodMC,woodMC是台阶上用于站立元件
21. this._wood = this._step.woodMC;
22. //牌子上显示台阶级数
23. this._floorText.text = String(this._id);
24. if (this._id%10 == 0)
25. {
26. this._signMC.visible = true;
27. }
28. else
29. {
30. this._signMC.visible = false;
31. }
32. //随机横坐标
33. this.x = Math.random() * 370 + 80;
34. //判断是超过最右边
35. if (this.x + this._step.width > 550)
36. {
37. this.x = 550 - this._step.width;
38. }
39. }
复制代码
参考图:
这是效果演示:
今天的内容是:控制钢铁侠跳跃和左右移动。
有两个要点:
1,按键监听判断;
按键是用stage来监听的,所以我创建了“KeyControl.as”这个类,而且是继承sprite的显示对象;
当KeyControl被加入显示列表时,stage就可以用了,写法请下载源文件参考。
重点:
左右按键判断,有个小技巧,把左右按键压入数组,一直按着时判断数组中是否已经有相同键按下,
如果有不压入数组,放开时从数组中删除。
数组最长为2,取数组最后面那个值,就是当前要移动的方向。
(类似这种方法,当要上下左右4个按键判断是更好用)
2,钢铁侠跳跃和移动的逻辑
控制钢铁侠的状态逻辑相对复杂些,因为他有很多种状态和条件判断;
左右移动用到了“加速度”“磨擦力”其实它们就是一个变量值,按现实中的理解写到代码中去就可以;
比如:
加速度 = 2;磨擦力 = 1;
玩家向左移动时要减去“加速度”,加上“磨擦力”
玩家向右移动时要加上“加速度”,减去“磨擦力”
跳跃的时候,有个“引力”,向上跳的速度,要减去这个“引力”值;
我觉得,看我代码注释,比看容易理解:
1. package com.key
2. {
3. import flash.display.Sprite;
4. import flash.events.Event;
5. import flash.display.Stage;
6. import flash.events.KeyboardEvent;
7. /**
8. * 按键监听
9. * @author Ragged
10. */
11. public class KeyControl extends Sprite
12. {
13. /**
14. * 保存左右按键
15. */
16. private var _leftRightArr:Array = [];
17. /**
18. * 获取左右移动按键值,如果没有按下,返回0
19. */
20. public function get leftRight():uint
21. {
22. if (this._leftRightArr.length == 0)
23. {
24. return 0;
25. }
26. return this._leftRightArr[this._leftRightArr.length - 1];
27. }
28. /**
29. * 空格键是否被按下
30. */
31. private var _space:Boolean = false;
32. /**
33. * 取空格键值
34. */
35. public function get space():Boolean
36. {
37. return this._space;
38. }
39.
40.
41. public function KeyControl()
42. {
43. this.addEventListener(Event.ADDED_TO_STAGE, addStage);
44. }
45. /**
46. * 加入显示列表后,才可以监听到stage
47. * @param $evt
48. */
49. private function addStage($evt:Event):void
50. {
51. stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown);
52. stage.addEventListener(KeyboardEvent.KEY_UP, keyup);
53. }
54. /**
55. * 把左右键值保存在一个数组中
56. * 重复按下的键数组中只保存一个值
57. *
58. * 为什么要这么做,是为了保证左右按键同时按下时
59. * 可以得到你要的按键值,特别是上下左右,4个键都用到的时候
60. * 这种方法特别的有用
61. * @param $evt
62. */
63. private function keydown($evt:KeyboardEvent):void
64. {
65. if ($evt.keyCode == 37 || $evt.keyCode == 39)
66. {
67. if (this._leftRightArr.length == 0)
68. {
69. this._leftRightArr.push($evt.keyCode);
70. }
71. else if (this._leftRightArr[this._leftRightArr.length - 1] != $evt.keyCode)
72. {
73. this._leftRightArr.push($evt.keyCode);
74. }
75. }
76. else if ($evt.keyCode == 32)
77. {
78. this._space = true;
79. }
80. }
81. /**
82. * 当按键放开时,将会从数组中查找,找到后,删除数组中的按键值
83. * @param $evt
84. */
85. private function keyup($evt:KeyboardEvent):void
86. {
87. if ($evt.keyCode == 32)
88. {
89. this._space = false;
90. return;
91. }
92. //
93. var i:uint;
94. var end:uint = this._leftRightArr.length;
95. for (i = 0; i < end;i++ )
96. {
97. if (this._leftRightArr[i] == $evt.keyCode)
98. {
99. this._leftRightArr.splice(i, 1);
100. }
101. }
102. }
103.
104. }
105. }
复制代码
因为钢铁侠类代码比较多,所以我用了继承的方法:
1. package com.player
2. {
3. import com.key.KeyControl;
4. import flash.display.Sprite;
5. import flash.display.MovieClip;
6. import flash.events.Event;
7.
8. /**
9. * Player的基类,主要是分担代码数量
10. * @author Ragged
11. */
12. public class PlayerClass extends Sprite
13. {
14. /**
15. * 钢铁侠
16. */
17. protected var _player:MovieClip;
18. /**
19. * 按键监听
20. */
21. protected var _keyControl:KeyControl;
22. /**
23. * 左右移动的速度
24. */
25. protected var _spX:Number = 0;
26. /**
27. * 左右移动加速度
28. */
29. protected var _spXAdd:Number = 1.5;
30. /**
31. * 磨擦力
32. */
33. protected var _friction:Number = 0.5;
34. /**
35. * 左右移动的最大值
36. */
37. protected var _spXMax:uint = 10;
38. /**
39. * 上升和下落的速度
40. */
41. protected var _spY:int;
42. /**
43. * 引力
44. */
45. protected var _spYAdd:int = 3;
46. /**
47. * 跳跃和下落的最大值
48. */
49. protected var _spYMax:uint = 30;
50. /**
51. * 起跳的力度
52. */
53. protected var _power:int = 30;
54. /**
55. * 跳起的状态:
56. * true表示跳跃中(向上升,或者下落),false表示停住了
57. */
58. protected var _jump:Boolean = false;
59.
60.
61.
62.
63.
64.
65.
66. public function PlayerClass()
67. {
68.
69. }
70. /**
71. * 开
72. */
73. public function startEvent():void
74. {
75. this.addEventListener(Event.ENTER_FRAME, enterframe);
76. }
77. /**
78. * 关
79. */
80. public function stopEvent():void
81. {
82. this.removeEventListener(Event.ENTER_FRAME, enterframe);
83. }
84. /**
85. * 发动机=+=
86. * @param $evt
87. */
88. protected function enterframe($evt:Event):void
89. {
90. //由子类实现
91. }
92.
93.
94.
95. /**
96. * 创建钢铁侠显示对象
97. */
98. protected function createPlayer():void
99. {
100. this._player = new PlayerResources();
101. this._player.x = 300;
102. this._player.y = 520;
103. //显示比例是原始大小的70%
104. this._player.scaleX = this._player.scaleY = 0.7;
105. this._player.stop();
106. this.addChild(this._player);
107. }
108. /**
109. * 按键监听,要先创建它,然后加入显示列表
110. * 也可以用其它方法,不用加入显示列表
111. * 但是我一直这样了用,不知道是否科学=+=
112. */
113. protected function createKeyControl():void
114. {
115. this._keyControl = new KeyControl();
116. this.addChild(this._keyControl);
117. }
118. }
119. }
复制代码
1. package com.player
2. {
3. import flash.display.Sprite;
4. import flash.events.Event;
5. /**
6. * ...
7. * @author Ragged
8. */
9. public class Player extends PlayerClass
10. {
11.
12. public function Player()
13. {
14. this.createPlayer();
15. //
16. this.createKeyControl();
17. //
18. this.startEvent();
19. }
20. /**
21. * 实现基类的发动机
22. */
23. protected override function enterframe($evt:Event):void
24. {
25. if (this._jump)
26. {
27. //处于跳跃状态
28. this._player.y += this._spY;
29. this._spY += this._spYAdd;
30. //限制下落的最大速度
31. if (this._spY > this._spYMax)
32. {
33. this._spY = this._spYMax;
34. }
35. //如果大于550高度,就停在550的位置
36. if (this._player.y > 520)
37. {
38. this._player.y = 520;
39. this._jump = false;
40. }
41.
42. //开始下落了,要跳到下落的帧状态
43. if (this._spY > 0)
44. {
45. //速度大于零时,为下落状态
46. if (this._player.currentLabel != "down")
47. {
48. this._player.gotoAndStop("down");
49. }
50. }
51. }
52. else if (this._jump == false)
53. {
54. //非跳跃状态,要判断是否按下起跳键
55. if (this._keyControl.space == true)
56. {
57. //算出左右移动速度加成
58. var tY:uint = Math.abs(this._spX);
59. //起跳
60. this._spY = -(this._power + tY);
61. //设置为跳跃状态
62. this._jump = true;
63. //跳帧
64. if (this._spY <= -45)
65. {
66. if (this._player.currentLabel != "jump2")
67. {
68. this._player.gotoAndStop("jump2");//大跳
69. }
70. }
71. else if (this._spY < -30)
72. {
73. if (this._player.currentLabel != "jump1")
74. {
75. this._player.gotoAndStop("jump1");//中跳
76. }
77. }
78. else
79. {
80. if (this._player.currentLabel != "jump0")
81. {
82. this._player.gotoAndStop("jump0");//小跳
83. }
84. }
85. }
86. }
87. ///////////////////////////