Actionscript3 shooter game error 1009 and 2009 - actionscript-3

Good day everyone! I'm Jeremy, a college student. I'm working on a shooter game that has story and up to 3 levels (this is our finals that is due in 2 days). I know that I must know this stuff bcoz of school, but honestly I dont. And I'm really not good with actionscript 3 so I really need ur help guys. I know that this problem has been all over the net. But I still don't know how to solve the Error Code 1009 and 2007..
So this is my code for frame4:
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.events.Event;
stop();
var container:MovieClip = new MovieClip();
addChild(container);
//move the spaceship using our mouse
function controlSpaceShip(event:Event):void
{
shipMC.x = stage.mouseX;
Mouse.hide();
}
stop();
var score:int = 0;
stage.addEventListener(Event.ENTER_FRAME, controlSpaceShip);
//shoot bullets when we left click on the mouse
function shootBullet(event:MouseEvent):void
{
var thebullet:MovieClip = new bullet(); //calls the bullet symbol from the library
//positions the bullet at the position of your ship
thebullet.x = shipMC.x + 30;
thebullet.y = shipMC.y + 20;
//we add the bullet onto the stage
container.addChild(thebullet);
}
stage.addEventListener(MouseEvent.CLICK, shootBullet);
//when bullet hits the enemy1
function hitEnemy(event:Event):void {
if (container.hitTestObject(enemy1)) {
enemy1.alpha = 0;
score = score + 5;
txtScore.text = (score).toString();
}
}
stage.addEventListener(Event.ENTER_FRAME, hitEnemy);
//when bullet hits the enemy2
function hitEnemy2(event:Event):void {
if (container.hitTestObject(enemy2)) {
enemy2.alpha = 0;
score = score + 5;
txtScore.text = (score).toString();
}
}
stage.addEventListener(Event.ENTER_FRAME, hitEnemy2);
//when bullet hits the enemy3
function hitEnemy3(event:Event):void {
if (container.hitTestObject(enemy3)) {
enemy3.alpha = 0;
score = score + 5;
txtScore.text = (score).toString();
}
}
stage.addEventListener(Event.ENTER_FRAME, hitEnemy3);
//when bullet hits the enemy4
function hitEnemy4(event:Event):void {
if (container.hitTestObject(enemy4)) {
enemy4.alpha = 0;
score = score + 5;
txtScore.text = (score).toString();
}
}
stage.addEventListener(Event.ENTER_FRAME, hitEnemy4);
//when bullet hits the enemy5
function hitEnemy5(event:Event):void {
if (container.hitTestObject(enemy5)) {
enemy5.alpha = 0;
score = score + 5;
txtScore.text = (score).toString();
}
}
stage.addEventListener(Event.ENTER_FRAME, hitEnemy5);
//when bullet hits the enemy6
function hitEnemy6(event:Event):void {
if (container.hitTestObject(enemy6)) {
enemy6.alpha = 0;
score = score + 5;
txtScore.text = (score).toString();
}
if (txtScore.text == (150).toString())
{
gotoAndStop(5);
}
}
stage.addEventListener(Event.ENTER_FRAME, hitEnemy6);
//move the enemies vertically
function moveEnemies(event:Event):void {
//ENEMY 1
enemy1.y += 8; //increment the y location of my enemy 6 pixels
if (enemy1.y > 600) { //if my enemy is outside the stage
enemy1.y = 0; //place it back on the stage
enemy1.x = Math.random() * 550; //randomize its horizontal location
}
//ENEMY 2
enemy2.y += 4; //increment the y location of my enemy 3 pixels
if (enemy2.y > 600) { //if my enemy is outside the stage
enemy2.y = 0; //place it back on the stage
enemy2.x = Math.random() * 550; //randomize its horizontal location
}
//ENEMY 3
enemy3.y += 3; //increment the y location of my enemy 3 pixels
if (enemy3.y > 600) { //if my enemy is outside the stage
enemy3.y = 0; //place it back on the stage
enemy3.x = Math.random() * 550; //randomize its horizontal location
}
//ENEMY 4
enemy4.y += 4; //increment the y location of my enemy 3 pixels
if (enemy4.y > 600) { //if my enemy is outside the stage
enemy4.y = 0; //place it back on the stage
enemy4.x = Math.random() * 550; //randomize its horizontal location
}
//ENEMY 5
enemy5.y += 4; //increment the y location of my enemy 3 pixels
if (enemy5.y > 600) { //if my enemy is outside the stage
enemy5.y = 0; //place it back on the stage
enemy5.x = Math.random() * 550; //randomize its horizontal location
}
//ENEMY 6
enemy6.y += 4; //increment the y location of my enemy 3 pixels
if (enemy6.y > 600) { //if my enemy is outside the stage
enemy6.y = 0; //place it back on the stage
enemy6.x = Math.random() * 550; //randomize its horizontal location
}
}
stage.addEventListener(Event.ENTER_FRAME, moveEnemies);
so after killing all 6 enemies and scoring at least 150, I want the frame to jump to frame5(success) while frame 5 has the button to click going to frame6(story and goes to level2)..
and here's the error that keeps on going and going..
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at finals_interactivegame2_fla::MainTimeline/moveEnemies()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at finals_interactivegame2_fla::MainTimeline/controlSpaceShip()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy2()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy3()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy4()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy5()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy6()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at finals_interactivegame2_fla::MainTimeline/moveEnemies()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at finals_interactivegame2_fla::MainTimeline/controlSpaceShip()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy2()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy3()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy4()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy5()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy6()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at finals_interactivegame2_fla::MainTimeline/moveEnemies()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at finals_interactivegame2_fla::MainTimeline/controlSpaceShip()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy2()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at finals_interactivegame2_fla::MainTimeline/hitEnemy3()
I hope somebody help me because I don't know what to do anymore. I don't have people to help me. My classmates are not approachable and friendly. and this will be submitted in 2 days. So I don't want to fail. I just hope someone.. someone to help me with my game.
You can check my file at Actionscript3 shooter game
Thank you.

