It's a basic interaction where you drag towers from a list to the map. After placed it's fixed on that spot, can't drag it to another spot. If you cant to change position, you must sell it and then place a new one on the desired spot.
When clicking a placed tower you get 2 options, to upgrade or to sell. Upgrading is disabled as there is no actual game in place, this is just a user interaction proof of concept. Selling works so you can place and remove towers as you see fit.
If you want to clear all towers you can click the "Clear" button on the bottom right corner.
If you want to use the code below, be sure to download the fla for it by clicking here. This is the source code that makes this work. I've left empty lines for readability, and I wrote comments, if theres something that doens't make sense just let me know in the comments and I'll expand on it.
Edit: I added upgrading for the towers up to level 3.
package {
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.*;
/**
* @package
* @author Newen
* @copyright February, 2014 Under Base
*/
public class Prototype_0 extends Sprite {
public var point0:MovieClip; // MC positioned at top right left of the map
public var t1:MovieClip; // towers 1 button
public var t2:MovieClip; // towers 2 button
public var t3:MovieClip; // towers 3 button
public var t4:MovieClip; // towers 4 button
public var tile:MapTile; // tile MC class
public var newTower:MovieClip; // new tower being created
public var mcClear:MovieClip; // clear map button
public var mcOptions:MovieClip; // tower options MC
private var initX:int = 0; // top left corner of the map x value
private var initY:int = 0; // top left corner of the map y value
private var mapWidth:int = 15; // number of collumns on the map
private var mapHeight:int = 10; // number of rows in the map
private var aTiles:Array; // array to store all map tiles
private var aTowers:Array; // array to store all created towers
private var sID:int = 0; // int variable to assign an id to all tiles
private var selectedTower:MovieClip; // to reference clicked tower when opening options
public function Prototype_0() {
// set the top left corner of the map to the position of point0 MovieClip
initX = point0.x;
initY = point0.y;
removeChild(point0);
selectedTower = null;
// hide tower options MC
mcOptions.visible = false;
// assign tower types to each tower
t1.towerType = "Tower1";
t2.towerType = "Tower2";
t3.towerType = "Tower3";
t4.towerType = "Tower4";
// create arrays for tiles and towers
aTiles = new Array();
aTowers = new Array();
// create map depending on mapWidth and mapHeight
for( var i = 0; i < mapWidth ; i++ ) {
for (var k:int = 0; k < mapHeight; k++) {
tile = new MapTile();
tile.x = initX + i*tile.width;
tile.y = initY + k*tile.height;
addChild(tile);
aTiles.push(tile);
tile.id = sID;
sID++;
}
}
addEventListeners();
}
// add initial event listeners
public function addEventListeners():void {
t1.addEventListener(MouseEvent.MOUSE_DOWN, addTower);
t2.addEventListener(MouseEvent.MOUSE_DOWN, addTower);
t3.addEventListener(MouseEvent.MOUSE_DOWN, addTower);
t4.addEventListener(MouseEvent.MOUSE_DOWN, addTower);
mcClear.addEventListener(MouseEvent.MOUSE_DOWN, clearMap);
stage.addEventListener(MouseEvent.MOUSE_DOWN, tappedOutside);
mcOptions.mcSell.addEventListener(MouseEvent.MOUSE_UP, sellTower);
mcOptions.mcSell.addEventListener(MouseEvent.MOUSE_UP, upgradeTower);
}
public function addTower(eve:MouseEvent):void {
// check towerType variable from clicked button
var towerType = eve.currentTarget.towerType;
// make instance of new tower
switch(towerType) {
case "Tower1":
newTower = new Tower1();
break;
case "Tower2":
newTower = new Tower2();
break;
case "Tower3":
newTower = new Tower3();
break;
case "Tower4":
newTower = new Tower4();
break;
}
addChild(newTower);
// position new tower at mouse location and start dragging it
newTower.x = mouseX - newTower.width/2;
newTower.y = mouseY - newTower.height/2;
newTower.startDrag();
// insert new tower into towers array
aTowers.push(newTower);
newTower.addEventListener(MouseEvent.MOUSE_UP, placeTower);
}
public function placeTower(eve:MouseEvent):void {
// stop dragging tower
newTower.stopDrag();
positionTower(newTower);
newTower.removeEventListener(MouseEvent.MOUSE_UP, placeTower);
newTower.addEventListener(MouseEvent.MOUSE_UP, showTowerOptions);
}
public function showTowerOptions(eve:MouseEvent):void {
// show options for this tower
selectedTower = eve.currentTarget as MovieClip;
mcOptions.x = selectedTower.x;
mcOptions.y = selectedTower.y;
addChild(mcOptions);
mcOptions.visible = true;
}
public function positionTower(tower:MovieClip):void {
// check for position of closest tile, make it the new tower position
var tintx:int = 0;
var tinty:int = 0;
var wintx:int = 0;
var winty:int = 0;
// check to see if tower is inside the map
if (tower.x < initX - aTiles[0].width/2 || tower.x > initX + (mapWidth*aTiles[0].width) || tower.y < initY - aTiles[0].height/2 || tower.y > initY + (mapHeight*aTiles[0].height)) {
// Tower not inside map. remove it
tower.x = -100;
tower.y = -100;
aTowers.pop();
removeChild(tower);
return;
}
// search for closest positioned tile
for (var i:int = 0; i < aTiles.length; i++ ) {
// check half width and height of tiles
tintx = aTiles[i].width/2;
tinty = aTiles[i].height/2;
// check difference in position between tower and tile
wintx = aTiles[i].x - tower.x;
winty = aTiles[i].y - tower.y;
// make difference postive number
wintx = Math.sqrt(wintx*wintx);
winty = Math.sqrt(winty*winty);
// check if difference less than half the tiles width and height. thus making it the closest
if (winty <= tinty && wintx <= tintx) {
// found closest tile
tower.x = aTiles[i].x + 5;
tower.y = aTiles[i].y + 5;
break;
} else {
//i = i + mapWidth;
}
}
// check if there are previous towers placed in the map
if (aTowers.length > 1) {
// check for collision between new tower and previous towers
for (var k:int = 0; k < aTowers.length - 1; k++ ) {
if (aTowers[k].hitTestObject(tower)) {
// tower colliding with a previous tower, remove it.
aTowers.pop();
removeChild(tower);
//trace("New tower colliding with previous tower. remove it");
return;
}
}
}
}
// Clear all towers from map
public function clearMap(eve:MouseEvent):void {
// remove all towers from map
for (var i:int = 0; i < aTowers.length; i ) {
removeChild(aTowers[i]);
aTowers.splice(i,1);
}
}
public function tappedOutside(eve:MouseEvent):void {
if (eve.target == stage) {
// hide options
if (selectedTower != null) {
selectedTower = null;
mcOptions.visible = false;
}
}
}
public function upgradeTower(eve:MouseEvent):void {
// here we would upgrade the tower, but this is just a proof of concept so nothing will actually happen.
selectedTower = null;
mcOptions.visible = false;
}
public function sellTower(eve:MouseEvent):void {
// remove the tower, and we would gain some currency back, but we don't have that here.
trace("tower:",eve.target);
for (var i:int = 0; i < aTowers.length; i++ ) {
if (selectedTower == aTowers[i]) {
// trace("Is the same",i);
removeChild(aTowers[i]);
aTowers.splice(i,1);
}
}
selectedTower = null;
mcOptions.visible = false;
}
}
}
No comments:
Post a Comment