Простой платформер на Flash | В паутине

Простой платформер на Flash

Полный туториал по созданию простой игры-платформера на Flash. Игра написана на языке ActionScript 3.0.
Оригинал руководства (на англ. языке) здесь.

Все уроки руководства

1. Создание главного персонажа
2. Создание уровня
3. Взаимодействие персонажа с блоками
4. Добавление элементов уровня
5. Добавление врагов
6. Завершение уровня
7. Последние штрихи

Полный код приложения

//LEVEL VARIABLES
//current row that we are creating
var row:int = 0;
//the current lvl
var lvlCurrent:int = 1;
/*The key for the level arrays:
1: Regular Block
2: Ladder
3: Bumper
4: Trampoline
5: Enemy
6: Invisible Marker
7: Coin
8: Goal
X: Main Character
*/
//this variable will hold the character
var X:String = 'MAIN';
//the array for level 1
var lvlArray1:Array = new Array(
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,7,7,8,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,0,0,5,0,0,6,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,7,7,7,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0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0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
);

//whether or not main is on the ground
var mainOnGround:Boolean = false;
//These variables will note which keys are down
//We don't need the up or down key just yet
//but we will later
var leftKeyDown:Boolean = false;
var upKeyDown:Boolean = false;
var rightKeyDown:Boolean = false;
var downKeyDown:Boolean = false;
//the main character's speed
var mainSpeed:Number = 7;
//whether or not the main guy is jumping
var mainJumping:Boolean = false;
//how quickly should the jump start off
var jumpSpeedLimit:int = 20;
//the current speed of the jump;
var jumpSpeed:Number = jumpSpeedLimit;

//checking if the guy is on a ladder
var mainOnLadder:Boolean = false;

//BUMPING VARIABLES
//if main is being bumped
var mainBumping:Boolean = false;
//how quickly he should be bumped
var bumpSpeed:int = 20;

//SCORING VARIABLES
var mainScore:int = 0;

//adding a listener to mcMain which will make it move 
//based on the key strokes that are down
mcMain.addEventListener(Event.ENTER_FRAME, moveChar);
function moveChar(event:Event):void{
	//if certain keys are down then move the character
	if(leftKeyDown){
		lvlHolder.x += mainSpeed;
		bgHolder.x -= mainSpeed*.5;
	}
	if(rightKeyDown){
		lvlHolder.x -= mainSpeed;
		bgHolder.x += mainSpeed*.5;
	}
	if(upKeyDown || mainJumping){
		if(!mainOnLadder){
			mainJump();
		}
	}
	if(upKeyDown && mainOnLadder){
		mcMain.y -= mainSpeed;
	}
	if(downKeyDown && mainOnLadder && !mainOnGround){
		mcMain.y += mainSpeed;
	}
	//checking if he isn't on ground
	//run the loop checking if he's touching any blocks again
	for(var i:int = 0;i<blockHolder.numChildren; i++){
		//getting the current block
		var hitBlock:DisplayObject = blockHolder.getChildAt(i);
		//checking hit test and if main is above the brick
		if(mcMain.hitTestObject(hitBlock) && mcMain.y < hitBlock.y){
			//this time, we just make mainOnGround true
			mainOnGround = true;
			//and break from the loop
			break;
		}
		mainOnGround = false;
	}
	
	//hit testing for ladders
	for(i = 0;i<ladderHolder.numChildren;i++){
		//getting the current ladder
		var hitLadder:DisplayObject = ladderHolder.getChildAt(i);
		//checking hit test with the ladder
		if(mcMain.hitTestObject(hitLadder)){
				//checking if the guy is close enough
				if(mcMain.x >= hitLadder.x + lvlHolder.x - 10 && mcMain.x <= hitLadder.x + lvlHolder.x + 35){
				mainOnLadder = true;
				//reset the jump speed so he doesn't keep on jumping
				jumpSpeed = jumpSpeedLimit;
				break;
			}
		}
		mainOnLadder = false;
	}
	//hit testing for bumpers
	for(i=0;i<bumperHolder.numChildren;i++){
		//getting current bumper
		var hitBumper:DisplayObject=bumperHolder.getChildAt(i);
		//hit testing with the bumper
		if(mcMain.hitTestObject(hitBumper)){
			mainBumping = true;
			bumpSpeed = 20;
		}
	}
	//run mainBump
	mainBump();
	//hit testing trampolines
	for(i=0;i<trampHolder.numChildren;i++){
		//you know the drill
		var hitTramp:DisplayObject=trampHolder.getChildAt(i);
		if(mcMain.hitTestObject(hitTramp)){
			//just make him jump
			mainJump();
		}
	}
	//then we make him fall if he isn't on ground
	if(!mainOnGround){
		mainJumping = true;
	}
	//checking death
	if(mcMain.y >= stage.stageHeight){
		resetLvl();
	}
	
	//updating score txt
	txtScore.text = "Score: "+mainScore;
}