Your problem is that your adding all these ENTER_FRAME listeners, but then not removing them before you go to the next frame. So they keep running forever, and they try to reference all your enemy's and container even though those things got unloaded when you changed frames.
To resolve this issue, you need to remove ALL those listeners before you move on to frame 5.
I'd recommend creating a function called checkGameComplete or something like that:
function checkGameComplete():void {
if (txtScore.text == (150).toString()){
stage.removeEventListener(Event.ENTER_FRAME, hitEnemy6); //repeat for ALL enter frame listeners
//rest of the removeEventListeners
gotoAndStop(5);
}
}
Then call that checkGameComplete() on every hitEnemy check.
As an aside, make your code less redundant by making ONE hit enemy method, ONE move method, and ONE enter frame listener:
stop();
var score:int = 0;
var container:MovieClip = new MovieClip();
addChild(container);
stage.addEventListener(Event.ENTER_FRAME, gameLoop); //just the one ENTER FRAME handler
Mouse.hide(); //you only need to call this once, instead of every bullet fire
stage.addEventListener(MouseEvent.CLICK, shootBullet);
function shootBullet(event:MouseEvent):void
{
var thebullet:MovieClip = new bullet(); //calls the bullet symbol from the library
//positions the bullet at the position of your ship
thebullet.x = shipMC.x + 30;
thebullet.y = shipMC.y + 20;
//we add the bullet onto the stage
container.addChild(thebullet);
}
function gameLoop(event:Event):void {
if(!shipMC) return;
shipMC.x = stage.mouseX;
moveEnemy(enemy1, 8);
moveEnemy(enemy2, 4);
moveEnemy(enemy3, 3);
moveEnemy(enemy4, 4);
moveEnemy(enemy5, 4);
moveEnemy(enemy6, 4);
}
function hitEnemyCheck(enemy):void {
if (container.hitTestObject(enemy)) {
//enemy.visible = false; //instead of hiding it, move it back up
enemy.y = -100;
score = score + 5;
txtScore.text = (score).toString();
checkGameComplete();
}
}
function moveEnemy(enemy, amount):void {
enemy.y += amount; //increment the y location of my enemy
if (enemy.y > 600) { //if my enemy is outside the stage
enemy.y = 0; //place it back on the stage
enemy.x = Math.random() * 550; //randomize its horizontal location
}
hitEnemyCheck(enemy);
}
function checkGameComplete():void {
if (txtScore.text == (150).toString()){
stage.removeEventListener(Event.ENTER_FRAME, gameLoop);
stage.removeEventListener(MouseEvent.CLICK, shootBullet); //clean up listeners
removeChild(container); //get rid of this container you created
Mouse.show(); //show the mouse again
gotoAndStop(5);
}
}
I also noticed, that on your frame 5 code (and frame 6 code too), you're calling the wrong function on the next3_btn click:
next3_btn.addEventListener(MouseEvent.CLICK,next2Click); // <-- your calling next2Click instead of next3Click
Should be:
next3_btn.addEventListener(MouseEvent.CLICK,next3Click);

