Hi everybody. I'm new here and I attempted to make a dungeon maze game
However, my hitTestObject method does not work. For specific, the player is supposed to pick up the key when he encounters it; but nothing happens.
Anyone can please show me some guidelines here?
Here is my code, so far.
My main class
Actionscript Code:
package
{
import flash.display.MovieClip;
import flash.events.Event;
/*construct the class with an event listener that checked
if objects have been added to the stage then the handle
is triggered */
public class DungeonOne_Manager extends MovieClip
{
public function DungeonOne_Mangager()
{
//Initilize the class once the
//dungeon object is displayed on the stage
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
addEventListener(Event.ENTER_FRAME, onEnterFrame);
addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}
private function onRemovedFromStage(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
removeEventListener(Event.ENTER_FRAME, onEnterFrame1);
removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}
// All the logics of the game lie in this function
private function onEnterFrame(event:Event):void
{
//1.If player is touching the key, but
//doesnt have it, then pick it up.
if(player1.hitTestObject(doorKey1))
{
player1.addChild(doorKey1);
}
trace( player1.getBounds(stage));
trace( doorKey1.getBounds(stage));
}
}
}
My player1 class
Actionscript Code:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Player1 extends MovieClip
{
private var vx,vy : int;
private var playerHalfWidth, playerHalfHeight: uint;
private var hasKey: Boolean;
public function Player1()
{
// constructor code
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
vx = 0; vy = 0;
playerHalfWidth = width/2;
playerHalfHeight = height/2;
hasKey = false;
//Add event listener
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown1);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp1);
addEventListener(Event.ENTER_FRAME, onEnterFrame1);
addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}
private function onRemovedFromStage(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
removeEventListener(Event.ENTER_FRAME, onEnterFrame1);
removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}
private function onKeyDown1(event:KeyboardEvent)
{
if(event.keyCode == Keyboard.LEFT)
{
vx = -5;
}
if(event.keyCode == Keyboard.RIGHT)
{
vx = 5;
}
if(event.keyCode == Keyboard.UP)
{
vy = -5;
}
if(event.keyCode == Keyboard.DOWN)
{
vy = 5;
}
}
private function onKeyUp1(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.LEFT ||
event.keyCode == Keyboard.RIGHT)
{
vx = 0;
}
if(event.keyCode == Keyboard.UP ||
event.keyCode == Keyboard.DOWN)
{
vy = 0;
}
}
public function onEnterFrame1(event:Event):void
{
x += vx;
y += vy;
if (x + playerHalfWidth > 550)
{
x = 550 - playerHalfWidth;
}
else if (x - playerHalfWidth < 0)
{
x = 0 + playerHalfWidth;
}
if (y + playerHalfHeight > 400)
{
y = 400 - playerHalfHeight
}
else if (y - playerHalfHeight < 0)
{
y = 0 + playerHalfHeight;
}
}
}
}