//listening for the keystrokes
//this listener will listen for down keystrokes
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
function checkKeysDown(event:KeyboardEvent):void{
	//making the booleans true based on the keycode
	//WASD Keys or arrow keys
	if(event.keyCode == 37 || event.keyCode == 65){
		leftKeyDown = true;
	}
	if(event.keyCode == 38 || event.keyCode == 87){
		upKeyDown = true;
	}
	if(event.keyCode == 39 || event.keyCode == 68){
		rightKeyDown = true;
	}
	if(event.keyCode == 40 || event.keyCode == 83){
		downKeyDown = true;
	}
}
//this listener will listen for keys being released
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
function checkKeysUp(event:KeyboardEvent):void{
	//making the booleans false based on the keycode
	if(event.keyCode == 37 || event.keyCode == 65){
		leftKeyDown = false;
	}
	if(event.keyCode == 38 || event.keyCode == 87){
		upKeyDown = false;
	}
	if(event.keyCode == 39 || event.keyCode == 68){
		rightKeyDown = false;
	}
	if(event.keyCode == 40 || event.keyCode == 83){
		downKeyDown = false;
	}
}

//jumping function
function mainJump():void{
	//if main isn't already jumping
	if(!mainJumping){
		//then start jumping
		mainJumping = true;
		jumpSpeed = jumpSpeedLimit*-1;
		mcMain.y += jumpSpeed;
	} else {
		//then continue jumping if already in the air
		//crazy math that I won't explain
		if(jumpSpeed < 0){
			jumpSpeed *= 1 - jumpSpeedLimit/125;
			if(jumpSpeed > -jumpSpeedLimit/5){
				jumpSpeed *= -1;
			}
		}
		if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){
			jumpSpeed *= 1 + jumpSpeedLimit/50;
		}
		mcMain.y += jumpSpeed;
		
		//if main hits a block, then stop jumping
		//this loop will check a hit test with any block
		for(var i:int = 0;i<blockHolder.numChildren; i++){
			//getting the current block
			var hitBlock:DisplayObject = blockHolder.getChildAt(i);
			//checking hit test
			if(mcMain.hitTestObject(hitBlock)){
				//if main is falling down
				if(jumpSpeed > 0){
					//stop jumping
					mainJumping = false;
					//and set the guys coordinates to be on top of the block
					mcMain.y = hitBlock.y - mcMain.height;
					//he's on solid ground
					mainOnGround = true;
					//break out of the loop
					break;
				} else {
					jumpSpeed = Math.abs(jumpSpeed);
					//making the main guy get away from the block
					mcMain.y = hitBlock.y + hitBlock.height + 1;
				}
			}
		}
	}
}

//creating the level
var lvlHolder:Sprite = new Sprite();
addChild(lvlHolder);
//bg
var bgHolder:Sprite = new Sprite();
lvlHolder.addChild(bgHolder);
//hold all trampolines
var trampHolder:Sprite = new Sprite();
lvlHolder.addChild(trampHolder);
//this guy will hold all of the blocks
var blockHolder:Sprite = new Sprite();
lvlHolder.addChild(blockHolder);
//this guy will hold all of the ladders
var ladderHolder:Sprite = new Sprite();
lvlHolder.addChild(ladderHolder);
//this guy wil hold all of the bumpers
var bumperHolder:Sprite = new Sprite();
lvlHolder.addChild(bumperHolder);
//enemy holder
var enemyHolder:Sprite = new Sprite();
lvlHolder.addChild(enemyHolder);
//the inivisible markers for enemies
var invisMarkerHolder:Sprite = new Sprite();
lvlHolder.addChild(invisMarkerHolder);