In your hitEnemy6 function and before gotoAndStop(5), you should remove all event listeners that you have already added, to goto to the 5th frame :
stage.removeEventListener(Event.ENTER_FRAME, hitEnemy)
stage.removeEventListener(Event.ENTER_FRAME, hitEnemy2)
stage.removeEventListener(Event.ENTER_FRAME, hitEnemy3)
stage.removeEventListener(Event.ENTER_FRAME, hitEnemy4)
stage.removeEventListener(Event.ENTER_FRAME, hitEnemy5)
stage.removeEventListener(Event.ENTER_FRAME, hitEnemy6)
stage.removeEventListener(Event.ENTER_FRAME, moveEnemies)
stage.removeEventListener(Event.ENTER_FRAME, controlSpaceShip)
stage.removeEventListener(MouseEvent.CLICK, shootBullet)
gotoAndStop(5)
In your moveEnemies function, you should verify every time if your enemy object is not null using a simple if as :
if (enemy1) {
enemy1.y += 8
if (enemy1.y > 600){
enemy1.y = 0
enemy1.x = Math.random() * 550
}
}
You should do that for all your enemy objects (1 -> 6).
I don't know why you created 6 Event.ENTER_FRAME, one for every enemy object !? you can simply do :
stage.addEventListener(Event.ENTER_FRAME, hitEnemy)
function hitEnemy(event:Event):void {
if (container.hitTestObject(enemy1)) {
enemy1.alpha = 0
score = score + 5
txtScore.text = (score).toString()
}
if (container.hitTestObject(enemy2)) {
enemy2.alpha = 0
score = score + 5
txtScore.text = (score).toString()
}
// do the same thing for all your enemy objects ...
}
...

Related

Error when catching objects and lives count wont decrease?

