Урок 4 Руководство по созданию AS3 Flash игры – часть 2: монеты
Страница 1 из 1
Урок 4 Руководство по созданию AS3 Flash игры – часть 2: монеты
Ранее я показал, как создать простейшую игру "move-the-ball", используя классы AS3. Теперь самое время рассказать еще о нескольких элементах…монетах, которые нужно собирать. Очевидно, что у монет должен быть свой собственный класс, называемый coin.as, сохраненный в той же ветви, где и остальные классы. Вот полностью расписанный coin.as файл
ACTIONSCRIPT:
1.package {
2. import flash.display.Sprite;
3. import flash.events.Event;
4. public class coin extends Sprite {
5. // variables used in this class
6. private var dist_x:int;
7. private var dist_y:int;
8. private var distance:int;
9. // main function
10. public function coin() {
11. // calling place_coin function.
12. // this function randomly places the coin in the field
13. place_coin();
14. // checking for collisions at every frame
15. addEventListener(Event.ENTER_FRAME, check_collisions);
16. }
17. private function check_collisions(e:Event) {
18. // determining the distance between the hero and the coin
19. // notice how do I refer the hero
20. dist_x = x - as3circle(root).circle_hero.x;
21. dist_y = y - as3circle(root).circle_hero.y;
22. distance = dist_x*dist_x+dist_y*dist_y;
23. // 1809 = (hero radius + coin radius)^2
24. // this way I don't have to perform a square root to distance
25. if (distance <1089) {
26. // if the hero picks up a coin, then move it elsewhere
27. place_coin();
28. }
29. }
30. private function place_coin() {
31. x = Math.floor(Math.random()*400)+50;
32. y = Math.floor(Math.random()*300)+50;
33. }
34. }
35.}
Также в главном файле as3circle.as отметьте, как создаются монеты, в строках 9-12.
ACTIONSCRIPT:
1.package {
2. import flash.display.Sprite;
3. import flash.events.Event;
4. public class coin extends Sprite {
5. // variables used in this class
6. private var dist_x:int;
7. private var dist_y:int;
8. private var distance:int;
9. // main function
10. public function coin() {
11. // calling place_coin function.
12. // this function randomly places the coin in the field
13. place_coin();
14. // checking for collisions at every frame
15. addEventListener(Event.ENTER_FRAME, check_collisions);
16. }
17. private function check_collisions(e:Event) {
18. // determining the distance between the hero and the coin
19. // notice how do I refer the hero
20. dist_x = x - as3circle(root).circle_hero.x;
21. dist_y = y - as3circle(root).circle_hero.y;
22. distance = dist_x*dist_x+dist_y*dist_y;
23. // 1809 = (hero radius + coin radius)^2
24. // this way I don't have to perform a square root to distance
25. if (distance <1089) {
26. // if the hero picks up a coin, then move it elsewhere
27. place_coin();
28. }
29. }
30. private function place_coin() {
31. x = Math.floor(Math.random()*400)+50;
32. y = Math.floor(Math.random()*300)+50;
33. }
34. }
35.}
Также в главном файле as3circle.as отметьте, как создаются монеты, в строках 9-12.
Оставшиеся классификационные файлы circle.as и keys.as не требуют изменений.
ACTIONSCRIPT:
1.package {
2. import flash.display.Sprite;
3. import flash.events.Event;
4. public class coin extends Sprite {
5. // variables used in this class
6. private var dist_x:int;
7. private var dist_y:int;
8. private var distance:int;
9. // main function
10. public function coin() {
11. // calling place_coin function.
12. // this function randomly places the coin in the field
13. place_coin();
14. // checking for collisions at every frame
15. addEventListener(Event.ENTER_FRAME, check_collisions);
16. }
17. private function check_collisions(e:Event) {
18. // determining the distance between the hero and the coin
19. // notice how do I refer the hero
20. dist_x = x - as3circle(root).circle_hero.x;
21. dist_y = y - as3circle(root).circle_hero.y;
22. distance = dist_x*dist_x+dist_y*dist_y;
23. // 1809 = (hero radius + coin radius)^2
24. // this way I don't have to perform a square root to distance
25. if (distance <1089) {
26. // if the hero picks up a coin, then move it elsewhere
27. place_coin();
28. }
29. }
30. private function place_coin() {
31. x = Math.floor(Math.random()*400)+50;
32. y = Math.floor(Math.random()*300)+50;
33. }
34. }
35.}
Также в главном файле as3circle.as отметьте, как создаются монеты, в строках 9-12.
ACTIONSCRIPT:
1.package {
2. import flash.display.Sprite;
3. import flash.events.Event;
4. public class coin extends Sprite {
5. // variables used in this class
6. private var dist_x:int;
7. private var dist_y:int;
8. private var distance:int;
9. // main function
10. public function coin() {
11. // calling place_coin function.
12. // this function randomly places the coin in the field
13. place_coin();
14. // checking for collisions at every frame
15. addEventListener(Event.ENTER_FRAME, check_collisions);
16. }
17. private function check_collisions(e:Event) {
18. // determining the distance between the hero and the coin
19. // notice how do I refer the hero
20. dist_x = x - as3circle(root).circle_hero.x;
21. dist_y = y - as3circle(root).circle_hero.y;
22. distance = dist_x*dist_x+dist_y*dist_y;
23. // 1809 = (hero radius + coin radius)^2
24. // this way I don't have to perform a square root to distance
25. if (distance <1089) {
26. // if the hero picks up a coin, then move it elsewhere
27. place_coin();
28. }
29. }
30. private function place_coin() {
31. x = Math.floor(Math.random()*400)+50;
32. y = Math.floor(Math.random()*300)+50;
33. }
34. }
35.}
Также в главном файле as3circle.as отметьте, как создаются монеты, в строках 9-12.
Оставшиеся классификационные файлы circle.as и keys.as не требуют изменений.
Похожие темы
» Урок 6 Руководство по созданию AS3 Flash игры – часть 3: стены
» Урок 7 Руководство по созданию AS3 Flash игры – часть 4: очки
» Урок 2 руководство-часть 1
» Урок 5 Новая тайловая основа движка игры – часть 2
» Урок 8 Новая тайловая основа движка игры – часть 3
» Урок 7 Руководство по созданию AS3 Flash игры – часть 4: очки
» Урок 2 руководство-часть 1
» Урок 5 Новая тайловая основа движка игры – часть 2
» Урок 8 Новая тайловая основа движка игры – часть 3
Страница 1 из 1
Права доступа к этому форуму:
Вы не можете отвечать на сообщения
|
|