Ultrashock Forums > Flash > ActionScript
Game Logic: Whack-a-mole

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
Game Logic: Whack-a-mole
Old 2009-11-25

Hey:

I am creating a whack-a-mole type game and have run into an issue with a pause functionality i am adding.

There will be a bonus round in the middle of the game if the player gets enough of a certain type of item. The pause works fine in the regular round, but when you enter the bonus round, i can't seem to grasp the logic of what i'm doing.

Here are the init and start functions:
Code:
function initLighteningRound(evt:MouseEvent=null):void{
	timeHolder = time;
	isLighteningRound = true;
	display_mc.removeEventListener(MouseEvent.CLICK, initLighteningRound);
	startMonitorAnim();
	addChild(score_lightening);
	/*var paramsArray = new Array(lighteningRound);
	TweenLite.to(display_mc, 1, {alpha:0, onComplete:removeText, onCompleteParams:[paramsArray]});*/
	display_mc.buttonMode = false;
	
	if(lighteningRound_1 == true){
		var removeDisplayObj2:DisplayObject = display_mc.getChildByName("lightening2") as MovieClip;
		display_mc.removeChild(removeDisplayObj2);
	} else {
		var removeDisplayObj1:DisplayObject = display_mc.getChildByName("lightening1") as MovieClip;
		display_mc.removeChild(removeDisplayObj1);
		lighteningRound_1 = true;
	}
	
	elevator.gotoAndPlay(3);
	time = 0;
	timeLimit_lighteningRound = 10;
	totalEmployeeNum = 21;
	availableEmployee = [];
	
	//timer for employees
	employeeMaker1 = new Timer(300, timeLimit);
	//timer for clock
	timer = new Timer(1000, timeLimit_lighteningRound);
	//timer for pizza
	//pizzaTimer = new Timer(1000, timeLimit_lighteningRound);
	
	//build the employee and available employee arrays
	for(var j:uint = 0; j < totalEmployeeNum; j++){
		var employee:MovieClip;
		var employeeName:String = "employee"+j;
		employee = this[employeeName];
		
		employeeArray.push(employee);
		availableEmployee.push(employeeName);
	}
	
	//start the timers
	employeeMaker1.addEventListener(TimerEvent.TIMER, startLighteningRound);
	employeeMaker1.start();
	timer.addEventListener(TimerEvent.TIMER, timeLimitHandler);
	timer.start();
	//pizzaTimer.addEventListener(TimerEvent.TIMER, pizzaTimeLimitHandler);
	//pizzaTimer.start();
}

function startLighteningRound(event:TimerEvent):void{
	//trace("lightening round");
	
	Mouse.hide();
	stage.addChild(cursor_mc);
	
	var randomEmployeeNum:int = Math.random() * availableEmployee.length;
	//create a string
	var employee_str:String = availableEmployee[randomEmployeeNum];
	//cast the string as a mc
	var employee_obj:Object = getDefinitionByName(employee_str);
	//instantiate mc
	var employee_mc:MovieClip = new employee_obj();
	
	var cubicle_mc:MovieClip;
	//calculate random cubicle #
	var randomCubicleNum:uint = Math.random() * availableCubicle.length;
	var cubicle_str:String = availableCubicle[randomCubicleNum];
	cubicle_mc = this[cubicle_str];
	
	//add employee to cubicle
	cubicle_mc.empty_mc.addChild(employee_mc);
	//animate employee
	cubicle_mc.empty_mc.addChild(employee_mc).animateEmployee_lighteningRound();
	//delete current employee and cubicle from the array
	employee_mc.spliced_cubicle = availableCubicle.splice(randomCubicleNum, 1);
	employee_mc.spliced_employee = availableEmployee.splice(randomEmployeeNum, 1);
}
and here is some of the Class i am referencing:
Code:
var isPaused_up1:TweenLite;
		var isPaused_down1:TweenLite;
		var isPaused_up2:TweenLite;
		var isPaused_down2:TweenLite;
		var isPaused_lr:TweenLite;;
		var isPaused_out:TweenLite;
		var animOut:Boolean = false;
		var lighteningRound:Boolean = false;

