ActionScript - Einfaches Spiel Tutorial

From XennisWiki
Jump to: navigation, search

Ein gutes Tutorial, um ein einfache Spiel in ActionScript 3 zu programmieren, hat der YouTuber Manifest222 erstellt. Diese besteht aus vier Teilen, zum ersten Teil geht es hier: Flash AS3 Game Tutorial 1

Quellcode

Der Quellcode zum Tutorial:

import flash.events.MouseEvent;
import flash.display.MovieClip;

var goldCap:Number = 0;
var goldEarned:Number = 0;
var gold:Number = 0;
gold.toString();

var gold_mc:Gold;

var enemy1:Enemy = new Enemy();
var enemy2:Enemy = new Enemy();
var enemy3:Enemy = new Enemy();
var enemy_mc:MovieClip;

addChild(enemy1);
addChild(enemy2),
addChild(enemy3);

enemy1.x = 200;
enemy1.y = 300;
enemy2.x = 100;
enemy2.y = 100;
enemy3.x = 400;
enemy3.y = 350;

makeEnemys([enemy1, enemy2, enemy3]);

stage.addEventListener(Event.ENTER_FRAME, moneyLoop);

function moneyLoop(event:Event) {
	money_txt.text = String(gold);
	if (gold < goldCap) {
		gold +=1;
	}
}

function makeEnemys(arrayOfEnemys:Array) {
	for (var i:int = 0; i<arrayOfEnemys.length; i++) {
		arrayOfEnemys[i].buttonMode = true;
		arrayOfEnemys[i].addEventListener(MouseEvent.CLICK, attack);
	}
}

function attack(event:MouseEvent) {
	enemy_mc = MovieClip(event.currentTarget);
	removeChild(enemy_mc);
	generateMoney(Math.ceil(Math.random()*15 + 5), gold);
}

function generateMoney(amount, type) {
	if(type == gold) {
		amount.toString();
		gold_mc = new Gold();
		addChild(gold_mc);
		gold_mc.x = enemy_mc.x;
		gold_mc.y = enemy_mc.y;
		gold_mc.amount_txt.text = amount;
		gold_mc.buttonMode = true;
		gold_mc.addEventListener(MouseEvent.CLICK, addMoney);
	}
	
	function addMoney(event:MouseEvent) {
		goldEarned = amount;
		goldCap = goldEarned + gold;
		removeChild(gold_mc);
	}
}

Erweiterung

Erweiterung des ursprünglichen Tutorials um eine komplexere Struktur.

GoldEnemy.fla

import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.display.Stage;

var goldCap:Number = 0;
var goldEarned:Number = 0;
var gold:Number = 0;
gold.toString();

var gold_mc:Gold;
var enemy_mc:MovieClip;
var arrayOfEnemy = new Array();

function addEnemy(number:int) {
	for (var i:int = 0; i<number; i++) {
		var enemy:Enemy = new Enemy( stage.stageWidth, stage.stageHeight );
		enemy.addEventListener(MouseEvent.CLICK, attack);

		addChild(enemy);
		arrayOfEnemy.push(enemy);
	}
}

addEnemy(3);

stage.addEventListener(Event.ENTER_FRAME, moneyLoop);

function moneyLoop(event:Event) {
	money_txt.text = String(gold);
	if (gold < goldCap) {
		gold +=1;
	}
}

function attack(event:MouseEvent) {
	enemy_mc = MovieClip(event.currentTarget);
	removeChild(enemy_mc);
	generateMoney(Math.ceil(Math.random()*15 + 5), gold);
}

function generateMoney(amount, type) {
	if(type == gold) {
		gold_mc = new Gold(enemy_mc.x, enemy_mc.y);
		addChild(gold_mc);
		gold_mc.amount_txt.text = amount.toString();
		gold_mc.addEventListener(MouseEvent.CLICK, addMoney);
	}
	
	function addMoney(event:MouseEvent) {
		goldEarned = amount;
		goldCap = goldEarned + gold;
		removeChild(gold_mc);
	}
}

Enemy.as

package  {
	
	import flash.display.MovieClip;
	
	
	public class Enemy extends MovieClip {
		
		private var size:Number = Math.random();
		
		public function Enemy(stageWidth:int, stageHeight:int) {
			this.width = Math.ceil(width * size);
			this.height = Math.ceil(height * size);
			this.x = generateRandom(0, stageWidth - this.width);
			this.y = generateRandom(0, stageHeight - this.height);
			this.buttonMode = true;
		}
		
		private function generateRandom(from:int, to:int):int {
			return Math.ceil(Math.random() * (to - from) + from);
		}
	}
}

Gold.as

package {
	
	import flash.display.MovieClip;
	
	
	public class Gold extends MovieClip {
		
		public function Gold(_x:int, _y:int) {
			this.x = _x;
			this.y = _y;
			this.buttonMode = true;
		}
	}
}