I'm currently making a catching game. So the user have to catch the right letter to get a score and go to the next question.
The letters are movieclips and I made the falling animation with motion tween.
I converted the avatar to a movieclip too.
When the avatar hits the right letter, the user will get 100 points and when the avatar hits the wrong letter, there will be a pop up button that says "try again" and it will lost its lives by 1.
Here's my code:
stop();
import flash.events.KeyboardEvent;
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveavatar);
avatarmove.addEventListener(Event.ENTER_FRAME, handleCollision);
trybutton.visible = false;
var myscore = 0;
var mylives = 5;
score.text = myscore+"";
lives.text = mylives+"";
//Move avatar function
function moveavatar (event:KeyboardEvent):void
{
avatarmove.x = Math.max(0,Math.min(stage.stageWidth-avatarmove.width,avatarmove.x - int(event.keyCode==37) * 10 + int(event.keyCode==39) * 10));
}
//Collision between the avatar and the letters
function handleCollision(e:Event):void
{
if(avatarmove.hitTestObject(a_alphabet))
{
myscore+=100;
a_alphabet.visible = false;
gotoAndStop(2);
}
else if(avatarmove.hitTestObject(o_alphabet))
{
mylives-=1;
o_alphabet.visible = false;
trybutton.visible = true;
}
}
//try again button
trybutton.addEventListener (MouseEvent.CLICK, ayo1);
function ayo1(event:MouseEvent):void{
o_alphabet.visible = true;
trybutton.visible = false;
}
I keep getting this error when the avatar hits the letter:
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at gamecompletethewords_fla::MainTimeline/handleCollision()
And this error on the next frame:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at gamecompletethewords_fla::MainTimeline/handleCollision2()
And the the lives won't decrease when the avatar hits the wrong letter, but when I move to the next frame it will decrease by -27. It's just really weird.
Can someone help me with these problems? I'm totally new to this and I have no idea how to fix it :(

Error 1009 Actionscript Flash CC

So I have been trying to figure out this error for the past two hours, I've tried researching the entire internet for an answer and so far I haven't been able to figure it out.
Would someone be able to help me! This is my code :
stop();
countdown_mc.visible = false;
stage.focus = stage;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.sensors.Accelerometer;
import flash.events.AccelerometerEvent;
var myAccel: Accelerometer = new Accelerometer();
var score: Number = 0;
var countDown: Timer = new Timer(1000);
stage.addEventListener(Event.ENTER_FRAME, checkHit);
myAccel.setRequestedUpdateInterval(100); //Every half second.
countDown.addEventListener(TimerEvent.TIMER, count);
countDown.start();
if (Accelerometer.isSupported == true) {
myAccel.addEventListener(AccelerometerEvent.UPDATE, update);
function update(e: AccelerometerEvent) {
x_mc.x -= (e.accelerationX * 30);
o_mc.x -= (e.accelerationX * 30);
if (x_mc.x < 150) {
x_mc.x = 150;
}
if (x_mc.x >= stage.stageWidth - 150) {
x_mc.x = stage.stageWidth - 150;
}
if (o_mc.x < 150) {
o_mc.x = 150;
}
if (o_mc.x >= stage.stageWidth - 150) {
o_mc.x = stage.stageWidth - 150;
}
}
}
function count(eeee: TimerEvent) {
var drop: Number = Math.floor(Math.random() * 5) + 1;
if (drop == 1) {
x_mc.gotoAndPlay(2);
countDown.stop();
gotoAndPlay(180);
o_mc.stop();
stage.removeEventListener(Event.ENTER_FRAME, checkHit);
stage.removeEventListener(AccelerometerEvent.UPDATE, update);
stage.removeEventListener(TimerEvent.TIMER, count);
} else if (drop == 2) {
o_mc.gotoAndPlay(2);
countDown.stop();
gotoAndPlay(180);
x_mc.stop();
stage.removeEventListener(Event.ENTER_FRAME, checkHit);
stage.removeEventListener(AccelerometerEvent.UPDATE, update);
stage.removeEventListener(TimerEvent.TIMER, count);
} else if (drop) {
x_mc.stop();
o_mc.stop();
} else if (x_mc.hitTestObject(exoHit_mc)) {
score++;
}
}
function checkHit(eeee: Event) {
if (x_mc.hitTestObject(exeHit_mc)) {
gotoAndStop(257);
stage.removeEventListener(Event.ENTER_FRAME, checkHit);
stage.removeEventListener(AccelerometerEvent.UPDATE, update);
stage.removeEventListener(TimerEvent.TIMER, count);
}
}
And this is the error :
[SWF] flash%20gmae.swf - 2013164 bytes after decompression
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at flashgmae_fla::MainTimeline/count()[flashgmae_fla.MainTimeline::frame179:91]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at flashgmae_fla::MainTimeline/count()[flashgmae_fla.MainTimeline::frame179:91]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at flashgmae_fla::MainTimeline/count()[flashgmae_fla.MainTimeline::frame179:79]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at flashgmae_fla::MainTimeline/count()[flashgmae_fla.MainTimeline::frame179:91]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at flashgmae_fla::MainTimeline/count()[flashgmae_fla.MainTimeline::frame179:79]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at flashgmae_fla::MainTimeline/count()[flashgmae_fla.MainTimeline::frame179:67]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
[UnloadSWF] flash%20gmae.swf
Test Movie terminated.
I have figured out that is has something to do with the x_mc and o_mc movie clips but I have no idea what the issue is as I am new to action script.
Thank you so much!
x_mc and o_mc are not present on the frames, or don't have an instance name set. Verify that both objects have the correct instance name set. After doing that, verify this for exoHit_mc and exeHit_mc as well.

