Error when catching objects and lives count wont decrease? - actionscript-3

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 :(

Related

scrolling text field help as3

Currently got text off stage that I would like to come onto the stage and stop at a certain position (97, 233.10) on my stage. I'm a little confused on where to stop it and what code to use?
addEventListener(Event.ENTER_FRAME, mcInfo);
function mcInfo (e:Event):void {
//check position of logo
//if inside the stage move left to right
//if outside stage reposition
if (info.x<stage.stageWidth) {
info.x+=30;
stop();
} else {
//reset position
info.x=-450;
}
}
Cheers!
It also seems that Flash is now returning an output error when I scroll through the rest of my pages:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at finaldesign_fla::MainTimeline/mcInfo()
In the if statement we check if the object has exceeded the target position and stop the loop if it has. Otherwise keep incrementing the object's position.
targetPosition = {x:97, y:233.10};
addEventListener(Event.ENTER_FRAME, mcInfo);
function mcInfo(e:Event) {
if (info.x >= targetPosition.x) {
info.x = targetPosition.x;
removeEventListener(Event.ENTER_FRAME, mcInfo);
} else {
info.x += 30;
}
}
make sure that info is on stage and try add:
if(info != null && stage != null)

stop(); undefined method in as3.0

i want to move to the next frame after clicking a specific button. tried putting stop(); method but it says error and the result is just alternating the 2 frames .
here's my code.
//the error says call to a possibly undefined method stop.
//i'm using adobe flash cc.
stop();
public function main ():void {
enter_button.buttonMode = true;
enter_button.addEventListener(MouseEvent.MOUSE_DOWN, checkForm);
player.text = "";
}
public function checkForm (event:MouseEvent):void {
if (player.text != ""){
gotoAndStop(1);
sendForm();
}
else{
player.text = "please enter your name";
}
}
Try MovieClip(root).gotoAndStop(1); - assuming you're trying to change frames on the main timeline and not an object.
Also, it's not clear where you're using this code, (on the timeline, in a class, or in the main .as) but stop(); should be placed in the actions panel of every frame of the timeline / movieClip.

cannot convert flash.display::Stage#2a2cdf99 to flash.display.MovieClip?

So I'm creating a platform game in Actionscript 3.0, trying to call a function that spawns blocks based on an array. The code is in a 'Game' class and is directed towards a movieclip on my .fla
When it is ran I get the error:
"cannot convert flash.display::Stage#2a2cdf99 to flash.display.MovieClip."
Here's the code:
public function GameScreen(stageRef:Stage = null )
{
this.stageRef = stageRef;
btnReturn.addEventListener(MouseEvent.MOUSE_DOWN, returnMainMenu, false, 0, true);
mcMain.addEventListener(Event.ENTER_FRAME, moveChar);
this.addEventListener(Event.ENTER_FRAME, createLvl);
this.stageRef.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
this.stageRef.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
this.stageRef.addChild(blockHolder);
}
And
private function createLvl(event:Event):void
{
var lvlArray:Array = MovieClip(root)['lvlArray' +lvlCurrent];
var lvlColumns:int = Math.ceil(lvlArray.length/16);
for(var i:int = 0;i<lvlArray.length;i++){
if(lvlArray[i] == 1){
if(i/lvlColumns == int(i/lvlColumns)){
row ++;
}
var newBlock:Block = new Block();
newBlock.graphics.beginFill(0xFFFFFF);
newBlock.graphics.drawRect(0,0,50,50);
newBlock.x = (i-(row-1)*lvlColumns)*newBlock.width;
newBlock.y = (row - 1)*newBlock.height;
blockHolder.addChild(newBlock);
} else if (lvlArray[i] == 'MAIN'){
mcMain.x = (i-(row-1)*lvlColumns)*newBlock.width;
mcMain.y = (row-1)*newBlock.height;
}
}
row = 0;
}
Please help =(
Thanks!
First of all:
Turn on "permit debugging" in your publish settings. This will give you line numbers with your errors, so you can determine the exact location of your error.
Post the entire stack trace. That error by itself is not a lot to go on.
Given the error and the code you've posted, the error must be caused by your use of MovieClip(root). The root property does not always point to the main timeline in Flash, it will point to the Stage if the display object is added directly to the stage. For example:
trace(stage.addChild(new Sprite()).root) // [object Stage]
Documentation on root: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#root

Actionscript3 shooter game error 1009 and 2009

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 ...
}
...

Going from one scene to next and gettingType1009 Null object reference errors

Okay so in my main menu for my game, I have a glow effect so that when you hover over the invisible button 'startdesu', the script will make the whole menu glow, fading in and out. This code (from the event listener onwards) has been repeated for each button on the menu, with appropriate changes to the button instance name and the function names. The only problem is, when I click 'startdesu' and it takes me to the next scene, I start getting repetitive errors, "TypeError: Error #1009: Cannot access a property or method of a null object reference.
at bjvb_fla::MainTimeline/increaseGlow()". I've tried removing the glow event listeners when the buttons are clicked and everything but no luck. Please help! ;0;
Here is the essential code for brevity's sake, but I can post the whole thing if it helps.
(also, i get the same Null error for something else when i go from the game back to the start menu.
import flash.filters.*;
import flash.events.Event;
stop();
wow.alpha=0.5;
var menuGlow:GlowFilter = new GlowFilter();
menuGlow.color = 0xffffff;
menuGlow.blurX = 20;
menuGlow.blurY = 20;
menuGlow.alpha = 0;
menu.filters = [menuGlow];
var glow:Boolean = false;
startdesu.addEventListener(MouseEvent.MOUSE_OVER, addGlow);
startdesu.addEventListener(MouseEvent.MOUSE_OUT, removeGlow);
startdesu.addEventListener(Event.ENTER_FRAME, increaseGlow);
function addGlow(e:MouseEvent):void
{
glow = true;
}
function removeGlow(e:MouseEvent):void
{
glow = false;
}
function increaseGlow(e:Event):void
{
if(glow == true)
{
menuGlow.alpha = menuGlow.alpha + 0.02;
}
else if(glow == false)
{
menuGlow.alpha = menuGlow.alpha - 0.02;
}
menu.filters = [menuGlow];
}
This is the line that is likely causing the error:
menu.filters = [menuGlow];
There is probably no object with the instance name 'menu' in your second scene. You could fix the error by just adding a check if the object exists, like so:
function increaseGlow(e:Event):void
{
//if there's no menu object, return
if(!menu) return;
if(glow == true) {
menuGlow.alpha = menuGlow.alpha + 0.02;
} else {
menuGlow.alpha = menuGlow.alpha - 0.02;
}
menu.filters = [menuGlow];
}
But the correct solution would be to remove the event listeners. Not sure why it didn't work for you, but you should be able to just add this in the click event handler before you switch your scene.
startdesu.removeEventListener(Event.ENTER_FRAME, increaseGlow);
What errors do you get when you try to remove the event listener?