-
Timers and Functions
I've been having trouble in creating a game where I need to disable some buttons so that an animation can play. I've tried using a function to call a timer, but I keep getting a coercion error. Can someone steer me to where I need to go to be able to figure this out?
-
We need your code to help you.
-
Here is the code that I was using
Here is the code that I'm using:
Actionscript Code:
var animationTimer:Timer = new Timer (5000, 1); animationTimer.addEventListener (TimerEvent.Timer, function2);
function function1 (e:MouseEvent):void { animationTimer.start (); }
function function2(e:TimerEvent):void { //the events that restart the game }
-
I cant see an error in this code, is there more code related?
Last edited by regbolD; 08-18-2010 at 04:46 PM.
-
That's the relevant parts. the rest are movie clip stops and gotoAndPlays.....
-
Nothing in the code you posted will throw a coercion error. Post the full error message, and the line that causes the error and any code that tells us more about that line, particularly variable types.
-
Sorry if I've been unclear...I was trying to be concise.
Right now, through tinkering with the code, I'm getting this error at Line 13:
1119: Access of possibly undefined property Timer through a reference with static type Class.
This is the complete code that I have for the game right now.
Actionscript Code:
stop();
var userSelect:String = new String (); var compSelect:String = new String (); var roundResult:String = new String (); var attackStatus:String = new String (); var compNumber:Number = new Number (); var damage:Number = new Number (); var playerHealth:Number = 100; var compHealth:Number = 100;
var animTimer:Timer = new Timer (5000, 1); animTimer.addEventListener (TimerEvent.Timer, roundReset);
p1HealthBar.gotoAndStop(playerHealth); compHealthBar.gotoAndStop(compHealth); damage = 10;
rButton.addEventListener(MouseEvent.CLICK, rSelect); rButton.addEventListener(MouseEvent.MOUSE_OVER, rOver); rButton.addEventListener(MouseEvent.MOUSE_OUT, rOut);
sButton.addEventListener(MouseEvent.CLICK, sSelect); sButton.addEventListener(MouseEvent.MOUSE_OVER, sOver); sButton.addEventListener(MouseEvent.MOUSE_OUT, sOut);
pButton.addEventListener(MouseEvent.CLICK, pSelect); pButton.addEventListener(MouseEvent.MOUSE_OVER, pOver); pButton.addEventListener(MouseEvent.MOUSE_OUT, pOut);
rButton.buttonMode = true; sButton.buttonMode = true; pButton.buttonMode = true;
function rSelect(e:MouseEvent):void { userSelect = "rock"; rButton.enabled = false; rButton.gotoAndStop("selected"); rButton.buttonMode = false; rButton.removeEventListener(MouseEvent.CLICK, rSelect); rButton.removeEventListener(MouseEvent.MOUSE_OVER, rOver); rButton.removeEventListener(MouseEvent.MOUSE_OUT, rOut); pButton.gotoAndStop("disabled"); pButton.enabled = false; pButton.buttonMode = false; pButton.removeEventListener(MouseEvent.CLICK, pSelect); pButton.removeEventListener(MouseEvent.MOUSE_OVER, pOver); pButton.removeEventListener(MouseEvent.MOUSE_OUT, pOut); sButton.gotoAndStop("disabled"); sButton.enabled = false; sButton.buttonMode = false; sButton.removeEventListener(MouseEvent.CLICK, sSelect); sButton.removeEventListener(MouseEvent.MOUSE_OVER, sOver); sButton.removeEventListener(MouseEvent.MOUSE_OUT, sOut); compChoice(); whoWins(); healthTrack(); animTimer.start (); }
function sSelect(e:MouseEvent):void { userSelect = "scissors"; sButton.enabled = false; sButton.gotoAndStop("selected"); sButton.buttonMode = false; sButton.removeEventListener(MouseEvent.CLICK, sSelect); sButton.removeEventListener(MouseEvent.MOUSE_OVER, sOver); sButton.removeEventListener(MouseEvent.MOUSE_OUT, sOut); rButton.gotoAndStop("disabled"); rButton.enabled = false; rButton.buttonMode = false; rButton.removeEventListener(MouseEvent.CLICK, rSelect); rButton.removeEventListener(MouseEvent.MOUSE_OVER, rOver); rButton.removeEventListener(MouseEvent.MOUSE_OUT, rOut); pButton.gotoAndStop("disabled"); pButton.enabled = false; pButton.buttonMode = false; pButton.removeEventListener(MouseEvent.CLICK, pSelect); pButton.removeEventListener(MouseEvent.MOUSE_OVER, pOver); pButton.removeEventListener(MouseEvent.MOUSE_OUT, pOut); compChoice(); whoWins(); healthTrack(); buttonReset (); }
function pSelect(e:MouseEvent):void { userSelect = "paper"; pButton.enabled = false; pButton.gotoAndStop("selected"); pButton.buttonMode = false; pButton.removeEventListener(MouseEvent.CLICK, pSelect); pButton.removeEventListener(MouseEvent.MOUSE_OVER, pOver); pButton.removeEventListener(MouseEvent.MOUSE_OUT, pOut); rButton.gotoAndStop("disabled"); rButton.enabled = false; rButton.buttonMode = false; rButton.removeEventListener(MouseEvent.CLICK, rSelect); rButton.removeEventListener(MouseEvent.MOUSE_OVER, rOver); rButton.removeEventListener(MouseEvent.MOUSE_OUT, rOut); sButton.gotoAndStop("disabled"); sButton.enabled = false; sButton.buttonMode = false; sButton.removeEventListener(MouseEvent.CLICK, sSelect); sButton.removeEventListener(MouseEvent.MOUSE_OVER, sOver); sButton.removeEventListener(MouseEvent.MOUSE_OUT, sOut); compChoice(); whoWins(); healthTrack (); buttonReset (); }
function compChoice(e:MouseEvent=null):void { compNumber = Math.floor (Math.random ()*2); if (compNumber == 0) { compSelect = "rock"; } else if (compNumber == 1) { compSelect = "paper"; } else if (compNumber == 2) { compSelect = "scissors"; } }
function whoWins(e:MouseEvent = null):void { if (userSelect == "rock" && compSelect == "rock") { roundResult = "draw"; drawAttackMovie.gotoAndPlay (2); } else if (userSelect == "rock" && compSelect == "paper") { roundResult = "lose"; compAttackMovie.gotoAndPlay ("paperAttack"); } else if (userSelect == "rock" && compSelect == "scissors") { roundResult = "win"; playerAttackMovie.gotoAndPlay ("rockAttack"); } else if (userSelect == "paper" && compSelect == "rock") { roundResult = "win"; playerAttackMovie.gotoAndPlay ("paperAttack"); } else if (userSelect == "paper" && compSelect == "paper") { roundResult = "draw"; drawAttackMovie.gotoAndPlay (2); } else if (userSelect == "paper" && compSelect == "scissors") { roundResult = "lose"; compAttackMovie.gotoAndPlay ("scissorAttack"); } else if (userSelect == "scissors" && compSelect == "rock") { roundResult = "lose"; compAttackMovie.gotoAndPlay ("rockAttack"); } else if (userSelect == "scissors" && compSelect == "paper") { roundResult = "win"; playerAttackMovie.gotoAndPlay ("scissorAttack"); } else if (userSelect == "scissors" && compSelect == "scissors") { roundResult = "draw"; drawAttackMovie.gotoAndPlay (2); } }
function healthTrack(e:MouseEvent = null):void { if (roundResult == "win") { playerHealth = playerHealth; compHealth = compHealth - damage; p1HealthBar.gotoAndStop(playerHealth); compHealthBar.gotoAndStop(compHealth); } else if (roundResult == "lose") { playerHealth = playerHealth - damage; compHealth = compHealth; p1HealthBar.gotoAndStop(playerHealth); compHealthBar.gotoAndStop(compHealth); } else if (roundResult == "draw") { playerHealth = playerHealth; compHealth = compHealth; p1HealthBar.gotoAndStop(playerHealth); compHealthBar.gotoAndStop(compHealth); } if (playerHealth <= 0) { loseMovie.gotoAndPlay(2); } else if (compHealth <= 0) { youWinMovie.gotoAndPlay(2); } else if (playerHealth && compHealth <=0) { drawMovie.gotoAndPlay(2); } }
function roundReset (e:MouseEvent=null):void { drawAttackMovie.gotoAndStop("blank"); playerAttackMovie.gotoAndStop ("blank"); compAttackMovie.gotoAndStop ("blank"); }
function buttonReset(e:MouseEvent = null):void { rButton.addEventListener(MouseEvent.CLICK, rSelect); rButton.addEventListener(MouseEvent.MOUSE_OVER, rOver); rButton.addEventListener(MouseEvent.MOUSE_OUT, rOut); rButton.gotoAndStop(1);
sButton.addEventListener(MouseEvent.CLICK, sSelect); sButton.addEventListener(MouseEvent.MOUSE_OVER, sOver); sButton.addEventListener(MouseEvent.MOUSE_OUT, sOut); sButton.gotoAndStop(1);
pButton.addEventListener(MouseEvent.CLICK, pSelect); pButton.addEventListener(MouseEvent.MOUSE_OVER, pOver); pButton.addEventListener(MouseEvent.MOUSE_OUT, pOut); pButton.gotoAndStop(1); }
function rOver(e:MouseEvent):void { rButton.height = rButton.height + 5; rButton.width = rButton.width +5; }
function rOut(e:MouseEvent):void { rButton.height = rButton.height -5; rButton.width = rButton.width -5; }
function sOver(e:MouseEvent):void { sButton.height = sButton.height +5; sButton.width = sButton.width + 5; }
function sOut(e:MouseEvent):void { sButton.height = sButton.height -5; sButton.width = sButton.width -5; }
function pOver(e:MouseEvent):void { pButton.height = pButton.height +5; pButton.width = pButton.width + 5; }
function pOut(e:MouseEvent):void { pButton.height = pButton.height -5; pButton.width = pButton.width -5; } }
-
TimerEvent.Timer should be TimerEvent.TIMER.
On further scanning, roundReset expects a MouseEvent, but will recieve a TimerEvent. If it needs to react to both, then have it take a plain Event.
-
That seemed to work. Thanks a lot!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|