AS3 removing event listener before child still causing null object reference

I made a game of Pong with external classes, it's currently configured so that when the playerScore/cpuScore == 1, the player, CPU and ball is removed from the stage. These are removed by the main.as file calling removePlayer(), removeBall() and removeCPU() e.g.
public function removePlayer(): void
{
removeEventListener(Event.ENTER_FRAME, loop);
parent.removeChild(this);
}
The event listener gets rid of the main loop for the element before the child is removed from stage. This works fine for both the player and CPU, but for the ball I'm getting the following error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.classes::ball/loop()
Commenting out parent.removeChild(this); gets rid of the error, but obviously the ball stays visible. removeEventListener seems to have worked as the ball stops moving when called, but I'm at a loss as to why I would get the null object reference if the loop has been removed. Here is the code for my ball:
package com.classes
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
public class ball extends MovieClip
{
private var theBackground:bg;
private var bounce:Sound = new Sound();
private var point:Sound = new Sound();
private var channel:SoundChannel = new SoundChannel();
public var ballSpeedX:int = -3;
public var ballSpeedY:int = -2;
public function ball(score:bg)
{
theBackground = score;
bounce.load(new URLRequest("com/sounds/sfxBounce.mp3"));
point.load(new URLRequest("com/sounds/sfxScore.mp3"));
addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
}
private function loop(e:Event)
{
x += ballSpeedX;
y += ballSpeedY;
if (x <= width/2)
{
x = width/2;
ballSpeedX *= -1;
theBackground.cpuScore++;
theBackground.updateScores();
channel = point.play();
}
else if (x >= stage.stageWidth - width/2)
{
x = stage.stageWidth - width/2;
ballSpeedX *= -1;
theBackground.playerScore++;
theBackground.updateScores();
channel = point.play();
}
if (y <= height/2)
{
y = height/2;
ballSpeedY *= -1;
channel = bounce.play();
}
else if (y >= stage.stageHeight - height/2)
{
y = stage.stageHeight - height/2;
ballSpeedY *= -1;
channel = bounce.play();
}
}
public function removeBall(): void
{
removeEventListener(Event.ENTER_FRAME, loop);
parent.removeChild(this);
}
}
}
Any help or pointers would be greatly appreciated!
You can add a if (!stage) return; statement in the beginning of the loop function. Apparently you are trying to call removeBall() while processing another Event.ENTER_FRAME listener, maybe on the clock or the player or elsewhere, this hits an issue with event's listener sequence already cached inside Flash. That is, if you are removing an event listener for a certain event type from within another listener of the very same event (enter frame event qualifies), the listener still gets called for that event. You should either call removeBall() from elsewhere, not from an enterframe listener, or just have a null check in that listener to avoid weird errors popping up.
EDIT: Apparently your removeBall() is called while you do theBackground.updateScores(), and after you do this, the ball that processes the event is already removed, thus further processing of the listener is useless. Place a return statement after your sound is triggered to play in all clauses that call theBackground.updateScores() and you should be fine.
if (x <= width/2)
{
x = width/2;
ballSpeedX *= -1;
theBackground.cpuScore++;
theBackground.updateScores();
point.play();
return; // here
}
else if (x >= stage.stageWidth - width/2)
{
x = stage.stageWidth - width/2;
ballSpeedX *= -1;
theBackground.playerScore++;
theBackground.updateScores();
point.play();
return; // and here
}

