A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Cannot remove objects upon game completion.

  1. #1
    Junior Member
    Join Date
    Dec 2014
    Posts
    2

    Cannot remove objects upon game completion.

    So, I have created a small drag and drop game featuring my kids.
    Basically, there are toys everywhere and you have to drag them to a toy box to make them disappear.
    I have everything working almost perfect but cannot figure out one thing.
    I want to have a "Try Again" option that lets you start the game over.
    If there are toys still on the stage, I want to remove them all.
    Then, pressing "Try Again" will reset everything and re-add the toys so everything can start again.
    I have reset the timer and score but cannot get the objects from an array to be removed from the stage.

    Here is my code ...
    Code:
    package  {
    	
    	import flash.display.MovieClip;
    	import flash.events.Event;
    	import flash.events.MouseEvent;
    	import flash.text.TextField;
    	import flash.text.TextFormat;
    	import flash.utils.Timer;
    	import flash.events.TimerEvent;
    	import flash.text.Font;
    	import flash.filters.GlowFilter;
    		
    	
    	public class MainGame extends MovieClip {
    		
    			const BG_SPEED:int = 5;
    			const BG_MIN:int =  -550;
    			const BG_MAX:int = 0;
    			const PBG_SPEED:int = 3;			
    			
    			var bg:BackGround = new BackGround;	
    			var paraBg:ParaBg = new ParaBg;
    			var toybox:TargetBox = new TargetBox;
    			var toy:Toy = new Toy;
    			var tryAgain:TryAgain = new TryAgain;
    			var cheer:Cheer = new Cheer;
    			var eightBit:EightBit = new EightBit;
    			var countDown:Number = 30;
    			var myTimer:Timer = new Timer(1000, 30);
    			var myText:TextField = new TextField;
    			var myText2:TextField = new TextField;
    			var myTextFormat:TextFormat = new TextFormat;
    			var myTextFormat2:TextFormat = new TextFormat;
    			var font1:Font1 = new Font1;
    			var kids:Kids = new Kids;
    			var count:int = 0;
    			var finalScore:int = 0;
    			
    			
    			
    			var score:Number = 0;
    			
    			var toy1:Toy1 = new Toy1;
    			var toy2:Toy2 = new Toy2;
    			var toy3:Toy3 = new Toy3;
    			var toy4:Toy4 = new Toy4;
    			var toy5:Toy5 = new Toy5;
    			var toy6:Toy6 = new Toy6;
    			var toy7:Toy7 = new Toy7;
    			var toy8:Toy8 = new Toy8;
    			var toy9:Toy9 = new Toy9;
    			var toy10:Toy10 = new Toy10;
    			var toy11:Toy11 = new Toy11;
    			var toy12:Toy12 = new Toy12;
    			var toy13:Toy13 = new Toy13;
    			var toy14:Toy14 = new Toy14;
    			var toy15:Toy15 = new Toy15;
    			var toy16:Toy16 = new Toy16;
    			var toy17:Toy17 = new Toy17;
    			var toy18:Toy18 = new Toy18;
    			var toy19:Toy19 = new Toy19;
    			var toy20:Toy20 = new Toy20;
    						
    			var toyArray:Array = new Array(toy1, toy2, toy3, toy4, toy5, toy6, toy7, toy8, toy9, toy10, toy11, toy12, toy13, toy14, toy15, toy16, toy17, toy18, toy19, toy20);
    			
    		public function mainGame():void
    		{
    			eightBit.play(0, 9999);
    			addChildAt(paraBg, 0);
    			addChildAt(bg, 1);	
    			addChildAt(kids, 2);
    			kids.x = 310;
    			kids.y = 200;
    			addChild(toy);
    			toy.x = 306;
    			toy.y = 133;
    			addChild(toybox);
    			toybox.x = 295;
    			toybox.y = 90;
    		
    		function addToys(xpos:int, ypos:int)
    			{				
    				addChild(toyArray[i]);
    				toyArray[i].x = xpos;
    				toyArray[i].y = ypos;	
    			}			
    			for (var i:int = 0; i < toyArray.length; i++)
    			{
    				addToys(1140 * Math.random() + 20, 170 * Math.random() + 230);					
    			}
    		
    			bg.addEventListener(Event.ENTER_FRAME, bgScroll);		
    		}
    		
    			public function bgScroll (e:Event)
    			{
    				stage.addEventListener(MouseEvent.MOUSE_UP, arrayDrop);
    				myTimer.addEventListener(TimerEvent.TIMER, countdown);
    				myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
    				myTimer.start();
    
    				e.target.addEventListener(Event.ENTER_FRAME, collision);
    				
    				if (stage.mouseX > 600 && bg.x > BG_MIN)
    				{
    					bg.x -= BG_SPEED;	
    					paraBg.x -= PBG_SPEED;
    					
    					for (var m:int=0; m< toyArray.length; m++)
    					{
    					(toyArray[m] as MovieClip).x -=BG_SPEED
    					}
    				}
    				else if (stage.mouseX < 50 && bg.x < BG_MAX)
    				{
    					bg.x += BG_SPEED;
    					paraBg.x += PBG_SPEED;
    					for (var j:int=0; j< toyArray.length; j++)
    					{
    					(toyArray[j] as MovieClip).x +=BG_SPEED
    					}							
    				}
    				for (var k:int = 0; k < toyArray.length; k++)
    				{
    					toyArray[k].addEventListener(MouseEvent.MOUSE_DOWN, arrayGrab);
    				}			
    				
    			} // End of BGScroll
    			
    		public function collision (e:Event)
    				{			
    					for (var l:int=0; l< toyArray.length; l++)
    						{						
    							if (toyArray[l].hitTestObject(toy))
    							{
    								e.target.removeEventListener(Event.ENTER_FRAME, collision);
    								removeChild(toyArray[l]);
    								toyArray[l].x=100000;								
    								toybox.gotoAndPlay(2);
    								cheer.play(1, 1);
    								score = score + 10;
    								trace(score);
    							}	
    							if (score == 200)
    							{
    								timerDone();
    								myTimer.stop();						
    							}
    							
    						}
    				}
    							
    		public function arrayGrab(e:MouseEvent)
    				{
    					e.target.startDrag();
    				}
    			
    		public function arrayDrop(e:MouseEvent)
    				{
    					stopDrag();
    				}
    				
    		public function countdown(e:TimerEvent):void
    			{ 			
    			
    				if (countDown > 0)
    				{
    					countDown--;
    				}
    				if (countDown < 10)
    				{
    					myText.text = "0" + countDown.toString();
    					myText.x = 270;
    					displayText();
    				}
    				else if (countDown < 20 && countDown > 9)
    				{
    					myText.text = countDown.toString();
    					myText.x = 280;
    					displayText();
    				}
    				else
    				{
    					myText.text = countDown.toString();
    					myText.x = 270;
    					displayText();
    				}			
    				
    			} // end of countdown function
    			
    			public function displayText():void
    				{	
    					myText.filters = [new GlowFilter(0x00FF00, 1.0, 5, 5, 4)];
    					addChild(myText);
    					myText.width = 500, myText.height = 50, myText.y = 10;		
    					myTextFormat.size = 50, myTextFormat.font = font1.fontName;					
    					myText.setTextFormat(myTextFormat);
    				}
    			
    			public function displayText2():void
    				{	myText2.filters = [new GlowFilter(0xFF0000, 1.0, 5, 5, 4)];
    					addChild(myText2);
    					myText2.width = 500, myText2.height = 35, myText2.x = 204, myText2.y = 200;		
    					myTextFormat2.size = 30, myTextFormat2.font = font1.fontName;					
    					myText2.setTextFormat(myTextFormat2);
    				}
    				
    				public function timerDone(e:TimerEvent=null):void
    				{							
    					if (countDown == 0)
    					{
    						count = 0;
    						finalScore = score;
    					}
    					else
    					{					
    						count = (30) - (myTimer.currentCount);
    						finalScore = (count * 10) + (score);
    					}					
    					myText.text = "GAME OVER!";
    					myText.x = 195;
    					displayText();							
    					myText2.text = "Your score = " + (finalScore);
    					displayText2();				
    					addChild(tryAgain);
    					tryAgain.x = 300;
    					tryAgain.y = 300;
    					tryAgain.addEventListener(MouseEvent.CLICK, resetGame);
    				}
    				
    				public function resetGame(e:Event):void {
       				trace("CLICK");
    				countDown = 30;
    				myText.text = "0" + countDown.toString();
    				myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
    				myTimer.reset();
    				removeChild(tryAgain);
    				removeChild(myText2);
    				score=0;
    				
    				
    				}
    		
    	
    	} // End of class
    	
    } //End of package
    And it is all called by another .as file here ...
    Code:
    package  {
    	
    	import flash.display.MovieClip;
    	import MainGame;
    		
    	
    	public class MyGame extends MovieClip {
    		
    		public function MyGame()
    			{
    				var game:MainGame = new MainGame();
    				addChild(game);
    				game.mainGame();
    				
    				
    			}
    
    	}
    }
    I have asked in other forums and haven't found a solution that works yet.
    I have had it suggested that I loop through the array again and then removeChild ... but I couldn't get that to work.
    What am I doing wrong?!?!
    Can anyone help me? I so wanted this finished by Christmas!
    My daughter has been itching to see this game with her and her brother in!
    I'd love to be able to show her it soon.

    Help me, ActionScript gurus ... you're my only hope!

  2. #2
    Junior Member
    Join Date
    Dec 2014
    Posts
    2
    Nevermind ... solved it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center