------------------------------------------------------------------------------------------------------------------------
1) MAKE FLASH FOLDER ON DESKTOP
2) SAVE FLA FILE IN THIS FODER
3) INSIDE THIS MAKE Game FOLDER WITH CLASS FILE C.as
ReadMore:
http://www.makeflashgames.com/tutorials/tut1.php
Download your files here
-------------------------------------------------------------------------------------------------------------------------
CODE : C.as
-------------------------------------------------------------------------------------------------------------------------
//SAVE THIS CODE IN C.as
package Game{
public class C{
public static const GRAVITY:Number = 3;
public static const HIT_TOLERANCE:Number = 40;
public static const PLAYER_SPEED:Number = 5;
public static const JOVIA_RIGHT:String = "right";
public static const JOVIA_LEFT:String = "left";
public static const APPLE_END_Y:Number = 440;
public static const APPLE_SPAWN_CHANCE:Number = 0.05;
public static const APPLE_START_Y:Number = 10;
public static const APPLE_SPAWN_START_X:Number = 50;
public static const APPLE_SPAWN_END_X:Number = 550;
public static const PLAYER_START_SCORE:Number = 0;
public static const SCORE_PER_APPLE:Number = 10;
}
}
-----------------------------------------------------------------------------------------------------------------
CODE : GameController.as FULL CODE
-----------------------------------------------------------------------------------------------------------------
package{
import flash.display.MovieClip;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
import Game.*;
public class GameController extends MovieClip{
private var player:Jovia;
private var speed:Number;
private var gravity:Number;
private var randomChance:Number;
private var apples:Array;
private var score:Number;
public function GameController(){}
public function startGame(){
speed = C.PLAYER_SPEED;
gravity = C.GRAVITY;
score = C.PLAYER_START_SCORE;
randomChance = C.APPLE_SPAWN_CHANCE;
apples = new Array();
player = new Jovia();
mcGameStage.addChild(player);
mcGameStage.addEventListener(Event.ENTER_FRAME,update);
}
private function update(evt:Event){
//******************
//Handle User Input
//******************
var currMouseX = mouseX;
var currMouseY = mouseY;
var moveX = 0;
var moveY = 0;
if (currMouseX > player.x){
moveX = 1;
}
else if (currMouseX < player.x){
moveX = -1;
}
if (currMouseY > player.y){
moveY = 1;
}
else if (currMouseY < player.y){
moveY = -1;
}
//******************
//Handle Game Logic
//******************
//Handle new Player Position
if (moveX > 0){
if (player.x + C.PLAYER_SPEED <= currMouseX)
player.x += C.PLAYER_SPEED;
}
else if (moveX < 0){
if (player.x - C.PLAYER_SPEED > currMouseX)
player.x -= C.PLAYER_SPEED;
}
if (moveY > 0){
if (player.y + C.PLAYER_SPEED <= currMouseY)
player.y += C.PLAYER_SPEED;
}
else if (moveY < 0){
if (player.y - C.PLAYER_SPEED > currMouseY)
player.y -= C.PLAYER_SPEED;
}
//Spawn new apples
if (Math.random() < randomChance){
var newApple = new Apple();
newApple.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
newApple.y = C.APPLE_START_Y;
apples.push(newApple);
mcGameStage.addChildAt(newApple,0);
}
//Move Apples PROTECT FOR DUPLICATE ERRORS
for (var j = apples.length-1; j >= 0; j--)
{
apples[j].y += gravity;
if (apples[j].y > C.APPLE_END_Y)
{
mcGameStage.removeChild(apples[j]);
apples.splice(j,1);
}
}
//Check for collision
var playerPoint = new Point(player.x, player.y);
for (var i = apples.length-1; i >= 0; i--)
{
var applePoint:Point = new Point(apples[i].x, apples[i].y);
if (Point.distance(applePoint,playerPoint) < C.HIT_TOLERANCE)
{
//Register hit
score += C.SCORE_PER_APPLE;
mcGameStage.removeChild(apples[i]);
apples.splice(i,1);
}
}
//******************
//Handle Display
//******************
//Animate the player
if (moveX > 0)
{
if (player.currentLabel != C.JOVIA_RIGHT)
player.gotoAndPlay(C.JOVIA_RIGHT);
}
else if (moveX < 0)
{
if (player.currentLabel != C.JOVIA_LEFT)
player.gotoAndPlay(C.JOVIA_LEFT);
}
//Display new Score
txtScore.text = String(score);
}
}
}
-------------------------------------------------------------------------------------------------------------------------
0) MAKE W:600 X H:500 SIZE STAGE
1) USE CONTENTS LAYER
2) MAKE LIGHT GREEN MOVIE CLIP WITH INSTANCE NAME mcGameStage
W: 600 X H:500 SIZE
3) MAKE TEXT BOX WITH INSTANCE NAME txtScore
4) PASTE CODE IN AS LAYER
-------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------
AS LAYER CODE:
-------------------------------------------------------------------------------------------------------------------------
//AS LAYER CODE
startGame();
import flash.display.MovieClip;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
//HOW IMPORT CLASS: import Game.*;
//HOW IMPORT CLASS: import Game.C;
//HERE GAME FOLDER NAME AND C FILECLASS NAME
import Game.*;
var player:Jovia;
var speed:Number;
var gravity:Number;
var randomChance:Number;
var apples:Array;
var score:Number;
function startGame(){
speed = C.PLAYER_SPEED;
gravity = C.GRAVITY;
score = C.PLAYER_START_SCORE;
randomChance = C.APPLE_SPAWN_CHANCE;
apples = new Array();
player = new Jovia();
mcGameStage.addChild(player);
mcGameStage.addEventListener(Event.ENTER_FRAME,update);
}
function update(evt:Event){
var currMouseX = mouseX;
var currMouseY = mouseY;
var moveX = 0;
var moveY = 0;
if (currMouseX > player.x){
moveX = 1;
}
else if (currMouseX < player.x){
moveX = -1;
}
if (currMouseY > player.y){
moveY = 1;
}
else if (currMouseY < player.y){
moveY = -1;
}
if (moveX > 0){
if (player.x + C.PLAYER_SPEED <= currMouseX)
player.x += C.PLAYER_SPEED;
}
else if (moveX < 0){
if (player.x - C.PLAYER_SPEED > currMouseX)
player.x -= C.PLAYER_SPEED;
}
if (moveY > 0){
if (player.y + C.PLAYER_SPEED <= currMouseY)
player.y += C.PLAYER_SPEED;
}
else if (moveY < 0)
{
if (player.y - C.PLAYER_SPEED > currMouseY)
player.y -= C.PLAYER_SPEED;
}
if (Math.random() < randomChance){
var newApple = new Apple();
newApple.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
newApple.y = C.APPLE_START_Y;
apples.push(newApple);
mcGameStage.addChildAt(newApple,0);
}
for (var i = apples.length-1; i >= 0; i--){
apples[i].y += gravity;
if (apples[i].y > C.APPLE_END_Y){
mcGameStage.removeChild(apples[i]);
apples.splice(i,1);
}
}
var playerPoint = new Point(player.x, player.y);
for (var j = apples.length-1; j >= 0; j--){
var applePoint:Point = new Point(apples[j].x, apples[j].y);
if (Point.distance(applePoint,playerPoint) < C.HIT_TOLERANCE){
score += C.SCORE_PER_APPLE;
mcGameStage.removeChild(apples[j]);
apples.splice(j,1);
}
}
if (moveX > 0){
if (player.currentLabel != C.JOVIA_RIGHT)
player.gotoAndPlay(C.JOVIA_RIGHT);
}
else if (moveX < 0){
if (player.currentLabel != C.JOVIA_LEFT)
player.gotoAndPlay(C.JOVIA_LEFT);
}
txtScore.text = String(score);
}
------------------------------------------------------------------------------------------------------------------
OR USE THIS CODE:
PUBLIC VARIABLES CHANGE INTO PRIVATE VARIABLES AS3
------------------------------------------------------------------------------------------------------------------
package{
import flash.display.MovieClip;
import flash.events.*;
import flash.geom.*;
import flash.text.*;
public class GameController extends MovieClip{
private var player:Jovia;
private var speed:Number;
private var randomChance:Number;
private var apples:Array;
private var score:Number;
//******************
//CLASS C PUBLIC VARIABLE CHANGE INTO PRIVATE
//ALSO DELETE C. WORD
//LIKE speed = C.PLAYER_SPEED;
//CHANGE speed = PLAYER_SPEED;
// ALSO DELETE LINE import Game.*;
//DON'T NEED C CLASS FILE
//******************
//Game
private var gravity:Number;
private var GRAVITY:Number = 3;
private var HIT_TOLERANCE:Number = 40;
//Player
private var PLAYER_SPEED:Number = 5;
private var JOVIA_RIGHT:String = "right";
private var JOVIA_LEFT:String = "left";
//Powerup
private var APPLE_END_Y:Number = 440;
private var APPLE_SPAWN_CHANCE:Number = 0.05;
private var APPLE_START_Y:Number = 10;
private var APPLE_SPAWN_START_X:Number = 50;
private var APPLE_SPAWN_END_X:Number = 550;
//Scoring
private var PLAYER_START_SCORE:Number = 0;
private var SCORE_PER_APPLE:Number = 10;
public function GameController(){}
public function startGame(){
speed = PLAYER_SPEED;
gravity = GRAVITY;
score = PLAYER_START_SCORE;
randomChance = APPLE_SPAWN_CHANCE;
apples = new Array();
player = new Jovia();
mcGameStage.addChild(player);
mcGameStage.addEventListener(Event.ENTER_FRAME,update);
}
private function update(evt:Event){
//******************
//Handle User Input
//******************
var currMouseX = mouseX;
var currMouseY = mouseY;
var moveX = 0;
var moveY = 0;
if (currMouseX > player.x){
moveX = 1;
}
else if (currMouseX < player.x){
moveX = -1;
}
if (currMouseY > player.y){
moveY = 1;
}
else if (currMouseY < player.y){
moveY = -1;
}
//******************
//Handle Game Logic
//******************
//Handle new Player Position
if (moveX > 0){
if (player.x + PLAYER_SPEED <= currMouseX)
player.x += PLAYER_SPEED;
}
else if (moveX < 0){
if (player.x - PLAYER_SPEED > currMouseX)
player.x -= PLAYER_SPEED;
}
if (moveY > 0){
if (player.y + PLAYER_SPEED <= currMouseY)
player.y += PLAYER_SPEED;
}
else if (moveY < 0){
if (player.y - PLAYER_SPEED > currMouseY)
player.y -= PLAYER_SPEED;
}
//Spawn new apples
if (Math.random() < randomChance){
var newApple = new Apple();
newApple.x = Math.random() * APPLE_SPAWN_END_X + APPLE_SPAWN_START_X;
newApple.y =APPLE_START_Y;
apples.push(newApple);
mcGameStage.addChildAt(newApple,0);
}
//Move Apples
for (var i = apples.length-1; i >= 0; i--){
apples[i].y += gravity;
if (apples[i].y > APPLE_END_Y){
mcGameStage.removeChild(apples[i]);
apples.splice(i,1);
}
}
//Check for collision
var playerPoint = new Point(player.x, player.y);
for (var j = apples.length-1; j >= 0; j--){
var applePoint:Point = new Point(apples[j].x, apples[j].y);
if (Point.distance(applePoint,playerPoint) <HIT_TOLERANCE){
//Register hit
score += SCORE_PER_APPLE;
mcGameStage.removeChild(apples[j]);
apples.splice(j,1);
}
}
//******************
//Handle Display
//******************
//Animate the player
if (moveX > 0){
if (player.currentLabel != JOVIA_RIGHT)
player.gotoAndPlay(JOVIA_RIGHT);
}
else if (moveX < 0){
if (player.currentLabel != JOVIA_LEFT)
player.gotoAndPlay(JOVIA_LEFT);
}
//Display new Score
txtScore.text = String(score);
}
}
}
------------------------------------------------------------------------------------------------------------------
NO NEED ANY CLASS USE THIS CODE
DELETE PRIVATE WORD
DELETE THIS LINE startGame();
DELETE THIS LINE public function startGame(){}
------------------------------------------------------------------------------------------------------------------
var player:Jovia;
var speed:Number;
var randomChance:Number;
var apples:Array;
var score:Number;
//******************
//CLASS C PUBLIC VARIABLE CHANGE INTO PRIVATE
//ALSO DELETE C. WORD
//LIKE speed = C.PLAYER_SPEED;
//CHANGE speed = PLAYER_SPEED;
// ALSO DELETE LINE import Game.*;
//DON'T NEED C CLASS FILE
//******************
//Game
var gravity:Number = 0;
var GRAVITY:Number = 3;
var HIT_TOLERANCE:Number = 40;
//Player
var PLAYER_SPEED:Number = 5;
var JOVIA_RIGHT:String = "right";
var JOVIA_LEFT:String = "left";
//Powerup
var APPLE_END_Y:Number = 440;
var APPLE_SPAWN_CHANCE:Number = 0.05;
var APPLE_START_Y:Number = 10;
var APPLE_SPAWN_START_X:Number = 50;
var APPLE_SPAWN_END_X:Number = 550;
//Scoring
var PLAYER_START_SCORE:Number = 0;
var SCORE_PER_APPLE:Number = 10;
speed = PLAYER_SPEED;
gravity = GRAVITY;
score = PLAYER_START_SCORE;
randomChance = APPLE_SPAWN_CHANCE;
apples = new Array();
player = new Jovia();
mcGameStage.addChild(player);
mcGameStage.addEventListener(Event.ENTER_FRAME,update);
function update(evt:Event){
//******************
//Handle User Input
//******************
var currMouseX = mouseX;
var currMouseY = mouseY;
var moveX = 0;
var moveY = 0;
if (currMouseX > player.x){
moveX = 1;
}
else if (currMouseX < player.x){
moveX = -1;
}
if (currMouseY > player.y){
moveY = 1;
}
else if (currMouseY < player.y){
moveY = -1;
}
//******************
//Handle Game Logic
//******************
//Handle new Player Position
if (moveX > 0){
if (player.x + PLAYER_SPEED <= currMouseX)
player.x += PLAYER_SPEED;
}
else if (moveX < 0){
if (player.x - PLAYER_SPEED > currMouseX)
player.x -= PLAYER_SPEED;
}
if (moveY > 0){
if (player.y + PLAYER_SPEED <= currMouseY)
player.y += PLAYER_SPEED;
}
else if (moveY < 0){
if (player.y - PLAYER_SPEED > currMouseY)
player.y -= PLAYER_SPEED;
}
//Spawn new apples
if (Math.random() < randomChance){
var newApple = new Apple();
newApple.x = Math.random() * APPLE_SPAWN_END_X + APPLE_SPAWN_START_X;
newApple.y =APPLE_START_Y;
apples.push(newApple);
mcGameStage.addChildAt(newApple,0);
}
//Move Apples
for (var i = apples.length-1; i >= 0; i--){
apples[i].y += gravity;
if (apples[i].y > APPLE_END_Y){
mcGameStage.removeChild(apples[i]);
apples.splice(i,1);
}
}
//Check for collision
var playerPoint = new Point(player.x, player.y);
for (var j = apples.length-1; j >= 0; j--){
var applePoint:Point = new Point(apples[j].x, apples[j].y);
if (Point.distance(applePoint,playerPoint) <HIT_TOLERANCE){
//Register hit
score += SCORE_PER_APPLE;
mcGameStage.removeChild(apples[j]);
apples.splice(j,1);
}
}
//******************
//Handle Display
//******************
//Animate the player
if (moveX > 0){
if (player.currentLabel != JOVIA_RIGHT)
player.gotoAndPlay(JOVIA_RIGHT);
}
else if (moveX < 0){
if (player.currentLabel != JOVIA_LEFT)
player.gotoAndPlay(JOVIA_LEFT);
}
//Display new Score
txtScore.text = String(score);
}
------------------------------------------------------------------------------------------------------------------
0) USE AS LAYER
1) ON AS LAYER MAKE MOVIE CLIPS AND GIVE CLASSES NAMES
Jovia ; Apple
-----------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
INSIDE JOVIA HAS 2 FRAMES WITH LABELS right AND left
NOW TAKE A INSIDE VIEW
INSIDE IT HAS TWO FRAMES WITH DIFFERENT RIGHT LEFT IMAGES AND
FAMES HAVE ALSO CODES
-----------------------------------------------------------------------------------------------------------------
//INSIDE JOVIA FRAME NO: 1 CODE
gotoAndPlay("right");
//INSIDE JOVIA FRAME NO: 2 CODE
gotoAndPlay("left");
-----------------------------------------------------------------------------------------------------------------
USEFUL IMAGES
FLASH CS4 DOES NOT EXPORT GIF ANIMATED WITH TRANSPARENT BACKGROUND
-----------------------------------------------------------------------------------------------------------------
HOW public static const TRACE IN APPLE CATCHING GAME AS3
http://flashcollege.blogspot.co.uk/2016/06/main-class-as3.html
-------------------------------------------------------------------------------------------------------------------
//FLA AS3 CODE
import Game.*;
var gravity:Number;
gravity = C.GRAVITY;
-------------------------------------------------------------------------------------------------------------------
//SAVE THIS CODE IN C.as IN Game FOLDER
package Game{
public class C{
public static const GRAVITY:Number = 3;
trace(GRAVITY);
}
}
-------------------------------------------------------------------------------------------------------------------
EmoticonEmoticon