function createLvl():void{
	//getting the current level that we are on
	var lvlArray:Array = MovieClip(root)['lvlArray'+lvlCurrent];
	//we have to find how far this level goes
	//this will be used so we know when to move to the next row
	//there will always be 16 rows, so this is how we find it out
	//of course, this will make the lvl formatting very strict
	var lvlColumns:int = Math.ceil(lvlArray.length/16);
	//now we must create the level
	for(var i:int = 0;i<lvlArray.length;i++){
		//checking if we move onto the next row
		//this checks if i is divisible by the # of columns
		if(i/lvlColumns == int(i/lvlColumns)){
			row ++;
		}
		//defining a target
		var newPlacement;
		if(lvlArray[i] == 1){
			//making a new block
			newPlacement = new Block();
			//drawing the block
			newPlacement.graphics.beginFill(0xFFFFFF/*The color for shape*/,1/*The alpha for the shape*/);
			//turning the shape into a square
			newPlacement.graphics.drawRect(0,0,25,25);
			//then finally adding it to stage
			blockHolder.addChild(newPlacement);
		} else if (lvlArray[i] == 'MAIN'){
			newPlacement = mcMain;
		} else if (lvlArray[i] == 2){
			newPlacement = new Sprite();
			//drawing the ladder
			newPlacement.graphics.beginFill(0xFFFF00,1);
			//turning it into a square
			newPlacement.graphics.drawRect(0,0,25,25);
			//then adding it to stage
			ladderHolder.addChild(newPlacement);
		} else if (lvlArray[i] == 3){
			newPlacement = new Sprite();
			//drawing the bumper
			newPlacement.graphics.beginFill(0x00FF00,1);
			//turning it into a square
			newPlacement.graphics.drawRect(0,0,25,25);
			//then adding it to stage
			bumperHolder.addChild(newPlacement);
		} else if (lvlArray[i] == 4){
			newPlacement = new Shape();
			newPlacement.graphics.beginFill(0x00FF00);
			newPlacement.graphics.drawCircle(12.5,25,12.5);
            trampHolder.addChild(newPlacement);
		} else if (lvlArray[i] == 5){
			//adding an enemy
			newPlacement = new Enemy();
			enemyHolder.addChild(newPlacement);
		} else if (lvlArray[i] == 6){
			//adding an invisible marker for the enemy to turn around
			newPlacement = new Shape();
			newPlacement.graphics.beginFill(0x000000,0);
			newPlacement.graphics.drawRect(0,0,25,25);
			invisMarkerHolder.addChild(newPlacement);
		} else if (lvlArray[i] == 7){
			//adding coins
			newPlacement = new Coin();
			//we'll put it in the enemyHolder because this is the only symbol
			//that isn't being looped through
			enemyHolder.addChild(newPlacement);
		} else if (lvlArray[i] == 8){
			//the goal!
			newPlacement = new Goal();
			enemyHolder.addChild(newPlacement);
		}
		
		if(lvlArray[i] != 0){
			//change the coordinates of the block
			newPlacement.x = (i-(row-1)*lvlColumns)*newPlacement.width;
			newPlacement.y = (row-1)*newPlacement.height;
		}
		
		//creating random background particles
		//we'll create 1 particle for every block
		var newPart:Shape = new Shape();
		newPart.graphics.beginFill(0x222222);
		newPart.graphics.drawRect(0,0,int(Math.random()*10)+1,int(Math.random()*10)+1);
		newPart.x = int(Math.random()*lvlColumns*50)-550;
		newPart.y = (row-1)*25;
		bgHolder.addChild(newPart);
	}
	//reset the row for another use
	row = 0;
	//then we center the screen on the main character
	//this variable will tell us how far away we have to move everything
	//250 is about centered for mcMain we'll use that number
	var mainMove:int = 250 - mcMain.x;
	//then move mcMain and the entire level to center on him
	mcMain.x += mainMove;
	lvlHolder.x += mainMove;
	bgHolder.x = 0;
}

//bumping function
function mainBump():void{
	//testing the direction of bump
	var bumpDirection:int;
	if(leftKeyDown){
		bumpDirection = 1;
	} else if (rightKeyDown){
		bumpDirection = -1;
	}
	if(mainBumping){
		lvlHolder.x -= bumpDirection*bumpSpeed;
		bumpSpeed *= .5;
		if(bumpSpeed <= 1){
			mainBumping = false;
		}
	}
}

