Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 12-29-2008, 03:25 AM   #1
Kimchee
Senior Member
 
Join Date: Nov 2008
Location: Provo, UT
Posts: 110
Default Simple Game, bugging out?

So I have only been working with AS3 for about 2 months now, but have been able to do quite a bit and almost have a math puzzle game finished. However, when I added a menu screen I started to get a lot of errors. If someone has about 10 minutes of spare time and would be willing to review some code to see why I am getting a Null reference error message I would be truly appreciative.

Again, this is a big request and I don't expect it of anyone, but I would be most appreciative. Also I have answered quite a few questions and tested quite a few games on this board already so I don't feel like I am asking without giving. Thanks for your time
Attached Files
File Type: zip almost.zip (460.5 KB, 55 views)
Kimchee is offline   Reply With Quote
Old 12-29-2008, 05:20 AM   #2
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 3,160
Send a message via AIM to Mazoonist
Default

I don't understand the game play. What are you supposed to do after you make your selection? Just wait it out? [Edit : Okay I get it now. but I would not have known you were supposed to press the spacebar without consulting the code. By the way, I'm not getting the Null object reference error]
__________________
My Tutorials * My Website

Last edited by Mazoonist; 12-29-2008 at 05:28 AM.
Mazoonist is offline   Reply With Quote
Old 12-29-2008, 06:01 AM   #3
Kimchee
Senior Member
 
Join Date: Nov 2008
Location: Provo, UT
Posts: 110
Default

Opps, hit spacebar. You goal is to select the correct percentage as displayed in the right column. After you make a selection you hit spacebar and Ka Pow! According to how close you are to the correct percentage dictares how much health you loose and how many points you get. Sorry about the rushed original post.
Kimchee is offline   Reply With Quote
Old 12-29-2008, 07:27 AM   #4
orange gold
got as?
 
Join Date: May 2007
Posts: 628
Default

