My most unfavorite errors, error 2007 and error 1009. TypeError: Error #2007: Parameter hitTestObject must be non-null. at flash.display::DisplayObject/_hitTest() at flash.display::DisplayObject/hitTestObject() at lamagame_fla::MainTimeline/hiyeh()[lamagame_fla.MainTimeline::frame2:15] TypeError: Error #1009: Cannot access a property or method of a null object reference. at lamagame_fla::MainTimeline/fl_EnterFrameHandler_4()[lamagame_fla.MainTimeline::frame2:187]
My question is why does it show as null after I remove the event?
Scene 2:
function fl_MouseClickHandler_15(event:MouseEvent):void
{
// Start your custom code
// This example code displays the words "Mouse clicked" in the Output panel.
removeEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler_11);
removeEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler_23);
gotoAndStop(1, "Scene 1");
// End your custom code
}
vcam.gotoAndStop(1);
var i:int;
function hiyeh(event:Event):void {
if(event.currentTarget.hitTestObject(llama)) {
currentHP -= 10;
if(currentHP <= 0) //if the player died
{
currentHP = 0; //set his HP to zero, (just in case)
vcam.gotoAndStop(2);
}
updateHealthBar(); //update the healthBar
}
if(vcam.currentFrame == 2) {
event.currentTarget.removeEventListener(Event.ENTER_FRAME, hiyeh);
}
}
var ticksSinceLastShot:int;
var j:int;
addEventListener(Event.ENTER_FRAME, enemyhit);
function enemyhit(event:Event):void {
for (i = enemy_array.length -1; i >= 0; i--){
for(var j = 0; j < bulletarray.length; j++) {
if(!bulletarray[j].hitTestObject(vcam)) {
(bulletarray[j] as MovieClip).parent.removeChild(bulletarray[j]);
bulletarray.splice(j, 1)
continue;
}
if(bulletarray[j].hitTestObject(enemy_array[i])) {
enemy_array[i].currentHPtwo-=10;
(bulletarray[j] as MovieClip).parent.removeChild(bulletarray[j]);
bulletarray.splice(j, 1);
if(enemy_array[i].currentHPtwo <= 0) //if the player died
{
enemy_array[i].currentHPtwo = 0; //set his HP to zero, (just in case)
(enemy_array[i] as MovieClip).parent.removeChild(enemy_array[i]);
}
continue;
}
}
}
}
vcam.pressme.addEventListener(MouseEvent.MOUSE_DOWN, onDown)
stage.addEventListener(MouseEvent.MOUSE_UP, onUp)
var pressedState:int = 0;
var myTimer:Timer = new Timer(10);
myTimer.addEventListener(TimerEvent.TIMER, onTimerTick);
function onDown(e:MouseEvent)
{
myTimer.start();
switch(e.currentTarget)
{
case vcam.pressme:
pressedState = 1;
break;
}
}
function onUp(e:MouseEvent)
{
myTimer.stop();
pressedState = 0
}
function onTimerTick(e:TimerEvent)
{
ticksSinceLastShot = ticksSinceLastShot + 1;
switch(pressedState)
{
case 1:
// Create a new bullet
var b:Bullet = new Bullet();
// Set his position to the tank position
if(llama.scaleX==1) {
b.x = llama.x+20;
b.y = llama.y-50;
}else{
if(llama.scaleX==-1) {
b.x = llama.x-25;
b.y = llama.y-50;
}else{
b.x = llama.x+20;
b.y = llama.y-50;
}
}
// Save the randian angle between the mouse and the tank
// This angle will set the direction of the bullet
b.angleRadian = Math.atan2(mouseY - llama.y,mouseX - llama.x);
// Add an enter frame event on each bullet
b.addEventListener(Event.ENTER_FRAME, bulletEnterFrame);
// Add this display object on the display list
if ( ticksSinceLastShot >= 10 )
{
addChild(b);
bulletarray.push(b);
ticksSinceLastShot = 0;
break;
}
return b;
}
}
import flash.display.MovieClip;
import flash.geom.Point;
import flash.events.MouseEvent;
// Code by Benoit Freslon.
// Tutorials, Flash games:
// http://www.benoitfreslon.com
// This object will always look at the mouse cursor
llama.head.addEventListener(Event.ENTER_FRAME, llamaenterframe);
// This function will be launched every frame (25 times by seconds);
function llamaenterframe(pEvt) {
// pEvt.currentTarget: myTank
var b = pEvt.currentTarget;
// Get the radian angle between the tank and the cursor
// You can also replace mouseX and mouseY by another coordinates
// Convert the radian angle in dedree angle
var angleDegree = b.angleRadian * 90 / Math.PI;
// Set the orientation
b.rotation = angleDegree;
// Display angle of rotation in degree
}
// Velocity of each bullet
var speed = 8;
function bulletEnterFrame(pEvent) {
// Get the current object (Bullet)
var b = pEvent.currentTarget;
// Move this bullet on each frames
// On X axis use the cosinus angle
b.x += Math.cos(b.angleRadian) * speed;
// On Y axis use the sinus angle
b.y += Math.sin(b.angleRadian) * speed;
// Orient the bullet to the direction
b.rotation = b.angleRadian * 180 / Math.PI;
// You have to remove each created bullet
// So after every moves you must check bullet position
// If the bullet is out of the screen
}
// Set his position to the tank position
var maxHP:int = 400;
var currentHP:int = maxHP;
var percentHP:Number = currentHP / maxHP;
updateHealthBar();
function updateHealthBar():void
{
percentHP = currentHP / maxHP;
llama.healthBar.barColor.scaleX = percentHP;
}
// properties in class ----------
var enemy_array:Array = new Array;
// dynamically place MovieClip instances on the stage ----------
// inside Constructor function of class
var enemy_number:int=100;//number of circles on the stage
for(i=0; i<enemy_number; i++) {
enemy_array[i] = new enemy(); //linkage in the library
enemy_array[i].x = MovieClip(root).background1tiles.x + Math.floor( Math.random()* MovieClip(root).background1tiles.width) ;
enemy_array[i].y = MovieClip(root).background1tiles.y + Math.floor( Math.random()* MovieClip(root).background1tiles.height) ;
enemy_array[i].addEventListener(Event.ENTER_FRAME, hiyeh);
addChild(enemy_array[i]);
enemy_array[i].addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler_4);
function fl_EnterFrameHandler_4(event:Event):void
{
if(vcam.currentFrame == 2) {
event.currentTarget.removeEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler_4);
}
if(event.currentTarget.hitTestObject(vcam)) {
if(event.currentTarget.x < Math.floor(llama.x)) {
event.currentTarget.x+=event.currentTarget.randomspeed;
}else{
event.currentTarget.x-=event.currentTarget.randomspeed;
}
if(event.currentTarget.y < Math.floor(llama.y)) {
event.currentTarget.y+=event.currentTarget.randomspeed;
}else{
event.currentTarget.y-=event.currentTarget.randomspeed;
}
}
}
}
/* Enter Frame Event
Executes the function fl_EnterFrameHandler_6 defined below each time the playhead moves into a new frame of the timeline.
Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when the playhead moves into a new timeline frame.
*/
addEventListener(Event.ENTER_FRAME, basics);
function basics(event:Event):void {
//Start your custom code
// This example code displays the words "Entered frame" in the Output panel.
if(llama) {
if(llama.x > 3005.6) {
llama.x = 3005.6;
}
if(llama.x < -2369) {
llama.x = -2369;
}
if(llama.y > 0) {
llama.y=0;
}
vcam.x=llama.x;
vcam.y=llama.y;
vcam.coordinates.text="Llama's X is "+llama.x+". And Llama's Y is "+llama.y;
// End your custom code
}
}
/* Move with Keyboard Arrows
Allows the specified symbol instance to be moved with the keyboard arrows.
Instructions:
1. To increase or decrease the amount of movement, replace the number 5 below with the number of pixels you want the symbol instance to move with each key press.
Note the number 5 appears four times in the code below.
*/
var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
stage.addEventListener(Event.ENTER_FRAME, dothelistener);
function dothelistener(event:Event) {
if(vcam.currentFrame==2) {
stage.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_15);
stage.removeEventListener(Event.ENTER_FRAME, dothelistener);
}
}
llama.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
function fl_MoveInDirectionOfKey(event:Event)
{
if (upPressed)
{
llama.y-=5;
}
if (downPressed)
{
llama.y+=5;
}
if (leftPressed)
{
llama.scaleX=-1;
llama.x-=5;
}
if (rightPressed)
{
llama.scaleX=1;
llama.x+=5;
}
}
function fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = true;
break;
}
case Keyboard.DOWN:
{
downPressed = true;
break;
}
case Keyboard.LEFT:
{
leftPressed = true;
break;
}
case Keyboard.RIGHT:
{
rightPressed = true;
break;
}
}
}
function fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
upPressed = false;
break;
}
case Keyboard.DOWN:
{
downPressed = false;
break;
}
case Keyboard.LEFT:
{
leftPressed = false;
break;
}
case Keyboard.RIGHT:
{
rightPressed = false;
break;
}
}
}
// Velocity of each llama
/* Mouse Click Event
Clicking on the specified symbol instance executes a function in which you can add your own custom code.
Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when the symbol instance is clicked.
*/
vcam.magnify.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_10);
function fl_MouseClickHandler_10(event:MouseEvent):void
{
// Start your custom code
// This example code displays the words "Mouse clicked" in the Output panel.
vcam.width-=25;
vcam.height-=20;
// End your custom code
}
/* Mouse Click Event
Clicking on the specified symbol instance executes a function in which you can add your own custom code.
Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when the symbol instance is clicked.
*/
vcam.magnifyun.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_11);
function fl_MouseClickHandler_11(event:MouseEvent):void
{
// Start your custom code
// This example code displays the words "Mouse clicked" in the Output panel.
vcam.width+=25;
vcam.height+=20;
// End your custom code
}
var bulletarray:Array = new Array;
/* Enter Frame Event
Executes the function fl_EnterFrameHandler_10 defined below each time the playhead moves into a new frame of the timeline.
Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when the playhead moves into a new timeline frame.
*/
/* Enter Frame Event
Executes the function fl_EnterFrameHandler_11 defined below each time the playhead moves into a new frame of the timeline.
Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when the playhead moves into a new timeline frame.
*/
addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler_11);
function fl_EnterFrameHandler_11(event:Event):void
{
//Start your custom code
// This example code displays the words "Entered frame" in the Output panel.
vcam.count.text = bulletarray.length;
// End your custom code
}
/* Enter Frame Event
Executes the function fl_EnterFrameHandler_23 defined below each time the playhead moves into a new frame of the timeline.
Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when the playhead moves into a new timeline frame.
*/
addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler_23);
function fl_EnterFrameHandler_23(event:Event):void
{
//Start your custom code
// This example code displays the words "Entered frame" in the Output panel.
if(!llama.hitTestObject(background1tiles)) {
llama.y = llama.y + 5;
}
// End your custom code
}
Scene 1:
stop();
/* Mouse Click Event
Clicking on the specified symbol instance executes a function in which you can add your own custom code.
Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when the symbol instance is clicked.
*/
movieClip_1.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {
removeEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler_25);
removeEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler_24);
gotoAndStop(1, "Scene 2");
});
/* Enter Frame Event
Executes the function fl_EnterFrameHandler_24 defined below each time the playhead moves into a new frame of the timeline.
Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when the playhead moves into a new timeline frame.
*/
addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler_24);
function fl_EnterFrameHandler_24(event:Event):void
{
//Start your custom code
// This example code displays the words "Entered frame" in the Output panel.
vcam2.x = llama1.x;
vcam2.y = llama1.y;
// End your custom code
}
/* Enter Frame Event
Executes the function fl_EnterFrameHandler_25 defined below each time the playhead moves into a new frame of the timeline.
Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when the playhead moves into a new timeline frame.
*/
addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler_25);
function fl_EnterFrameHandler_25(event:Event):void
{
//Start your custom code
// This example code displays the words "Entered frame" in the Output panel.
if(!llama1.hitTestObject(tiles)) {
tiles.width += 5;
tiles.height += 5;
}
// End your custom code
}
Please help I can't seem to find what's wrong with my code
EDIT I removed all events, not just some, and it worked. It seems you have to remove every event and not just some.
I still have this question: why does it show as null even after you remove the event?
EDIT I still seem to be getting a null even though I removed the event help is appreciated
null. The errors are telling you exactly which objects arenulland which line of your code is causing the error. This site is not for pasting your code and having people debug it. Removing all the events is one way to fix it, since that removes the functionality. Having an empty program is a great way to have no errors, but it doesn't make a very good program. See the FAQ to learn what types of questions to ask here. – Byte56 Jan 20 at 18:44null. Start with something simpler if this is unclear to you. – Byte56 Jan 20 at 20:08