Can't get hit detection through classes working
HTML Code:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
public class TreeTile extends MovieClip {
private var rootMC: MovieClip;
public function EmptyBlock() {
addEventListener(Event.ADDED, beginClass);
addEventListener(Event.ENTER_FRAME, render);
}
private function beginClass(e: Event): void {
this.buttonMode = true;
}
private function render(e: Event): void {
rootMC = MovieClip(root);
if (rootMC.gameOver) {
removeEventListener(Event.ENTER_FRAME, render)
this.parent.removeChild(this);
if (rootMC.player.hitTestObject(rootMC.treeBlock)) {
rootMC.player.x = 100;
}
}
}
//Always at the end
}
}
HTML Code:
import flash.events.Event;
var player: Player = new Player();
var mapArray: Array = new Array();
var treeBlock;
mapArray = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2,
2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,
2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 2,
2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2,
2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3,
]
function makeRoad() {
var row: int = 0;
var block;
for (var i: int = 0; i < mapArray.length; i++) {
if (mapArray[i] == 1) {
block = new Block();
addChild(block)
block.x = (i - row * 12) * 46;
block.y = row * 40;
}
if (mapArray[i] == 2) {
treeBlock = new TreeTile()
addChild(treeBlock)
treeBlock.x = (i - row * 12) * 46;
treeBlock.y = row * 40;
}
if (mapArray[i] == 3) {
addChild(player)
player.x = 180;
player.y = 60;
}
for (var c: int = 1; c <= 12; c++) {
if (i == c * 12 - 1) {
row++;
}
}
}
}
makeRoad();
var houseTile: House = new House();
addChild(houseTile)
houseTile.x = 196
houseTile.y = 80
stage.addEventListener(Event.ENTER_FRAME, render)
stage.addEventListener(KeyboardEvent.KEY_UP, KeyboardUnpressed)
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyboardPressed)
var leftPressed:Boolean = false
var rightPressed:Boolean = false
var upPressed:Boolean = false
var downPressed:Boolean = false
var boxSpeed:int = 4
function KeyboardUnpressed (e:KeyboardEvent):void{
if (e.keyCode == Keyboard.UP)
{
upPressed = false;
}
if (e.keyCode == Keyboard.DOWN)
{
downPressed = false;
}
if (e.keyCode == Keyboard.LEFT)
{
leftPressed = false;
}
if (e.keyCode == Keyboard.RIGHT)
{
rightPressed = false;
}
}
function KeyboardPressed (e:KeyboardEvent):void{
if(e.keyCode == Keyboard.UP){
upPressed = true;
}
if(e.keyCode == Keyboard.DOWN){
downPressed = true
}
if(e.keyCode == Keyboard.RIGHT){
rightPressed = true
}
if(e.keyCode == Keyboard.LEFT){
leftPressed = true
}
}
function render(e:Event):void{
if(rightPressed){
player.x += boxSpeed
}
if(leftPressed){
player.x -= boxSpeed
}
if(upPressed){
player.y -= boxSpeed
}
if(downPressed){
player.y += boxSpeed
}
}