//resets the level
function resetLvl():void{
	for(var i:int=0;i<lvlHolder.numChildren;i++){
		//selecting the current container
		var currentContainer = lvlHolder.getChildAt(i);
		//and deleting all of it's contents
		while(currentContainer.numChildren > 0){
			for(var i2:int = 0;i2<currentContainer.numChildren;i2++){
				currentContainer.removeChildAt(i2);
			}
		}
	}
	//then we remake the lvl and reset the lvlHolder
	lvlHolder.x = 0;
	createLvl();
	//reset the score
	mainScore = 0;
}
//running the createlvl funciton
createLvl();
//placing the guy on top
MovieClip(root).setChildIndex(mcMain,MovieClip(root).numChildren-1);
package{
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.events.*;
	
	//sprites are just movieclips without any frames in them
	public class Block extends Sprite{
		//_root will signify the root of the document
		private var _root:Object;
		
		public function Block(){
			//this code will only be run once
			addEventListener(Event.ADDED, beginClass);
			//this code will constantly be run
			addEventListener(Event.ENTER_FRAME, eFrame);
		}
		
		private function beginClass(event:Event):void{
			//defining the root of the document
			_root = MovieClip(root);
		}
		
		private function eFrame(event:Event):void{
			
		}
	}
}
package{
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.DisplayObject;
	import flash.events.*;
	//sprites are just movieclips without any frames in them
	public class Coin extends Sprite{
		//_root will signify the root of the document
		private var _root:Object;
		
		public function Coin(){
			//this code will only be run once
			addEventListener(Event.ADDED, beginClass);
			//this code will constantly be run
			addEventListener(Event.ENTER_FRAME, eFrame);
		}
		
		private function beginClass(event:Event):void{
			//defining the root of the document
			_root = MovieClip(root);
			//making an invisible box that will help in placement
			this.graphics.beginFill(0x000000,0);
			this.graphics.drawRect(0,0,25,25);
			//then adding a shape within it that will show the coin
			this.graphics.beginFill(0xFFFFFF,1);
			this.graphics.drawCircle(12.5,12.5,2.5);
		}
		private function eFrame(event:Event):void{
			//hit testing with the coin and the main guy
			//we'll use math for this!
			if(_root.mcMain.x <= this.x + _root.lvlHolder.x + 10
			   && _root.mcMain.x >= this.x + _root.lvlHolder.x - 10
			   && _root.mcMain.y <= this.y + 10
			   && _root.mcMain.y >= this.y - 10){
				this.parent.removeChild(this);
				removeEventListener(Event.ENTER_FRAME, eFrame);
				//then we update the score!
				_root.mainScore += 100;
			}
		}
	}
}
package{
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.DisplayObject;
	import flash.events.*;
	
	//sprites are just movieclips without any frames in them
	public class Enemy extends Sprite{
		//_root will signify the root of the document
		private var _root:Object;
		//how quickly the enemy can move
		private var speed:Number;
		//-1 is left, 1 is right
		private var direction:int;
		
		public function Enemy(){
			//this code will only be run once
			addEventListener(Event.ADDED, beginClass);
			//this code will constantly be run
			addEventListener(Event.ENTER_FRAME, eFrame);
		}
		
		private function beginClass(event:Event):void{
			//defining the root of the document
			_root = MovieClip(root);
			//defining the movement variables of the enemy
			speed = 5;
			direction = 1;
			//drawing the enemy (it'll be a red circle)
			this.graphics.beginFill(0xFF0000,1);
			this.graphics.drawCircle(12.5,12.5,12.5);
		}
		
		private function eFrame(event:Event):void{
			//MOVING THE ENEMY
			this.x += speed * direction;
			//checking if touching any invisible markers
			for(var i:int=0;i<_root.invisMarkerHolder.numChildren;i++){
				//the process is very similar to the main guy's testing with other elements
				var targetMarker:DisplayObject = _root.invisMarkerHolder.getChildAt(i);
				if(hitTestObject(targetMarker)){
					direction *= -1;
					this.x += speed * direction;
				}
			}
			//TOUCHING THE MAIN CHARACTER
			if(this.hitTestObject(_root.mcMain)){
				//if it touches the main guy while he's jumping
				//and the guy is falling down, not jumping up
				if(_root.mainJumping && _root.jumpSpeed > 0){
					//kill this guy
					this.parent.removeChild(this);
					//and remove listners
					this.removeEventListener(Event.ENTER_FRAME, eFrame);
					//and update score
					_root.mainScore += 500;
				} else {
					//otherwise, kill the main character
					_root.resetLvl();
				}
			}
		}
	}
}
package{
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.DisplayObject;
	import flash.events.*;
	//sprites are just movieclips without any frames in them
	public class Goal extends Sprite{
		//_root will signify the root of the document
		private var _root:Object;
		
		public function Goal(){
			//this code will only be run once
			addEventListener(Event.ADDED, beginClass);
			//this code will constantly be run
			addEventListener(Event.ENTER_FRAME, eFrame);
		}
		
		private function beginClass(event:Event):void{
			//defining the root of the document
			_root = MovieClip(root);
			//making an invisible box that will help in placement
			this.graphics.beginFill(0x00FFFF);
			this.graphics.drawRect(0,0,25,25);
		}
		private function eFrame(event:Event):void{
			if(hitTestObject(_root.mcMain)){
				//advancing a level
				_root.lvlCurrent ++;
				var lastScore:int = _root.mainScore;
				_root.resetLvl();
				_root.mainScore = lastScore;
			}
		}
	}
}

Комментарии (0)

Ваш email не будет опубликован. Все поля обязательны