on frame 2 replace your code with this code...
ActionScript Code:
import fl.transitions.Tween; import fl.transitions.easing.*; stop(); var level1: Array = [ [1,1,1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1,1,1] ]; var level2: Array = [ [2,2,2,2,11,11,2,2,2,2], [2,2,2,2,11,11,2,2,2,2], [2,2,2,2,11,11,2,2,2,2], [2,2,2,2,11,11,2,2,2,2], [11,11,11,11,11,11,11,11,11,11], [11,11,11,11,11,11,11,11,11,11], [2,2,2,2,11,11,2,2,2,2], [2,2,2,2,11,11,2,2,2,2], [2,2,2,2,11,11,2,2,2,2], [2,2,2,2,11,11,2,2,2,2] ]; var level3: Array = [ [3,3,3,3,3,3,3,3,3,3], [0,0,0,0,0,0,0,0,0,3], [0,3,3,3,3,3,3,3,0,3], [0,3,0,0,0,0,0,3,0,3], [0,3,0,3,3,3,0,3,0,3], [0,3,0,3,0,3,0,3,0,3], [0,3,0,3,0,0,0,3,0,3], [0,3,0,3,3,3,3,3,0,3], [0,3,0,0,0,0,0,0,0,3], [0,3,3,3,3,3,3,3,3,3] ]; var level4: Array = [ [4,5,6,0,6,6,0,6,5,4], [5,6,0,0,6,6,0,0,6,5], [6,0,0,0,5,5,0,0,0,6], [0,0,0,0,5,5,0,0,0,0], [6,6,5,5,4,4,5,5,6,6], [6,6,5,5,4,4,5,5,6,6], [0,0,0,0,5,5,0,0,0,0], [6,0,0,0,5,5,0,0,0,6], [5,6,0,0,6,6,0,0,6,5], [4,5,6,0,6,6,0,6,5,4] ]; var level5: Array = [ [8,8,8,8,8,8,8,8,8,8], [0,8,9,9,9,9,9,9,8,0], [0,0,8,9,9,9,9,8,0,0], [0,0,0,8,9,9,8,0,0,0], [0,0,0,0,8,8,0,0,0,0], [0,0,0,0,8,8,0,0,0,0], [0,0,0,8,9,9,8,0,0,0], [0,0,8,9,9,9,9,8,0,0], [0,8,9,9,9,9,9,9,8,0], [8,8,8,8,8,8,8,8,8,8] ]; var level6: Array = [ [0,0,7,7,7,7,7,7,0,0], [0,7,7,7,7,7,7,7,7,0], [7,7,9,7,7,7,7,9,7,7], [7,8,9,9,7,7,8,9,9,7], [7,7,9,7,7,7,7,9,7,7], [7,7,7,7,7,7,7,7,7,7], [7,7,7,7,7,7,7,7,7,7], [7,7,7,7,7,7,7,7,7,7], [7,7,7,7,7,7,7,7,7,7], [7,7,0,0,7,7,0,0,7,7] ]; var level7: Array = [ [0,0,0,0,0,0,0,10,0,0], [0,0,0,0,0,0,10,10,0,0], [0,0,00,0,0,10,0,10,0,0], [0,0,10,0,0,0,0,10,0,0], [0,0,10,0,0,0,0,10,0,0], [10,10,10,10,10,0,0,10,0,0], [0,0,10,0,0,0,0,10,0,0], [0,0,10,0,0,0,0,10,0,0], [0,0,0,0,0,0,0,10,0,0], [0,0,0,0,0,10,10,10,10,10] ]; var level8: Array = [ [1,1,1,1,1,1,1,1,1,1], [0,2,0,0,0,0,0,0,2,0], [0,2,3,3,3,3,3,3,2,0], [0,2,0,4,0,0,4,0,2,0], [0,2,0,4,0,5,4,0,2,0], [0,2,0,4,5,0,4,0,2,0], [0,2,0,4,0,0,4,0,2,0], [0,2,3,3,3,3,3,3,2,0], [0,2,0,0,0,0,0,0,2,0], [1,1,1,1,1,1,1,1,1,1] ]; var level9: Array = [ [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0] ]; var level10: Array = [ [Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14)], [Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14)], [Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14)], [Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14)], [Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14)], [Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14)], [Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14)], [Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14)], [Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14)], [Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14),Math.ceil(Math.random()*14)] ]; var upComing: int = 1; var spheres: Array = new Array(); var i: int; var target: int; var health: int; var score: int; var itemsTotal: int; var percentage: int; var itemsSelected:int; var selectionSprite:Sprite; var startX:Number; var startY:Number; var myTimer:Timer = new Timer(10000, 0); var timer:int; var tween1:Tween; /*-------------------------------------------------------- --------------------------------------------------------*/ level(level1); init(); function level (currentlevel){ for(var i:int = 0; i < 10; i++){     for(var j:int = 0; j < 10; j++){             if(currentlevel[i][j]==1){                 var blue:blueSphere = new blueSphere();                 blue.x = 25 +j*35;                 blue.y = 30 + i*35;                 addChild(blue);                 spheres.push(blue);             }             else if (currentlevel[i][j] ==2){                 var red:redSphere = new redSphere()                 red.x = 25 +j*35;                 red.y = 30 + i*35;                 addChild(red);                 spheres.push(red);             }             else if (currentlevel[i][j] ==3){                 var green:greenSphere = new greenSphere();                 green.x = 25 +j*35;                 green.y = 30 + i*35;                 addChild(green);                 spheres.push(green);             }             else if (currentlevel[i][j] ==4){                 var orange:orangeSphere = new orangeSphere();                 orange.x = 25 +j*35;                 orange.y = 30 + i*35;                 addChild(orange);                 spheres.push(orange);             }             else if (currentlevel[i][j] ==5){                 var yellow:yellowSphere = new yellowSphere();                 yellow.x = 25 +j*35;                 yellow.y = 30 + i*35;                 addChild(yellow);                 spheres.push(yellow);             }             else if (currentlevel[i][j] ==6){                 var brown:brownSphere = new brownSphere();                 brown.x = 25 +j*35;                 brown.y = 30 + i*35;                 addChild(brown);                 spheres.push(brown);             }             else if (currentlevel[i][j] ==7){                 var purple:purpleSphere = new purpleSphere();                 purple.x = 25 +j*35;                 purple.y = 30 + i*35;                 addChild(purple);                 spheres.push(purple);             }             else if (currentlevel[i][j] ==8){                 var black:blackSphere = new blackSphere();                 black.x = 25 +j*35;                 black.y = 30 + i*35;                 addChild(black);                 spheres.push(black);             }             else if (currentlevel[i][j] ==9){                 var white:whiteSphere = new whiteSphere();                 white.x = 25 +j*35;                 white.y = 30 + i*35;                 addChild(white);                 spheres.push(white);             }             else if (currentlevel[i][j] ==10){                 var multi:multiSphere = new multiSphere();                 multi.x = 25 +j*35;                 multi.y = 30 + i*35;                 addChild(multi);                 spheres.push(multi);             }         }     }     for(i = 0; i < spheres.length; i++) {         spheres[i].glow=false;     }     itemsTotal = 0;     for(i = 0; i < spheres.length; i++) {                 if(spheres[i].alpha ==1){                     itemsTotal++;             }     }     target = 100*Math.ceil((Math.random()*itemsTotal))/itemsTotal;     percentage_txt.text = String(target) + "%";     items_txt.text = String(itemsSelected + "/" + itemsTotal);     for(i = 0; i < spheres.length; i++) {         spheres[i].addEventListener(MouseEvent.CLICK,select);     }    } function init(){     target = Math.round(Math.random()*100+1);     health = 100;     score = 0;     percentage = 0;     itemsSelected = 0;     selectionSprite = new Sprite();     addChild(selectionSprite);     timer = 5;     stage.addEventListener(MouseEvent.MOUSE_DOWN, down);     stage.addEventListener(MouseEvent.MOUSE_UP, up);     stage.addEventListener(KeyboardEvent.KEY_DOWN, kaPow);         myTimer.addEventListener(TimerEvent.TIMER, countDown);     myTimer.start();     gameOver_txt.text = "";         endScore_txt.text = "";         playAgain_txt.text = ""         playAgain_mc.removeEventListener(MouseEvent.CLICK, again);                 //fill in text box. score_txt.text=String(score); percentage_txt.text = String(target); health_txt.text=String(health); timer_txt.text = String(timer); } function countDown(event:TimerEvent):void {     timer -=1;     timer_txt.text = String(timer);     if(timer==0){         endGame();     } }

continued in the next post.... too long of code for this post
__________________
Feel free to PM me if you have a question
orange gold is offline   Reply With Quote
Old 12-29-2008, 07:27 AM   #5
orange gold
got as?
 
Join Date: May 2007
Posts: 628
Default

continued code....

ActionScript Code:
//function ends the game function endGame(){             upComing = 1;         percentage_txt.text = "";         timer_txt.text = "0";         health_txt.text = "0";         items_txt.text = "";         score_txt.text= "";         gameOver_txt.text = "Game Over";         endScore_txt.text = "Total Score: " +score;         playAgain_txt.text = "Play Again"         stage.removeEventListener(MouseEvent.MOUSE_DOWN, down);         stage.removeEventListener(KeyboardEvent.KEY_DOWN, kaPow);         myTimer.removeEventListener(TimerEvent.TIMER, countDown);         playAgain_mc.addEventListener(MouseEvent.CLICK, again);             for(i = spheres.length - 1; i >= 0; i--) {         trace(10);         removeChild(spheres[i]);         spheres.splice(i, 1);     }     gotoAndPlay(5); } function again(evtent:MouseEvent): void{     level(level1);     init();     } //Function for calculating score. function scoreTotal (){     health -= Math.abs(percentage - target);     minusHp_txt.text = "-" + String(Math.abs(percentage - target));     health_txt.text = String(health);     tween1 = new Tween(minusHp_txt, "alpha", None.easeNone, 1, 0, 2                         , true);     if(health<1){         endGame();     }     if(percentage-target >=0  && percentage - target<21 ){        score += 20-(percentage-target);        score_txt.text=String(score);        plusScore_txt.text = "+" + String(20-(percentage-target));        tween1 = new Tween(plusScore_txt, "alpha", None.easeNone, 1, 0, 2, true); }     if(percentage-target <0  && percentage - target>-21 ){        score += 20+(percentage-target);        score_txt.text=String(score);        plusScore_txt.text = "+" + String(20+(percentage-target));        tween1 = new Tween(plusScore_txt, "alpha", None.easeNone, 1, 0, 2, true);     }     setChildIndex(advice_txt,numChildren - 1);     setChildIndex(percentageGot_txt,numChildren - 1);     percentageGot_txt.text = String(percentage) + "%";     tween1 = new Tween(percentageGot_txt, "alpha", None.easeNone, 1, 0, 1, true);     if(percentage - target == 0){         advice_txt.text = "PERFECT!";         tween1 = new Tween(advice_txt, "alpha", None.easeNone, 1, 0, 1, true);     }     else if(3>= Math.abs(percentage-target)){         advice_txt.text = "Great!";         tween1 = new Tween(advice_txt, "alpha", None.easeNone, 1, 0, 1, true);     }     else if(6>= Math.abs(percentage-target) ){         advice_txt.text = "Good!";         tween1 = new Tween(advice_txt, "alpha", None.easeNone, 1, 0, 1, true);     } } //Function when you click the mouse function down(event:MouseEvent):void {     itemsTotal = 0;             for(i = 0; i < spheres.length; i++) {             if(spheres[i].alpha ==1){             itemsTotal++;         }         spheres[i].picked=false;     }         itemsSelected = 0;     selectionSprite.graphics.clear();     startX = this.mouseX;     startY = this.mouseY;     stage.addEventListener(MouseEvent.MOUSE_MOVE, select); } //Function when you move the mouse function select(event:MouseEvent):void {     itemsSelected = 0;         for(var i:int = 0; i < spheres.length; i++){         if(spheres[i].glow == true){             itemsSelected++;         }     }         selectionSprite.graphics.clear();     selectionSprite.graphics.lineStyle(1);     selectionSprite.graphics.beginFill(0xCCCCCC, 0.1);     selectionSprite.graphics.drawRect(startX, startY, mouseX - startX, mouseY - startY);         for(i = 0; i < spheres.length; i++) {         if(selectionSprite.hitTestObject(spheres[i]) && spheres[i].alpha ==1) {             if(spheres[i].picked ==false){                 //makes sphere glow                 if(spheres[i].glow ==false){                     spheres[i].picked=true;                     spheres[i].glow = true;                     spheres[i].gotoAndPlay(6);                     itemsSelected++;                     items_txt.text = String(itemsSelected + "/" + itemsTotal);                 }                 else{                     //if spheres is already glowing when selected makes it not glow.                     spheres[i].picked = true;                     spheres[i].gotoAndPlay(1);                     spheres[i].glow = false;                     itemsSelected--;                     items_txt.text = String(itemsSelected + "/" + itemsTotal);                 }             }         }         else {             //Unselects spheres as you roll off of them.             if(spheres[i].picked == true){                 if(spheres[i].glow == true){                     spheres[i].gotoAndPlay(1);                     spheres[i].glow = false;                     spheres[i].picked = false;                     itemsSelected--;                     items_txt.text = String(itemsSelected + "/" + itemsTotal);                 }                 else{                     spheres[i].glow = true;                     spheres[i].picked = false;                     spheres[i].gotoAndPlay(6);                     itemsSelected++;                     items_txt.text = String(itemsSelected + "/" + itemsTotal);                 }             }         }     } } //function when you let go fo the mouse button. function up(event:MouseEvent):void {     selectionSprite.graphics.clear();     stage.removeEventListener(MouseEvent.MOUSE_MOVE, select); } //When the play hits space bard it sets the glowing spheres; //alph to 0, calculates the percentage you selected and displays //that number and then gives you a score and a new target. function kaPow(event:KeyboardEvent): void {     if(event.keyCode ==32){         for(i = 0; i < spheres.length; i++) {             if(spheres[i].glow ==true){                 spheres[i].alpha = 0;                 spheres[i].glow = false;             }         }         percentage=100*itemsSelected/itemsTotal         //items_txt.text = String(percentage);         itemsTotal=0;             for(i = 0; i < spheres.length; i++) {                 if(spheres[i].alpha ==1){                     itemsTotal++;             }                     }         items_txt.text = "0/" + String(itemsTotal);     }     scoreTotal();     target = 100*Math.ceil((Math.random()*itemsTotal))/itemsTotal;     percentage_txt.text = String(target) + "%";     timer = 5;     timer_txt.text = String(timer);     if( itemsTotal==0){         upComing++;         switch(String(upComing)){         case "1":         level(level1);         break;         case "2":         level(level2);         break;         case "3":         level(level3);         break;                 case "4":         level(level4);         break;         case "5":         level(level5);         break;                 case "6":         level(level6);         break;         case "7":         level(level7);         break;         case "8":         level(level8);         break;         case "9":         level(level9);         break;         case "10":         level(level10);         upComing = 1;         break;         }             }     }

glad i could help
__________________
Feel free to PM me if you have a question
orange gold is offline   Reply With Quote
Old 12-29-2008, 06:02 PM   #6
Kimchee
Senior Member
 
Join Date: Nov 2008
Location: Provo, UT
Posts: 110
Default

Thanks, I guess a simple "add gotoandPlay (5); in the function end game" would have worked too

Still throws a

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at almost_fla::MainTimeline/kaPow(

Message though, and I have looked and looked again at the kaPow function and don't see when it would ever be called after ending the game. So if anyone has insight to this it would help.
Kimchee is offline   Reply With Quote
Old 12-29-2008, 06:22 PM   #7
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 3,160
Send a message via AIM to Mazoonist
Default

Here's a hint: You have to remove listeners when you are done with them. Once you add an event listener, they remain active throughout a timeline unless you remove them. Going to another frame does not remove the listener. But, when you go to another frame, now maybe the listener function refers to an object which is no longer present on the stage, and you get null object reference errors.
__________________
My Tutorials * My Website
Mazoonist is offline   Reply With Quote
Old 12-29-2008, 08:05 PM   #8
Kimchee
Senior Member
 
Join Date: Nov 2008
Location: Provo, UT
Posts: 110
Default

Hahaha! I got it! With much thanks to both of you of course.

Okay this was the bug and I should have known, Order matters.

I was calling a function in the middle of a function that would end the game, but then the rest of the function tried to run, thus throwing the error. I am so jazzed, that took like 10 hours to find! Hahahaha!
Kimchee is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Building a Game Engine (start off simple) Stevenjs86 Gaming and Game Development 2 07-03-2008 10:45 PM
[AS2] qns abt a simple game bochup Gaming and Game Development 3 01-01-2007 12:46 PM
simple game... mnickle Gaming and Game Development 1 02-27-2006 03:13 PM
[AS2] set interval jennys26 Gaming and Game Development 2 02-27-2006 07:23 AM
simple tennis game media_newbie Gaming and Game Development 1 10-22-2002 12:01 PM


All times are GMT. The time now is 02:36 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.