In this tutorial you'll put together a simple but full featured game. You've already performed most of the steps in previous tutorials. If you feel comfortable with actionScript and Flash, you can skim this tutorial for new material. If not, you should proceed step by step.
//Main Game ScriptAny acript you add in this tutorial should be added here unless you are explicitly told otherwise.
<level backgroundClip="mBackground0"> </level>
// initialization code
theLevel = new XML();
theLevel.ignoreWhite=true;
theLevel.onLoad=parseLevel;
theLevel.load("level0.xml");
function parseLevel() {
trace("Read level description.");
var p=theLevel.childNodes[0];
// set up background
imScene.attachMovie(p.attributes.backgroundClip,"i"+p.attributes.backgroundClip,0);
}
Run the game. It should load the background image.
imMask._visible = false; imScene.setMask(imMask);The first prevents the rectangle from being drawn. The second line prevents portions of imScene that lie outside the rectangle from being drawn.
<level backgroundClip="mBackground0" xStart="225" yStart="155">
// set up the player's character(pc) imPC._x=Number(p.attributes.xStart); imPC._y=Number(p.attributes.yStart);
//Global variables var xVelocity=0; // pc's x velocity var yVelocity=0; // pc's y velocity var acceleration=2; // pc's acceleration due to arrow key
//functions
onEnterFrame=function () {
checkInput();
imScene._x -= xVelocity;
imScene._y -= yVelocity;
}
function checkInput() {
if(Key.isDown(Key.RIGHT)) {
xVelocity += acceleration;
}
if (Key.isDown(Key.LEFT)) {
xVelocity -= acceleration;
}
if (Key.isDown(Key.UP)) {
yVelocity -= acceleration;
}
if (Key.isDown(Key.DOWN)) {
yVelocity += acceleration;
}
}
<platforms> <platform x="0" y="208" w="1200" h="196" v="false" clip="mPlatform"></platform> <platform x="150" y="100" w="100" h="40" v="true" clip="mPlatform"></platform> <platform x="500" y="120" w="100" h="40" v="true" clip="mPlatform"></platform> </platforms>
var depth=1; // display depth for platforms; starts at 1 var platforms = new Array(); // platform data
// set up the platforms var q=p.childNodes[0]; parsePlatforms(q);
function Platform(x,y,w,h,v,clip,clipName) {
this._x = x;
this._y = y;
this._width = w;
this._height = h;
this._visible = v;
this._clip = clip;
this._clipName = clipName
}
function parsePlatforms(p) {
numPlatforms=p.childNodes.length;
for (var i=0;i<numPlatforms;i++) {
platforms[i] = new Platform(
Number(p.childNodes[i].attributes.x),
Number(p.childNodes[i].attributes.y),
Number(p.childNodes[i].attributes.w),
Number(p.childNodes[i].attributes.h),
Boolean(p.childNodes[i].attributes.v=="true"),
p.childNodes[i].attributes.clip,
"i"+p.childNodes[i].attributes.clip+i);
theClip = imScene.attachMovie(platforms[i]._clip,platforms[i]._name,depth);
depth++;
theClip._x = platforms[i]._x;
theClip._y = platforms[i]._y;
theClip._height = platforms[i]._height;
theClip._width = platforms[i]._width;
theClip._visible = platforms[i]._visible;
}
}
var xChange; var yChange; var damp=.8 var onSurface = true; var epsilon=.1
onEnterFrame=function () {
checkInput();
platformDetect();
imScene._x -= xChange;
imScene._y -= yChange;
}
function platformDetect() {
// initialize change to the velocities
xChange = xVelocity;
yChange = yVelocity;
// if the player's character isn't moving there is not
// going to be a collision
if (xVelocity==0 && yVelocity==0)
return false;
// otherwise we have to check for collision
var pTop, pBottom, pLeft, pRight; // platform positions
var cTopStart, cBottomStart, cLeftStart, cRightStart; // character positions at start
var cTopEnd, cBottomEnd, cLeftEnd, cRightEnd; // character positions at end
// get boundaries of player's character before move
cTopStart = imPC._y;
cBottomStart = cTopStart + imPC._height;
cLeftStart = imPC._x;
cRightStart = cLeftStart + imPC._width;
// get boundaries of player's character after move
cTopEnd = cTopStart + yVelocity;
cBottomEnd = cBottomStart + yVelocity;
cLeftEnd = cLeftStart + xVelocity;
cRightEnd = cRightStart + xVelocity;
// assume not on surface
onSurface=false;
// test against each platform
for (i=0;i<platforms.length;i++) {
// get platform boundaries in screen coordinates
pTop = platforms[i]._y + imScene._y;
pBottom = platforms[i]._y + imScene._y + platforms[i]._height;
pLeft = platforms[i]._x + imScene._x;
pRight = platforms[i]._x + imScene._x + platforms[i]._width;
// check if we've collided with the platform
if ((cBottomEnd ≥ pTop) && (cTopEnd ≤ pBottom) && (cRightEnd ≥ pLeft) && (cLeftEnd ≤ pRight)) {
// we do collide when we move
// now find which side of the platform we hit
// check top
// did we start out above and move down?
// because of numerical inaccuracies we need
// an epsilon tolerance in our test so we don't
//fall through platforms
if (cBottomStart ≤ pTop+epsilon) {
onSurface=true;
yChange=pTop-cBottomStart;
if (yVelocity >0) {
trace("collided with top of platform ");
var fraction=(pTop-cBottomStart)/yVelocity;
xChange=xVelocity*fraction;
// if we are making very small bounces
// stop the ball
if (yVelocity < 0.9) {
yVelocity=0;
}
yVelocity *= - damp *.75;
}
}
// check bottom
// did we start out below and move up?
else if (cTopStart ≥ pBottom && yVelocity < 0) {
var fraction=(pBottom-cTopStart)/yVelocity;
xChange=xVelocity*fraction;
yChange=pBottom-cTopStart;
yVelocity *= - damp;
trace("collided with bottom of platform");
}
// check left of platform
// did we start to the left and move right
if (cRightStart ≤ pLeft && xVelocity > 0) {
var fraction=(cRightStart-pLeft)/xVelocity;
xChange = cRightStart-pLeft;
if (!onSurface) {
yChange = fraction*yVelocity;
}
xVelocity *= - damp;
trace("collided with left of platform");
}
// check right of platform
// did we start to the right and move left
else if (cLeftStart ≥ pRight && xVelocity < 0) {
var fraction=(cLeftStart-pRight)/xVelocity;
xChange = pRight-cLeftStart;
if (!onSurface) {
yChange = fraction * yVelocity;
}
xVelocity *= - damp;
trace("collided with right of platform");
}
} // end of if
} // end of for
}
var gravity = .5; var friction = .95;Add the following code in the onEnterFrame function after checkInput and before detectPlatform.
if (onSurface) {
// add friction
xVelocity *= friction;
if (Math.abs(xVelocity)<.2) {
xVelocity = 0;
}
}
else {
yVelocity += gravity;
}