TypeError: Error #2007: Parameter hitTestObject must be non-null

I've a question here.
I'm doing a game, space invader.
It prompt me an error
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at Enemy/eFrame()
I hope that anyone of you could help me with it.
Thank you very much!
//This is the basic skeleton that all classes must have
package{
//we have to import certain display objects and events
import flash.display.MovieClip;
import flash.events.*;
//this just means that Enemy will act like a MovieClip
public class Enemy extends MovieClip{
//VARIABLES
//this will act as the root of the document
//so we can easily reference it within the class
private var _root:Object;
//how quickly the enemy will move
private var speed:int = 5;
//this function will run every time the Bullet is added
//to the stage
public function Enemy(){
//adding events to this class
//functions that will run only when the MC is added
addEventListener(Event.ADDED, beginClass);
//functions that will run on enter frame
addEventListener(Event.ENTER_FRAME, eFrame);
}
private function beginClass(event:Event):void{
_root = MovieClip(root);
}
private function eFrame(event:Event):void{
//moving the bullet up screen
y += speed;
//making the bullet be removed if it goes off stage
if(this.y > stage.stageHeight){
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.removeChild(this);
}
//checking if it is touching any bullets
//we will have to run a for loop because there will be multiple bullets
for(var i:int = 0;i<_root.bulletContainer.numChildren;i++){
//numChildren is just the amount of movieclips within
//the bulletContainer.
//we define a variable that will be the bullet that we are currently
//hit testing.
var bulletTarget:MovieClip = _root.bulletContainer.getChildAt(i);
//now we hit test
if(hitTestObject(bulletTarget)){
//remove this from the stage if it touches a bullet
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.removeChild(this);
//also remove the bullet and its listeners
_root.bulletContainer.removeChild(bulletTarget);
bulletTarget.removeListeners();
//up the score
_root.score += 5;
}
}
//hit testing with the user
if(hitTestObject(_root.mcMain)){
//losing the game
_root.gameOver = true;
_root.gotoAndStop('lose');
}
if(_root.gameOver){
removeEventListener(Event.ENTER_FRAME, eFrame);
this.parent.removeChild(this);
}
}
public function removeListeners():void{
this.removeEventListener(Event.ENTER_FRAME, eFrame);
}
}
}
The error means you have called hitTestObject(null) in your code, which is invalid.
From the code you shared, either bulletTarget or _root.mcMain could be the reason.

AS3 Bouncing Ball

Okay, after learning tutorials online, I'm trying to make a bouncing ball in AS3.
Here's my code thus far:
var count:Number = 0;
var bounceHeight:Number = 100;
var floorHeight:Number = 300;
var speed:Number = .1;
function run(e:Event):void
{
ball_mc.y = floorHeight - Math.abs(Math.cos(count)) * bounceHeight;
count += speed;
}
{
this.addEventListener(Event.ENTER_FRAME(run));
}
Thanks in advance for the help!
EDIT: The compiler errors are
Scene 1, Layer 'Layer 1', Frame 1, Line 13 1195: Attempted access of
inaccessible method ENTER_FRAME through a reference with static type
Class.
Scene 1, Layer 'Layer 1', Frame 1, Line 13 1136: Incorrect
number of arguments. Expected 2.
Within your closure, addEventListener requires a type parameter and a listener function.
Your type is Event.ENTER_FRAME and your handler is run, which means to call run every frame you need:
addEventListener(Event.ENTER_FRAME, run);
Therefore, your code should be:
function run(e:Event):void
{
ball_mc.y = floorHeight - Math.abs(Math.cos(count)) * bounceHeight;
count += speed;
}
this.addEventListener(Event.ENTER_FRAME, run);