Copy link to clipboard
Copied
Hi, I need help with a school project, as I have a little amount of knowledge and I'm quite inexperienced with programming. I will put the error beneath the code, thanks for your time.
stop();
var dx:Number;
var dy:Number;
var cCounter:int=0;
var xSpeed:Number=10;
var stageWidth:Number=stage.stageWidth;
var stageHeight:Number=stage.stageHeight;
var x_coordinate:Number=x;
var y_coordinate:Number=y;
var speed:Number=15;
function randRange(minNum:Number, maxNum:Number):Number {
return (Math.floor(Math.random()*(maxNum - minNum + 1)) + minNum);
}
stage.addEventListener(Event.ENTER_FRAME, checkMouse);
function checkMouse(evt:Event):void {
dx=mouseX-gun.x;
dy=mouseY-gun.y;
gun.rotation = (Math.atan2(dy, dx) * 180 / Math.PI);
}
stage.addEventListener(MouseEvent.CLICK, shotFired);
function shotFired(Mouse:Event):void {
var bulletMC:MovieClip = new bullet_mc();
cCounter++;
bulletMC.x=gun.x;
bulletMC.y=gun.y;
bulletMC.rotation=gun.rotation;
bulletMC.speed=15;
bulletMC.name="bullet_mc"+cCounter;
bulletMC.addEventListener(Event.ENTER_FRAME, BulletMoveHandler);
addChild(bulletMC);
}
function BulletMoveHandler(event:Event):void {
var bulletMC:MovieClip=event.currentTarget as MovieClip;
bulletMC.x=bulletMC.x+Math.cos(bulletMC.rotation/180*Math.PI)*speed;
bulletMC.y=bulletMC.y+Math.sin(bulletMC.rotation/180*Math.PI)*speed;
if (bulletMC.x>stageWidth||bulletMC.y>stageHeight) {
removeChild(bulletMC);
bulletMC.removeEventListener(Event.ENTER_FRAME, BulletMoveHandler);
}
}
function createEnemy():void {
if (randRange(0,25)==0) {
var enemyMC:MovieClip = new enemy_mc();
cCounter++;
x_coordinate= - enemyMC.width;
y_coordinate=randRange(stageHeight-enemyMC.height,215);
enemyMC.x=x_coordinate;
enemyMC.y=y_coordinate;
enemyMC.speed=xSpeed;
enemyMC.name="Enemy"+cCounter;
enemyMC.addEventListener(Event.ENTER_FRAME, EnemyMoveHandler);
addChild(enemyMC);
}
if (enemyMC.hitTestObject(bulletMC)) {
var bulletMC:MovieClip=bullet_mc(0);
removeChild(enemyMC);
enemyMC.removeEventListener(Event.ENTER_FRAME, EnemyMoveHandler);
}
}
function EnemyMoveHandler(event:Event):void {
var enemyMC:MovieClip=event.currentTarget as MovieClip;
enemyMC.x+=enemyMC.speed;
if (enemyMC.x>stageWidth) {
removeChild(enemyMC);
enemyMC.removeEventListener(Event.ENTER_FRAME, EnemyMoveHandler);
}
}
cursor_mc.startDrag(true);
Mouse.hide();
stage.addEventListener(Event.ENTER_FRAME, main);
function main(event:Event):void {
createEnemy();
}
for each(var enemy:enemy_mc in enemy_mc){
for each(var bullet:bullet_mc in bullet_mc){
if(bullet.x > enemy.x - enemy.width / 2 &&
bullet.x < enemy.x + enemy.width / 2 &&
bullet.y > enemy.y - enemy.height / 2 &&
bullet.y < enemy.y + enemy.width / 2){
trace("collision");
}
}
}
Error -
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Flash001_Shooting_fla::MainTimeline/createEnemy()
at Flash001_Shooting_fla::MainTimeline/main()
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at Flash001_Shooting_fla::MainTimeline/createEnemy()
at Flash001_Shooting_fla::MainTimeline/main()
The 1009 error indicates that one of the objects being targeted by your code is out of scope. This could mean that the object....
- is declared but not instantiated
- doesn't have an instance name (or the instance name is mispelled)
- does not exist in the frame where that code is trying to talk to it
- is animated into place but is not assigned instance names in every keyframe for it
- is one of two or more consecutive keyframes of the same objects with no name assigned in the preceding frame(s).
...Copy link to clipboard
Copied
There are indentations it is just the way it copied and pasted. Sorry for the inconvenience. Also if anyone wants the file to download to try and have a go at helping me that way, you can send me your Gmail and I can share it with you on Google Drive.
Copy link to clipboard
Copied
The 1009 error indicates that one of the objects being targeted by your code is out of scope. This could mean that the object....
- is declared but not instantiated
- doesn't have an instance name (or the instance name is mispelled)
- does not exist in the frame where that code is trying to talk to it
- is animated into place but is not assigned instance names in every keyframe for it
- is one of two or more consecutive keyframes of the same objects with no name assigned in the preceding frame(s).
If you go into your Publish Settings Flash section and select the option to Permit debugging, your error message should have a line number following the frame number which will help you isolate which object is involved.
For the 2007 error, if you are trying to test if you hit an object that has been removed from the game (likely because it was hit earlier, as in the last time it was checked) that will end up with the function trying to test a null object. What you need to do in that case is to remove that object from the candidates that are being tested for hits.
Copy link to clipboard
Copied
Thank you, I will try and use your advice, I will get back to you If I do it correctly, thanks again Ned.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now