Copy link to clipboard
Copied
Well as the title says, flash is screaming at me "TypeError: Error #1010: A term is undefined and has no properties.
at main/onenterFrame()"
But I dont know why im getting this error. This is the first game i have tried to make without following a tutorial... Not going to bad so far. It still tests, but once I kill about 10 enemies flash crashes! But everytime i shoot a enemy I get hit error.
This is my main class
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class main extends MovieClip
{
private var enemySpawnTimer:Timer = new Timer(1000,8);
private var _bullets:Array;
private var _enemies:Array;
private var _damage:Number;
public function main()
{
_bullets = new Array();
_bullets = [];
_enemies = new Array();
_enemies = [];
player.stop();
enemySpawnTimer.addEventListener(TimerEvent.TIMER , onTimeStart);
stage.addEventListener("bulletCreated", onBulletCreated);
addEventListener(Event.ENTER_FRAME, onenterFrame);
}
private function onBulletCreated(event:Event)
{
_bullets.push(MovieClip(event.target));
}
private function onTimeStart(event:TimerEvent):void
{
var newEnemy:Enemy = new Enemy();
addChild(newEnemy);
newEnemy.x = Math.ceil(Math.random() * stage.stageWidth);
newEnemy.y = Math.ceil(Math.random() * stage.stageHeight);
_enemies.push(newEnemy);
}
public function onenterFrame(event:Event):void
{
enemySpawnTimer.start();
for (var j:int = 0; j < _bullets.length; j++)
{
for (var i:int = 0; i < _enemies.length; i++)
{
if (player.hitTestObject(_enemies))
{
trace("hit");
health.scaleX -= 2;
i--;
}
if (_bullets
If you have any suggestions for optimizing my code please let me know, it will be much appreciated.
Copy link to clipboard
Copied
I have not gone through your entire code, but I did notice a declaration which just doesn't seem right. Try this and see what you come up with:
This is your code:
public class main extends MovieClip
{
private var enemySpawnTimer:Timer = new Timer(1000,8);
private var _bullets:Array;
private var _enemies:Array;
private var _damage:Number;
public function main()
{
_bullets = new Array();
_bullets = [];
_enemies = new Array();
_enemies = [];
player.stop();
enemySpawnTimer.addEventListener(TimerEvent.TIMER , onTimeStart);
stage.addEventListener("bulletCreated", onBulletCreated);
addEventListener(Event.ENTER_FRAME, onenterFrame);
}
Your timer object(enemySpawnTimer) should be initialized within the constructor. You have declared it and initialized at the same time. So when the construction is initialized, it is not creating the timer object:
This is how it should be:
public class main extends MovieClip { private var enemySpawnTimer:Timer; private var _bullets:Array; private var _enemies:Array; private var _damage:Number; public function main() { enemySpawnTimer =
= new Timer(1000,8);
_bullets = new Array(); _bullets = []; _enemies = new Array(); _enemies = []; player.stop(); enemySpawnTimer.addEventListener(TimerEvent.TIMER , onTimeStart); stage.addEventListener("bulletCreated", onBulletCreated); addEventListener(Event.ENTER_FRAME, onenterFrame); }
Give it a try, see how you make out.
Thanks.
Copy link to clipboard
Copied
"health" and "weapon" look suspicious but click file/publish settings/flash and tick "permit debugging". retest.
the problematic line of code will be mentioned in the error message allowing you to pinpoint your error.
Copy link to clipboard
Copied
says its on line 55, but I cannot see anything wrong with it.
this is line 55 - if (_bullets
and all together from 55 and below:
if (_bullets
{
_enemies.sub.health.scaleX -= .1;
removeChild(_bullets
_bullets.splice(j,1);
j--;
}
if (_enemies.sub.health.scaleX <= 0)
{
removeChild(_enemies);
_enemies.splice(i,1);
i--;
}
}
}
if (player.hitTestObject(weapon))
{
player.gotoAndStop(2);
weapon.isArmed = true;
removeChild(weapon);
}
}
public function checkCollisionWithPlayer(wall:MovieClip)
{
Collision.block(player, wall);
}
}
}
Copy link to clipboard
Copied
both for-loops are problematic if elements are spliced from the corresponding arrays during the loops. also, never change a for-loop variable in the loop body. you're going to cause all sorts of problems.
you should use the trace() function to see what you're doing if you want a better understanding of the problems you were causing.
to fix your for-loops, use:
for (var j:int =_bullets.length-1;j>=0; j--)
{
for (var i:int =_enemies.length-1;i>=0; i--)
{
if (player.hitTestObject(_enemies))
{
trace("hit");
health.scaleX -= 2;
}
if (_bullets.hitTestObject(_enemies))
{
_enemies.sub.health.scaleX -= .1;
removeChild(_bullets);
_bullets.splice(j,1);
}
if (_enemies.sub.health.scaleX <= 0)
{
removeChild(_enemies);
_enemies.splice(i,1);
}
}
}