A game with "Apple Catch" like gameplay. (part 2)

So far:

Before going any farther, your file/game should have 4 frames, with 2 layers on your timeline. One layer for your background art, and another for your buttons. On frame 1 there should be a background piece of art and 2 buttons, one for "instructions" and the other "to play". Also you will want a title and author name on this frame. On the second frame, there should be a background, the instructions on how to play the game and a button to play the game. On the third frame, which is where the actual game will reside, you should only have the background. On the fourth frame, you'll want to have a background graphic and a button to return the user to the main menu/page as well as some sort of feedback on their performance.

  1. Add a new layer to your main timeline and call it "code".
  2. In each frame on the code layer, select the frame and add the following command into the actions pallette.

    stop();

    That command tells the Flash player to stop on that frame.
  3. Now we can add some code to our buttons so we can navigate through each of the frames when we test our game/file/movie.

    On frame 1, select the "instructions" button and add the following code into the actions pallette

    on (press) {
    go to and stop("instructions");// or go to and stop(2);
    }

    in the above case "instructions" is the same as frame 2, so you can refer to a frame numerically or if you have named it, you can reference it by that.

    On frame 1, select the "play" button and add the following code into the actions pallette

    on (press) {
    go to and stop("game");// or go to and stop(3);
    }

Now, lets code the engine that will run this game, the following code will need to be placed on the code layer, in frame 3. Insert the following code into your actions pallette:

stop();//tells the main timeline to stop on this frame
basket.startDrag(true, 10, 333, 590, 333);//defines where we can move our basket
fscommand(allowscale, false);//keeps our movie at the 400H x 600W pixel format
score = 0;//score is a variable with a value of zero
u = 0;//u is a variable with a value of zero, it is used to help identify broken apples
right = 600;//_x value for the right side of screen
left = 0;//_x value for the left side of the screen
top = 0;//_y value for the top of the screen
bottom = 350;//_y value for the bottom of the screen
tm = 0;//tm is our variable for time
tDif = 2;//tDif is our variable for the time between apples coming on screen
startTime = Math.floor(getTimer()/1000);//variable that gets the time you atart playing
ot = getTimer()/1000-3;//ot is a variable for the old time
otd = getTimer()/1000;//otd is a variable for the old time difference
a = 0;//a is another variable, used to identify apples
maxA = 10;//variable for maximum amount of apples on screen at one time
applesSaved = 0;//variable for amount of apples saved/caught in basket
//moveApple is a function that controls the apples, moves apples, adds splat if one hits the ground, and hitTests between apples and basket to see if you catch one
moveApple = function () {
for (i=0; i<=maxA; i++) {
_root["apple"+i].yspeed++;
_root["apple"+i]._y += _root["apple"+i].yspeed;
_root["apple"+i]._x += _root["apple"+i].xspeed;
//if the apple hits the ground, remove it and add an effect
if (_root["apple"+i]._y>bottom) {
_root.attachMovie("uhoh", "uhoh"+u, 200+u);
_root["uhoh"+u]._y = _root["ball"+i]._y-50;
_root["uhoh"+u]._x = _root["ball"+i]._x;
u++;
score -= 1;
_root["apple"+i].removeMovieClip();
//this line of code would reverse the apples direction
// _root["apple"+i].yspeed *= -1;
}
//place a mc inside of the "basket" and name it "top" to check for collisions
if (_root["apple"+i].hitTest(basket.top)) {
//same here, useful in a game like PONG
// _root["apple"+i].yspeed *= -1;
// _root["apple"+i].yspeed = -Math.abs(_root["ball"+i].yspeed);
_root["apple"+i].removeMovieClip();
score += 1;
}
}
};
possibleX = [20, 40, 60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280, 300, 320, 340, 360, 380, 400, 420, 440, 460, 480, 500, 520, 540, 560, 580];//possibleX is a variable that is an array of possible _x positions an apple can start from
possibleY = [-10, -10, -10, -10];//possibleY is a variable that is an array of possible _y positions an apple can start from
//addApple is a function that will add an apple to the screen if enough time has passed from the last time it added an apple
pxl = possibleX.length;//the length of the possibleX array
addApple = function () {
nt = getTimer()/1000;
if (nt>ot+tDif) {
_root.attachMovie("apple", "apple"+a, 100+a);
x = ((Math.floor(Math.random()*pxl)));//variable for...
y = ((Math.floor(Math.random()*4)));//variable for...
//_root["apple"+a].gotoAndStop(x+1);//tells the apple movieclip to stop at frame
_root["apple"+a]._x = possibleX[x];//tells the apple movieclip it's starting _x position
_root["apple"+a]._y = possibleX[y];//tells the apple movieclip it's starting _y position
_root["apple"+a].yspeed = .5;//tells the apple movieclip it's _y speed
//_root["apple"+a].xspeed = x+2;//tells the apple movieclip it's _x speed
ot = getTimer()/1000;// old time variable
++a;//add one to the variable a
if (a>maxA) {
a = 0;
}
}
};
//timer function
tDifUpdate = function () {
ntd = getTimer()/1000;
if (ntd>otd+10) {
tDif -= .1;
otd = getTimer()/1000;
}
};
//if your game has a timer/countdown
timeLeft = function () {
time = Math.floor(getTimer()/1000)-startTime;
if(time > 10){
_root.gotoAndStop(3);
}
};
//our main function that runs everything
onEnterFrame = function () {
if (play) {
//basket._yscale = basket._y-200;//adds dimension
//basket._xscale = basket._y-200;//adds dimension
addApple();
moveApple();
timeLeft();
tDifUpdate();
}
};
//trace(time);//for debugging
//trace(tm);//for debugging
//trace(score);//for debugging

 

Click here to see the text file (another view of the code) or click here to see the actionscript version of the file. Click here to download the .fla

I've commented all the code to help make it easier to understand.

 

Since my game is using apples as the falling objects, the code reflects that, aslo with using a basket to catch those apples, the code reflects that.

  1. So now on frame 3, on your art layer, drag an instance of the basket (or whatever you'll be using to catch your objects). Name it "basket" in the properties panel, if you call it something else, you'll have to adjust the code. Double click your basket to edit that movieclip, make a new layer inside the mc and make a shape that will be used for collision detection, once you've drawn this shape, convert it to a symbol, make it an mc. Now name you new mc in the properties panel "top". Reposition this layer so it is below the basket artwork layer (so you do not see it).
  2. Now the apples are attached to the stage using code, so do not drag any of those onto the stage. Since the code calling them refers to "apple"+"a variable number", so inside the library, set the apple mc's properties likage identifier to "apple", else the computer won't recognize it at runtime.




  3. If you want to see the score when playing, on frame 3 of the main timeline, you can add a new layer above the others and draw a text box, this will be a static text box, and type "score". Now to the right of that box, draw another text box and make it a dynamic text box using the properties panel, type "score" without the quotes into the variable field. Now everytime the score changes, for better or worse, the data populates that dynamic text box.
  4. In the next lesson, we'll discuss ways to modify/enhance the game.