1 / 41

Intro to Action Script 11

"The games of a people reveal a great deal about them.“ Marshall McLuhan. Intro to Action Script 11. The player controls an object or a character that moves left-right Other object fall down The player must either catch or avoid falling objects. Catch games applecatch.fla.

dgatlin
Download Presentation

Intro to Action Script 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. "The games of a people reveal a great deal about them.“ Marshall McLuhan Intro to Action Script 11

  2. The player controls an object or a character that moves left-right Other object fall down The player must either catch or avoid falling objects Catch games applecatch.fla

  3. Catch games applecatch.fla

  4. The fox moves along the bottom of the stage left –right controlled by the user through left and right arrow keys on the keyboard Fox moves as long as the arrow key is pressed Fox stops automatically before going beyond the left and right sides of the screen Apples fall down from the tree at random places and random times But not too quickly The apples start falling slowly and then fall faster and faster as the game progresses The fox has to catch apples Each successfully catch apple increments the player score shown in the score text field Catch games applecatch.fla

  5. Open applecatch.fla flash file. Pay attention to the 3 frames. 1 frame – “start”, 2 frame – play, 3 frame – “game over” Catch games applecatch.fla

  6. 1 frame – “start” Catch games applecatch.fla

  7. 2 frame – play Catch games applecatch.fla

  8. 3 frame – “game over” Catch games applecatch.fla

  9. “running fox” movie clip in the library which contains animation 1 frame “stand” 2-6 frames “run” Catch games applecatch.fla

  10. “running fox” movie clip exported for Action Script as “running fox” Catch games applecatch.fla

  11. “apple movie clip exported for Action Script as “apple” Catch games applecatch.fla

  12. attachMovie is used to make apples timeSinceLastApple counter starts with 0 for each new apple incremented once per frame then counter becomes 20, another apple is created this assures the time between the apples (20 frames) appleSpeed variable to control falling speed of apples after each new apple it is increased the apples fall faster and faster Each apple movie clip is removed then fox catches it or it hits the bottom of the stage After certain number of dropped apples the game ends and the score is displayed Catch games applecatch.fla

  13. “Actions” movie clip contains the following script that calls Function initGame() once the actions movie clip is loaded Calls functions moveFox(), dropNewApple(), moveApples() every frame. onClipEvent (load) { _root.initGame(); } onClipEvent (enterFrame) { _root.moveFox(); _root.dropNewApple(); _root.moveApples(); } Catch games applecatch.fla

  14. Second frame (play frame) script. The functions are declared in the main Timeline stop(); function initGame() { // sets the variables to start the game and creates the fox movie clip // the range of falling apple clips firstApple = 1; lastApple = 0; // init the score score = 0; Catch games applecatch.fla

  15. // set the number of apples to fall totalApples = 20; // init the speed and time delay timeSinceLastApple = 0; appleSpeed = 5; // create the fox so that it is on top of the apples attachMovie( "running fox", "fox", 999999 ); fox._x = 275; fox._y = 300; } Because the fox movie clip is created through script as the movie is built it appears on top of the apples the assigned level to fox movie clip is 999 999. All apples start at level 1 so they are behind the fox. Catch games applecatch.fla

  16. Key.isDown(Key.RIGHT))checks if the right arrow key is down Key.isDown(Key.LEFT))checks if the left arrow key is down dx variable set to 10 / -10 to indicate how much and in what direction fox should move _xscalenegative or positive value flip fox movie clips horizontally so that fox is facing the direction of the movement Math.abs (x) computes and returns an absolute value for the number specified by the parameter x . Catch games applecatch.fla

  17. function moveFox() { // check for arrow keys if (Key.isDown(Key.RIGHT)) { dx = 10; // fox faces right fox._xscale = -Math.abs(fox._xscale); } else if (Key.isDown(Key.LEFT)) { dx = -10; // fox faces left fox._xscale = Math.abs(fox._xscale); } else { // no movement dx = 0; } Catch games applecatch.fla

  18. // move the fox for a value of dx //and limit that movement fox._x += dx; if (fox._x < 30) fox._x = 30; if (fox._x > 520) fox._x = 520; // make the fox run or stand still if ((dx != 0) and (fox._currentFrame == 1)) { fox.gotoAndPlay("run"); } else if ((dx == 0) and (fox._currentFrame != 1)) { fox.gotoAndPlay("stand"); } } If there is a movement going on and fox is standing still on the first frame then clip sent to 2 frame “run”. Catch games applecatch.fla

  19. function dropNewApple() { // drop only if it has been long enough if (timeSinceLastApple > 20) { // drop only if there are more apples if (lastApple < totalApples) { // drop only 10% of the time //prevents apples to be dropped with at exact time intervals if (Math.random() < .1) { Catch games applecatch.fla

  20. // create next apple and set its locations lastApple++; attachMovie( "apple", "apple"+lastApple, lastApple ); //horizontal location of each new apple between 30 and 520 _root["apple"+lastApple]._x = Math.random()*490+30; _root["apple"+lastApple]._y = 0; // reset time delay for next apple timeSinceLastApple = 0; // increase apple speed if (appleSpeed < 10) appleSpeed += .5; } } } Catch games applecatch.fla

  21. // even if no apple dropped, get closer to next drop timeSinceLastApple++; } Catch games applecatch.fla

  22. function moveApples() { // loop through all existing apple clips for (i=firstApple;i<=lastApple;i++) { // get apple location x = _root["apple"+i]._x; y = _root["apple"+i]._y + appleSpeed; // see whether apple reached ground if (y > 400) { removeApple(i); Catch games applecatch.fla

  23. // see whether apple hit basket } else if ((Math.abs(y-fox._y) < 10) and (Math.abs(x-fox._x) < 25)) { removeApple(i); score += 1; // continue to move apple } else { _root["apple"+i]._y = y; } } } Math.abs used to determine collision between basket and apples When an apple within 10 pixels of vertical and 25 pixels of horizontal proximity, the collision is detected Catch games applecatch.fla

  24. function removeApple(n) { // take away apple movie clip (delete) _root["apple"+n].removeMovieClip(); // reset range of apples to move firstApple = n+1; // see whether this was the last apple if (n == totalApples) { fox.removeMovieClip(); gotoAndPlay("game over"); } } Catch games applecatch.fla

  25. Catch game with selection add/remove power add/remove score add/remove objects in the game scene etc. Modified appleCatch game Each apple has a letter inside. The user has to catch vowels and avoid consonants The right apple with vowel increments user’s score The wrong apple with a consonant decrements the score the score should not go below 0 Avoid games applelettercatch.fla

  26. Catch game with selection Avoid games applelettercatch.fla

  27. bad apple - consonants Avoid games applelettercatch.fla

  28. good apple - vowels Avoid games applelettercatch.fla

  29. Two apple movie clips- good (vowels) and bad (consonants) “good apple” 5 frames each showing a vowel “bad apple” 20 frames each showing a consonant When it is time to drop there is a chance it is good apple or bad apple The script displays a random frame from the appropriate movie clip When the player catches the apple the script determines if its good or bad one and adjusts the score Avoid games applelettercatch.fla

  30. The “actions” movie clip contains the same script as in the applecatch game: onClipEvent (load) { _root.initGame(); } onClipEvent (enterFrame) { _root.moveFox(); _root.dropNewApple(); _root.moveApples(); } Avoid games applelettercatch.fla

  31. initGame() function is the same as in the applecatch game: stop(); function initGame() { // the range of falling apple clips firstApple = 1; lastApple = 0; // init the score score = 0; // set the number of apples to fall now 50 totalApples = 50; Avoid games applelettercatch.fla

  32. // init the speed and time delay timeSinceLastApple = 0; appleSpeed = 5; // create the fox so that it is on top of the apples attachMovie( "running fox", "fox", 999999 ); fox._x = 275; fox._y = 300; } Avoid games applelettercatch.fla

  33. moveFox() function is the same as in the applecatch game: function moveFox() { // check for arrow keys if (Key.isDown(Key.RIGHT)) { dx = 10; // fox faces right fox._xscale = -Math.abs(fox._xscale); } else if (Key.isDown(Key.LEFT)) { dx = -10; // fox faces left fox._xscale = Math.abs(fox._xscale); } else { // no movement dx = 0; } Avoid games applelettercatch.fla

  34. // move the fox and limit that movement fox._x += dx; if (fox._x < 30) fox._x = 30; if (fox._x > 520) fox._x = 520; // make the fox run or stand still if ((dx != 0) and (fox._currentFrame == 1)) { fox.gotoAndPlay("run"); } else if ((dx == 0) and (fox._currentFrame != 1)) { fox.gotoAndPlay("stand"); } } Avoid games applelettercatch.fla

  35. dropNewApple() function decides which type of apple to drop, And then pick a random frame in the corresponding movie clip to display. function dropNewApple() { // only drop if it has been long enough if (timeSinceLastApple > 20) { // only drop if there are more apples if (lastApple < totalApples) { // only drop 10% of the time if (Math.random() < .1) { // create next apple and set itslocations lastApple++; if (Math.random() < .5) { Avoid games applelettercatch.fla

  36. // 50% chance of a bad apple attachMovie( "bad apple", "apple"+lastApple, lastApple ); _root["apple"+lastApple].type = "bad"; } else { // 50% chance of a good apple attachMovie( "good apple", "apple"+lastApple, lastApple ); _root["apple"+lastApple].type = "good"; } f = int(Math.Random()*_root["apple"+lastApple]._totalFrames) + 1; Avoid games applelettercatch.fla

  37. _root["apple"+lastApple].gotoAndStop(f); _root["apple"+lastApple]._x = Math.random()*490+30; _root["apple"+lastApple]._y = 0; // reset time delay for next apple timeSinceLastApple = 0; // increase apple speed if (appleSpeed < 10) appleSpeed += .5; } } } Avoid games applelettercatch.fla

  38. // even if no apple dropped, get closer to next drop timeSinceLastApple++; } function moveApples() { // loop through all existing apple clips for (i=firstApple;i<=lastApple;i++) { // get apple location x = _root["apple"+i]._x; y = _root["apple"+i]._y + appleSpeed; Avoid games applelettercatch.fla

  39. // see whether apple reached ground if (y > 400) { removeApple(i); // see whether apple hit basket } else if ((Math.abs(y-fox._y) < 10) and (Math.abs(x-fox._x) < 25)) { if (_root["apple"+i].type == "good") { // good apple, get a point //differentiates “good” and “bad” catches score += 1; } else { // bad apple, lose a point score -= 1; Avoid games applelettercatch.fla

  40. // don't go below 0 if (score < 0) score = 0; } removeApple(i); // continue to move apple } else { _root["apple"+i]._y = y; } } } Avoid games applelettercatch.fla

  41. function removeApple(n) { // take away apple movie clip _root["apple"+n].removeMovieClip(); // reset range of apples to move firstApple = n+1; // see whether this was the last apple if (n == totalApples) { fox.removeMovieClip(); gotoAndPlay("game over"); } } Avoid games applelettercatch.fla

More Related