public function animateEmployee_lighteningRound():void{
			lighteningRound = true;
			trace("lightengingRound = "+lighteningRound);
			isPaused_lr = new TweenLite(this, .5, {y:-109, ease:Elastic.easeInOut, paused:false});
			TweenLite.delayedCall(1.8, animateOutEmployee);
		}

		public function animateOutEmployee():void{
			trace("this is "+this);
			//lighteningRound = false;
			//animOut = true;
			isPaused_out = new TweenLite(this, .5, {y:0, ease:Back.easeIn, onComplete:deleteEmployee, paused:false});
			this.gotoAndStop(2);
		}
		
		private function deleteEmployee():void{
			//trace("employee "+this+" has been deleted!");
			//trace(this);
			//trace(this.parent);
			//trace(this.parent.parent);
			animOut = false;
			MovieClip(this.parent.parent.parent).updateArray(this);
			this.parent.removeChild(this);
		}
		
		public function pauseAnim():void{
			trace("pauseAnim");
			if(lighteningRound == true){
				//trace("this is "+this);
				//MovieClip(this.parent.parent.parent).stopMonitorAnim();
				trace("lighteningRound pause");
				isPaused_lr.pause();
			} else if(animOut == true){
				trace("animOut pause");
				isPaused_out.pause();
			} else {
				trace("pause +"+lighteningRound);
				isPaused_down1.pause();
				isPaused_up1.pause();
				isPaused_down2.pause();
				isPaused_up2.pause();
			}
		}
		
		public function resumeAnim():void{
			trace("pauseAnim");
			if(lighteningRound == true){
				//trace("this is "+this);
				//MovieClip(this.parent.parent.parent).stopMonitorAnim();
				isPaused_lr.resume();
			} else if(animOut == true){
				isPaused_out.resume();
			} /*else { 
				isPaused_down1.resume();
				isPaused_up1.resume();
				isPaused_down2.resume();
				isPaused_up2.resume();
			}*/
		}
I can't seem to get lighteningRound to be true for the pauseAnim method.

Thanks
postbit arrow 3 comments | 170 views postbit arrow Reply: with Quote   
Registered User
trowley is offline
seperator
Posts: 139
2007-03-23
seperator

Ultrashock Member Comments:
trowley trowley is offline 2009-11-25 #2 Old  
Well, I am confused about when things are listed in the DisplayList. It seems that the object returns a null error when i try to pause it. However, when i trace anything in the display list (before running the pause function) it seems to trace correctly:

Code:
trace("empty_mc = "+displayName);//this traces correctly(i think)
if(displayName !=null){
	MovieClip(displayName).pauseAnim();//this causes a null error
}
Any ideas?
Reply With Quote  
trowley trowley is offline 2009-11-30 #3 Old  
So, still confused.

It seems that when i test, i can pause in both states (up or down), however, it only works on some of the mcs at a time. I guess they are (at the time when i hit the pause) not part of the display list.

When i just run a trace of the for loop i get (there are nine total mcs to run through):
Code:
displayName = null //1
displayName = [object employee5] //2
[object employee5] not null
displayName = [object employee25] //3
[object employee25] not null
displayName = [object employee23] //4
[object employee23] not null
displayName = null //5
displayName = [object employee23] //6
[object employee23] not null
displayName = [object employee13] //7
[object employee13] not null
displayName = [object employee0] //8
[object employee0] not null
displayName = null //9
So, why would the the pause method only work on a selected few at a time and ignore the others?!!
Reply With Quote  
trowley trowley is offline 2009-12-01 #4 Old  
ok, very confused about DisplayObject and how that works:

Code:
var displayName:DisplayObject = cubicle_mc.empty_mc.getChildByName("employeeName") as MovieClip;
trace("displayName = "+displayName);
if(displayName !=null){
	//MovieClip(displayName).pauseAnim();
	trace(displayName+" not null");
}
Everything traces correctly (whatever is displaying at the time of the click, traces as not null)

Code:
displayName = [object employee17]
[object employee17] not null
displayName = [object employee23]
[object employee23] not null
displayName = [object employee11]
[object employee11] not null
displayName = [object employee15]
[object employee15] not null
displayName = [object employee8]
[object employee8] not null
displayName = [object employee1]
[object employee1] not null
displayName = [object employee4]
[object employee4] not null
displayName = null
displayName = [object employee25]
[object employee25] not null
However, if you run the commented code:
Code:
MovieClip(displayName).pauseAnim();
It starts tracing correctly but then displays:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
and stops

Does that make sense?
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: