Copy link to clipboard
Copied
Hello,
I'm continue to get these errors but mainly Error 2007:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at 15SpaceShooter_fla::MainTimeline/moveShip()[15SpaceShooter_fla.MainTimeline::frame3:48]
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at Enemy/eFrame()
Here is my Enemy.as file
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(playerShip)){
//losing the game
_root.gameOver = true;
removeEventListener(Event.ENTER_FRAME, eFrame);
this.parent.removeChild(this);
_root.gotoAndStop('lose');
}
}
public function removeListeners():void{
this.removeEventListener(Event.ENTER_FRAME, eFrame);
}
}
}
And here is my file for actions
stop();
//these booleans will check which keys are down
var leftDown:Boolean = false;
var upDown:Boolean = false;
var rightDown:Boolean = false;
var downDown:Boolean = false;
//how fast the character will be able to go
var mainSpeed:int = 5;
//how much time before allowed to shoot again
var cTime:int = 0;
//the time it has to reach in order to be allowed to shoot (in frames)
var cLimit:int = 12;
//whether or not the user is allowed to shoot
var shootAllow:Boolean = true;
//how much time before another enemy is made
var enemyTime:int = 0;
//how much time needed to make an enemy
//it should be more than the shooting rate
//or else killing all of the enemies would
//be impossible 😮
var enemyLimit:int = 16;
//the player's score
var score:int = 0;
//this movieclip will hold all of the bullets
var bulletContainer:MovieClip = new MovieClip();
addChild(bulletContainer);
//whether or not the game is over
var gameOver:Boolean = false;
//adding a listener to mcMain that will move the character
playerShip.addEventListener(Event.ENTER_FRAME, moveShip);
function moveShip(event:Event):void{
//checking if the key booleans are true then moving
//the character based on the keys
if(leftDown){
playerShip.x -= mainSpeed;
}
if(upDown){
playerShip.y -= mainSpeed;
}
if(rightDown){
playerShip.x += mainSpeed;
}
if(downDown){
playerShip.y += mainSpeed;
}
//keeping the main character within bounds
if(playerShip.x <= 0){
playerShip.x += mainSpeed;
}
if(playerShip.y <= 0){
playerShip.y += mainSpeed;
}
if(playerShip.x >= stage.stageWidth - playerShip.width){
playerShip.x -= mainSpeed;
}
if(playerShip.y >= stage.stageHeight - playerShip.height){
playerShip.y -= mainSpeed;
}
//Incrementing the cTime
//checking if cTime has reached the limit yet
if(cTime < cLimit){
cTime ++;
} else {
//if it has, then allow the user to shoot
shootAllow = true;
//and reset cTime
cTime = 0;
}
//adding enemies to stage
if(enemyTime < enemyLimit){
//if time hasn't reached the limit, then just increment
enemyTime ++;
} else {
//defining a variable which will hold the new enemy
var newEnemy = new Enemy();
//making the enemy offstage when it is created
newEnemy.y = -1 * newEnemy.height;
//making the enemy's x coordinates random
//the "int" function will act the same as Math.floor but a bit faster
newEnemy.x = int(Math.random()*(stage.stageWidth - newEnemy.width));
//then add the enemy to stage
addChild(newEnemy);
//and reset the enemyTime
enemyTime = 0;
}
//updating the score text
scoreField.text = 'Score: '+score;
}
//this listener will listen for down keystrokes
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
function checkKeysDown(event:KeyboardEvent):void{
//making the booleans true based on the keycode
//WASD Keys or arrow keys
if(event.keyCode == 37 || event.keyCode == 65){
leftDown = true;
}
if(event.keyCode == 38 || event.keyCode == 87){
upDown = true;
}
if(event.keyCode == 39 || event.keyCode == 68){
rightDown = true;
}
if(event.keyCode == 40 || event.keyCode == 83){
downDown = true;
}
//checking if the space bar is pressed and shooting is allowed
if(event.keyCode == 32 && shootAllow){
//making it so the user can't shoot for a bit
shootAllow = false;
//declaring a variable to be a new Bullet
var newBullet:Bullet = new Bullet();
//changing the bullet's coordinates
newBullet.x = playerShip.x + playerShip.width/2 - newBullet.width/2;
newBullet.y = playerShip.y;
//then we add the bullet to stage
bulletContainer.addChild(newBullet);
}
}
//this listener will listen for keys being released
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
function checkKeysUp(event:KeyboardEvent):void{
//making the booleans false based on the keycode
if(event.keyCode == 37 || event.keyCode == 65){
leftDown = false;
}
if(event.keyCode == 38 || event.keyCode == 87){
upDown = false;
}
if(event.keyCode == 39 || event.keyCode == 68){
rightDown = false;
}
if(event.keyCode == 40 || event.keyCode == 83){
downDown = false;
}
}
I'm not sure but it keeps specifiying Line 57 on enemy.as and then moveShip which cause Frame 3 of my Space Shooter game to act up
Thanks
Luke
Copy link to clipboard
Copied
You're doing the hit test with a root level movieclip named playerShip, but you're saying this:
if(hitTestObject(playerShip)){
instead of:
if(hitTestObject(_root.playerShip)){
Copy link to clipboard
Copied
I just realized that, but it's still stating those two errors...
Thanks
Luke
Copy link to clipboard
Copied
Try adding 'this' to the hittestobject lines:
if(this.hitTestObject(bulletTarget)){
if(this.hitTestObject(playerShip)){
Copy link to clipboard
Copied
Still getting Errors.
Stating Line 57 on Enemy.as and Line 48 in the frame code
Luke
Copy link to clipboard
Copied
Those two lines are commented out, so there's some difference between the code you showed and the code that is being run.
Are you able to post a zip file online somewhere, of the FLA and the source files needed to try it out?
Copy link to clipboard
Copied
Here we go: 15 Space Shooter - Google Drive
Please note: Frame 1 & 2 are in the works as introduction frames however there is no actionscript on those frames yet so it shouldn't be causing any problem, let alone being run.
Please note I run Adobe CS4 so if you send back my files, could it please be to CS4, I don't have animate unfortunately
Thanks
Luke
Copy link to clipboard
Copied
There are two separate problems. You're going to the lose frame but not stopping the playerShip enterframe function. Also, every enemy on screen is continuing to look for playerShip after you've already gone to the lose screen.
These should sort out both problems:
In your frame 1 script, add this function somewhere:
function goLose(){
playerShip.removeEventListener(Event.ENTER_FRAME, moveShip);
gotoAndStop("lose");
}
In Enemy.as, change the start of the eFrame function to have these extra lines:
private function eFrame(event:Event):void{
if(_root.gameOver){
removeEventListener(Event.ENTER_FRAME, eFrame);
this.parent.removeChild(this);
return;
}
Also in Enemy.as, change this line:
_root.gotoAndStop('lose');
to be this:
_root.goLose();
The goLose function stops the playerShip enterframe listener, and also handles going to the lose frame. The changes in Enemy.as will make it look at the gameOver variable, to check if the game is over, before looking at hittestobject, to make sure that playerShip is still around.
Copy link to clipboard
Copied
Ok so now is says _root.goLose() is not a function Error 1006 on Line 67, all other errors have cleared
Thanks
Luke
Copy link to clipboard
Copied
If you added this to frame 1 timeline script:
function goLose(){
playerShip.removeEventListener(Event.ENTER_FRAME, moveShip);
gotoAndStop("lose");
}
then replace the files on the server, and I'll try what you've got so far.
I can't easily make a CS4 FLA, otherwise I could have sent you my one.
Copy link to clipboard
Copied
Here is what I did, updated the drive folder with what I did.
15 Space Shooter - Google Drive
TypeError: Error #1006: goLose is not a function.
at Enemy/eFrame()
That's the error I get but on the Enemy.as file
Thanks
Luke
Copy link to clipboard
Copied
Your issue comes from the fact that you are somewhere in your code on a frame
that you lose some properties. everything must be set on the Main class or the frame 1,
unless if you create symbols dynamically from the library.
Copy link to clipboard
Copied
Although a lot of people only use external code, and a lot more only use timeline code, you can mix them together and it will work.
Currently the first problem is that this is still missing from the frame 1 script:
function goLose(){
playerShip.removeEventListener(Event.ENTER_FRAME, moveShip);
gotoAndStop("lose");
}
You can put it at the end if you like. Once you get past that there is a problem on line 90:
scoreField.text = 'Score: '+score;
You only have scoreField on frame 1. Put it into its own layer, that spans across to frame 2, then the error will go away.
Copy link to clipboard
Copied
Perfect Colin Holgate, thanks for the assistance! However another error has popped up....
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Bullet/eFrame()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Bullet/eFrame()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at 15SpaceShooter_fla::MainTimeline/checkKeysDown()[15SpaceShooter_fla.MainTimeline::frame3:136]
If you scroll back to my original post, the Line 52 on Enemy.as is saying that error
Here is the Bullet.as code:
//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 Bullet will act like a MovieClip
public class Bullet 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 bullet will move
private var speed:int = 10;
//this function will run every time the Bullet is added
//to the stage
public function Bullet(){
//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 < -1 * this.height){
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.bulletContainer.removeChild(this);
}
//checking if game is over
if(_root.gameOver){
removeEventListener(Event.ENTER_FRAME, eFrame);
this.parent.removeChild(this);
}
}
public function removeListeners():void{
removeEventListener(Event.ENTER_FRAME, eFrame);
}
}
}
These errors happen everytime I click stage to restart game. The score isn't calculating at the end and then when I click the stage to restart, it doesn't calculate the score at all, resulting in these errors.
Can you look to see what I did wrong or misplaced?
Thanks
Luke
Copy link to clipboard
Copied
Did you update your posted files?
Copy link to clipboard
Copied
Here is my updated files:
Once again, Here are the errors I now am getting:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Bullet/eFrame()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Bullet/eFrame()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at 15SpaceShooter_fla::MainTimeline/checkKeysDown()[15SpaceShooter_fla.MainTimeline::frame3: 136]
If you scroll back to my original post, the Line 52 on Enemy.as is saying that error
Frame Code:
stop();
//these booleans will check which keys are down
var leftDown:Boolean=false;
var upDown:Boolean=false;
var rightDown:Boolean=false;
var downDown:Boolean=false;
//how fast the character will be able to go
var mainSpeed:int=5;
//how much time before allowed to shoot again
var cTime:int=0;
//the time it has to reach in order to be allowed to shoot (in frames)
var cLimit:int=12;
//whether or not the user is allowed to shoot
var shootAllow:Boolean=true;
//how much time before another enemy is made
var enemyTime:int=0;
//how much time needed to make an enemy
//it should be more than the shooting rate
//or else killing all of the enemies would
//be impossible 😮
var enemyLimit:int=16;
//the player's score
var score:int=0;
//this movieclip will hold all of the bullets
var bulletContainer:MovieClip = new MovieClip();
addChild(bulletContainer);
//whether or not the game is over
var gameOver:Boolean=false;
//adding a listener to mcMain that will move the character
playerShip.addEventListener(Event.ENTER_FRAME, moveShip);
function moveShip(event:Event):void {
//checking if the key booleans are true then moving
//the character based on the keys
if (leftDown) {
playerShip.x-=mainSpeed;
}
if (upDown) {
playerShip.y-=mainSpeed;
}
if (rightDown) {
playerShip.x+=mainSpeed;
}
if (downDown) {
playerShip.y+=mainSpeed;
}
//keeping the main character within bounds
if (playerShip.x<=0) {
playerShip.x+=mainSpeed;
}
if (playerShip.y<=0) {
playerShip.y+=mainSpeed;
}
if (playerShip.x>=stage.stageWidth-playerShip.width) {
playerShip.x-=mainSpeed;
}
if (playerShip.y>=stage.stageHeight-playerShip.height) {
playerShip.y-=mainSpeed;
}
//Incrementing the cTime
//checking if cTime has reached the limit yet
if (cTime<cLimit) {
cTime++;
} else {
//if it has, then allow the user to shoot
shootAllow=true;
//and reset cTime
cTime=0;
}
//adding enemies to stage
if (enemyTime<enemyLimit) {
//if time hasn't reached the limit, then just increment
enemyTime++;
} else {
//defining a variable which will hold the new enemy
var newEnemy = new Enemy();
//making the enemy offstage when it is created
newEnemy.y=-1*newEnemy.height;
//making the enemy's x coordinates random
//the "int" function will act the same as Math.floor but a bit faster
newEnemy.x = int(Math.random()*(stage.stageWidth - newEnemy.width));
//then add the enemy to stage
addChild(newEnemy);
//and reset the enemyTime
enemyTime=0;
}
//updating the score text
scoreField.text="Score: "+score;
}
//this listener will listen for down keystrokes
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
function checkKeysDown(event:KeyboardEvent):void {
//making the booleans true based on the keycode
//WASD Keys or arrow keys
if (event.keyCode==37||event.keyCode==65) {
leftDown=true;
}
if (event.keyCode==38||event.keyCode==87) {
upDown=true;
}
if (event.keyCode==39||event.keyCode==68) {
rightDown=true;
}
if (event.keyCode==40||event.keyCode==83) {
downDown=true;
}
//this listener will listen for keys being released
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
function checkKeysUp(event:KeyboardEvent):void{
//making the booleans false based on the keycode
if(event.keyCode == 37 || event.keyCode == 65){
leftDown = false;
}
if(event.keyCode == 38 || event.keyCode == 87){
upDown = false;
}
if(event.keyCode == 39 || event.keyCode == 68){
rightDown = false;
}
if(event.keyCode == 40 || event.keyCode == 83){
downDown = false;
}
}
//checking if the space bar is pressed and shooting is allowed
if (event.keyCode==32&&shootAllow) {
//making it so the user can't shoot for a bit
shootAllow=false;
//declaring a variable to be a new Bullet
var newBullet:Bullet = new Bullet();
//changing the bullet's coordinates
newBullet.x=playerShip.x+playerShip.width/2-newBullet.width/2;
newBullet.y=playerShip.y;
//then we add the bullet to stage
bulletContainer.addChild(newBullet);
}
}
function goLose() {
playerShip.removeEventListener(Event.ENTER_FRAME, moveShip);
gotoAndStop("lose");
}
stage.addEventListener(Event.ENTER_FRAME, generateParticles);
//checking if there already is another particlecontainer there
if(particleContainer == null){
//this movieclip will hold all of the particles
var particleContainer:MovieClip = new MovieClip();
addChild(particleContainer);
}
function generateParticles(event:Event):void{
//so we don't do it every frame, we'll do it randomly
if(Math.random()*10 < 2){
//creating a new shape
var mcParticle:Shape = new Shape();
//making random dimensions (only ranges from 1-5 px)
var dimensions:int = int(Math.random()*5)+1;
//add color to the shape
mcParticle.graphics.beginFill(0x999999/*The color for shape*/,1/*The alpha for the shape*/);
//turning the shape into a square
mcParticle.graphics.drawRect(dimensions,dimensions,dimensions,dimensions);
//change the coordinates of the particle
mcParticle.x = int(Math.random()*stage.stageWidth);
mcParticle.y = -10;
//adding the particle to stage
particleContainer.addChild(mcParticle);
}
//making all of the particles move down stage
for(var i:int=0;i<particleContainer.numChildren;i++){
//getting a certain particle
var theParticle:Shape = Shape(particleContainer.getChildAt(i));
//it'll go half the speed of the character
theParticle.y += mainSpeed*.5;
//checking if the particle is offstage
if(theParticle.y >= 400){
//remove it
particleContainer.removeChild(theParticle);
}
}
}
Enemy.as File Code:
package {
import flash.display.MovieClip;
import flash.events.*;
//Enemy acts as MovieClip
public class Enemy extends MovieClip {
//VARIABLES
private var _root:Object;
//speed of enemy
private var speed:int=5;
//Bullet added to stage
public function Enemy() {
addEventListener(Event.ADDED, beginClass);
addEventListener(Event.ENTER_FRAME, eFrame);
}
private function beginClass(event:Event):void {
_root=MovieClip(root);
}
private function eFrame(event:Event):void {
if (_root.gameOver) {
removeEventListener(Event.ENTER_FRAME, eFrame);
this.parent.removeChild(this);
return;
}
//moving bullet on screen, going up
y+=speed;
//move bullet if off stage
if (this.y>stage.stageHeight) {
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.removeChild(this);
}
//checking if it is touching any bullets
for (var i:int = 0; i<_root.bulletContainer.numChildren; i++) {
var bulletTarget:MovieClip=_root.bulletContainer.getChildAt(i);
//now we hit test
if (this.hitTestObject(bulletTarget)) {
//remove this from the stage if bullet touches
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.removeChild(this);
//remove bullet and listeners
_root.bulletContainer.removeChild(bulletTarget);
bulletTarget.removeListeners();
//increase score
_root.score+=5;
}
}
//user hit testing
if (this.hitTestObject(_root.playerShip)) {
//losing the game
_root.gameOver=true;
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.goLose();
}
}
public function removeListeners():void {
this.removeEventListener(Event.ENTER_FRAME, eFrame);
}
}
}
Bullet.as File:
//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 Bullet will act like a MovieClip
public class Bullet 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 bullet will move
private var speed:int = 10;
//this function will run every time the Bullet is added
//to the stage
public function Bullet(){
//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 < -1 * this.height){
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.bulletContainer.removeChild(this);
}
//checking if game is over
if(_root.gameOver){
removeEventListener(Event.ENTER_FRAME, eFrame);
this.parent.removeChild(this);
}
}
public function removeListeners():void{
removeEventListener(Event.ENTER_FRAME, eFrame);
}
}
}
Copy link to clipboard
Copied
With that code I only get the error about the scoreField being missing. I said earlier that you should put that onto both frames.
If you have fixed the scoreField and still get errors, you may need to update your posted files again, so I can have a fresh start with your current files.
Copy link to clipboard
Copied
Colin Holgate Sorry, I'm not exactly sure what you mean, are you able to look at my document?
I put it on both Frame 3/4 (1,2 are intro)
15 Space Shooter - Google Drive
Thanks,
Luke
PS. Could you send over the documents back once you update them. Appreciate it!
Copy link to clipboard
Copied
In frame 3 you have a textfield named scoreField. In frame 4 you have another textfield, also named scoreField. I think that is confusing the frame 3 script, because it's trying to put text into scoreField from frame 4, that isn't around any more.
If you put the scoreField textfield into its own layer, and have it be in frame 1,2, 3 and 4, the game will replay ok, and you can fix the other issues that are going to come up, then come back to the score later on, and have it off stage when it's not needed, small and at the bottom during game play, and bigger and centered for the lose screen. You can do that with keyframes in the same layer.
But like I said, come back to that later.
I can't make a CS4 FLA to send to you, but here is how I have the timeline, which makes it easier to see things, and it solves the errors because you keep only one scoreField:
Copy link to clipboard
Copied
Some of the other errors you saw were because the keyboard listener was still going. Change the goLose function to close that listener too:
function goLose() {
stage.removeEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
playerShip.removeEventListener(Event.ENTER_FRAME, moveShip);
gotoAndStop("lose");
}
Copy link to clipboard
Copied
Okay wait, please be patient with me Colin Holgate , what do I write in the Score field layer?
Cause I had :
var score:int=0
scoreField.text("Score: ")+score
Pretty sure I'm missing something right?
Thanks again for all the help
Luke
Copy link to clipboard
Copied
Select the scoreField textfield in frame 3, and do a Cut. Make a new layer, and Edit/Paste in Place the scoreField into frame 1. Go to frame 4 and delete the larger version of scoreField.
You should then only have one scoreField, that starts in frame 1 and goes all the way to the end. Then most of the errors will go away.
Copy link to clipboard
Copied
Colin Holgate​ So I did that and now I get a error on that line:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at 15SpaceShooter_fla::MainTimeline/frame1()[15SpaceShooter_fla.MainTimeline::frame1:1]
Did I need to add any variable? (var score:int=0)?
Luke
Copy link to clipboard
Copied
Colin Holgate​ I fixed the scoreField.text.
However the goLose is not a function error is returning
TypeError: Error #1006: goLose is not a function.
at Enemy/eFrame()
Enemy.as code (again)
package {
import flash.display.MovieClip;
import flash.events.*;
//Enemy acts as MovieClip
public class Enemy extends MovieClip {
//VARIABLES
private var _root:Object;
//speed of enemy
private var speed:int=5;
//Bullet added to stage
public function Enemy() {
addEventListener(Event.ADDED, beginClass);
addEventListener(Event.ENTER_FRAME, eFrame);
}
private function beginClass(event:Event):void {
_root=MovieClip(root);
}
private function eFrame(event:Event):void {
if (_root.gameOver) {
removeEventListener(Event.ENTER_FRAME, eFrame);
this.parent.removeChild(this);
return;
}
//moving bullet on screen, going up
y+=speed;
//move bullet if off stage
if (this.y>stage.stageHeight) {
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.removeChild(this);
}
//checking if it is touching any bullets
for (var i:int = 0; i<_root.bulletContainer.numChildren; i++) {
var bulletTarget:MovieClip=_root.bulletContainer.getChildAt(i);
//now we hit test
if (this.hitTestObject(bulletTarget)) {
//remove this from the stage if bullet touches
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.removeChild(this);
//remove bullet and listeners
_root.bulletContainer.removeChild(bulletTarget);
bulletTarget.removeListeners();
//increase score
_root.score+=5;
}
}
//user hit testing
if (this.hitTestObject(_root.playerShip)) {
//losing the game
_root.gameOver=true;
removeEventListener(Event.ENTER_FRAME, eFrame);
_root.goLose()
}
}
public function removeListeners():void {
this.removeEventListener(Event.ENTER_FRAME, eFrame);
}
}
}
Frame Code:
stop();
//these booleans will check which keys are down
var leftDown:Boolean=false;
var upDown:Boolean=false;
var rightDown:Boolean=false;
var downDown:Boolean=false;
//how fast the character will be able to go
var mainSpeed:int=5;
//how much time before allowed to shoot again
var cTime:int=0;
//the time it has to reach in order to be allowed to shoot (in frames)
var cLimit:int=12;
//whether or not the user is allowed to shoot
var shootAllow:Boolean=true;
//how much time before another enemy is made
var enemyTime:int=0;
//how much time needed to make an enemy
//it should be more than the shooting rate
//or else killing all of the enemies would
//be impossible 😮
var enemyLimit:int=16;
//the player's score
//var score:int=0;
//this movieclip will hold all of the bullets
var bulletContainer:MovieClip = new MovieClip();
addChild(bulletContainer);
//whether or not the game is over
var gameOver:Boolean=false;
//adding a listener to mcMain that will move the character
playerShip.addEventListener(Event.ENTER_FRAME, moveShip);
function moveShip(event:Event):void {
//checking if the key booleans are true then moving
//the character based on the keys
if (leftDown) {
playerShip.x-=mainSpeed;
}
if (upDown) {
playerShip.y-=mainSpeed;
}
if (rightDown) {
playerShip.x+=mainSpeed;
}
if (downDown) {
playerShip.y+=mainSpeed;
}
//keeping the main character within bounds
if (playerShip.x<=0) {
playerShip.x+=mainSpeed;
}
if (playerShip.y<=0) {
playerShip.y+=mainSpeed;
}
if (playerShip.x>=stage.stageWidth-playerShip.width) {
playerShip.x-=mainSpeed;
}
if (playerShip.y>=stage.stageHeight-playerShip.height) {
playerShip.y-=mainSpeed;
}
//Incrementing the cTime
//checking if cTime has reached the limit yet
if (cTime<cLimit) {
cTime++;
} else {
//if it has, then allow the user to shoot
shootAllow=true;
//and reset cTime
cTime=0;
}
//adding enemies to stage
if (enemyTime<enemyLimit) {
//if time hasn't reached the limit, then just increment
enemyTime++;
} else {
//defining a variable which will hold the new enemy
var newEnemy = new Enemy();
//making the enemy offstage when it is created
newEnemy.y=-1*newEnemy.height;
//making the enemy's x coordinates random
//the "int" function will act the same as Math.floor but a bit faster
newEnemy.x = int(Math.random()*(stage.stageWidth - newEnemy.width));
//then add the enemy to stage
addChild(newEnemy);
//and reset the enemyTime
enemyTime=0;
}
}
//this listener will listen for down keystrokes
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
function checkKeysDown(event:KeyboardEvent):void {
//making the booleans true based on the keycode
//WASD Keys or arrow keys
if (event.keyCode==37||event.keyCode==65) {
leftDown=true;
}
if (event.keyCode==38||event.keyCode==87) {
upDown=true;
}
if (event.keyCode==39||event.keyCode==68) {
rightDown=true;
}
if (event.keyCode==40||event.keyCode==83) {
downDown=true;
}
//this listener will listen for keys being released
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
function checkKeysUp(event:KeyboardEvent):void{
//making the booleans false based on the keycode
if(event.keyCode == 37 || event.keyCode == 65){
leftDown = false;
}
if(event.keyCode == 38 || event.keyCode == 87){
upDown = false;
}
if(event.keyCode == 39 || event.keyCode == 68){
rightDown = false;
}
if(event.keyCode == 40 || event.keyCode == 83){
downDown = false;
}
function goLose() {
stage.removeEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
playerShip.removeEventListener(Event.ENTER_FRAME, moveShip);
gotoAndStop("lose");
}
}
//checking if the space bar is pressed and shooting is allowed
if (event.keyCode==32&&shootAllow) {
//making it so the user can't shoot for a bit
shootAllow=false;
//declaring a variable to be a new Bullet
var newBullet:Bullet = new Bullet();
//changing the bullet's coordinates
newBullet.x=playerShip.x+playerShip.width/2-newBullet.width/2;
newBullet.y=playerShip.y;
//then we add the bullet to stage
bulletContainer.addChild(newBullet);
}
}
stage.addEventListener(Event.ENTER_FRAME, generateParticles);
//checking if there already is another particlecontainer there
if(particleContainer == null){
//this movieclip will hold all of the particles
var particleContainer:MovieClip = new MovieClip();
addChild(particleContainer);
}
function generateParticles(event:Event):void{
//so we don't do it every frame, we'll do it randomly
if(Math.random()*10 < 2){
//creating a new shape
var mcParticle:Shape = new Shape();
//making random dimensions (only ranges from 1-5 px)
var dimensions:int = int(Math.random()*5)+1;
//add color to the shape
mcParticle.graphics.beginFill(0x999999/*The color for shape*/,1/*The alpha for the shape*/);
//turning the shape into a square
mcParticle.graphics.drawRect(dimensions,dimensions,dimensions,dimensions);
//change the coordinates of the particle
mcParticle.x = int(Math.random()*stage.stageWidth);
mcParticle.y = -10;
//adding the particle to stage
particleContainer.addChild(mcParticle);
}
//making all of the particles move down stage
for(var i:int=0;i<particleContainer.numChildren;i++){
//getting a certain particle
var theParticle:Shape = Shape(particleContainer.getChildAt(i));
//it'll go half the speed of the character
theParticle.y += mainSpeed*.5;
//checking if the particle is offstage
if(theParticle.y >= 400){
//remove it
particleContainer.removeChild(theParticle);
}
}
}
I'm not sure why it's doing that. Is it because of where I have function goLose() in the Frame Code?
Thanks
Luke
Copy link to clipboard
Copied
Try this:
public function Enemy(){
addEventListener(Event.ADDED_TO_STAGE,beginClass);
}
private function beginClass(event:Event):void {
_root=MovieClip(root);
addEventListener(Event.ENTER_FRAME, eFrame);
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now