AS3 timer works in game - actionscript-3

I have a countdown timer I made in AS3 the start, stop, and reset works fine in the game.
Problem: This Flash app is now inserted into an external virtual-life game as some animated billboard/sign. Each "person" in the game sees the sign but must click the (Flash) button, inside the sign, to start the timer code. Only the person pressing button can see it working. Everyone is given 05:00 minutes to present their offer.
I need everyone in a room to see timer countdown since its for a auction.
Any help I would appreciate it.
This is what I tried to use so far:
package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
import flash.ui.Mouse;
public class timerClass extends MovieClip
{
var myTimer:Timer = new Timer(1000, 300);
var i:Number = 300;
public function timerClass()
{
//# constructor code
timerTxt.text = String("05:00");
myTimer.addEventListener(TimerEvent.TIMER, updateTime);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, TimerComplete);
startbutton.addEventListener(MouseEvent.CLICK, StartNow);
pausebutton.addEventListener(MouseEvent.CLICK, PauseNow);
restartbutton.addEventListener(MouseEvent.CLICK, restartNow);
}
private function updateTime(e:TimerEvent)
{
i--;
var totalSeconds:* = i;
var minutes:* = Math.floor(totalSeconds/60);
var seconds:* = totalSeconds % 60;
if(String(minutes).length < 2)
{
minutes = "0" + minutes;
if(String(seconds).length < 2)
seconds = "0" + seconds;
}
timerTxt.text = minutes + ":" + seconds;
}
private function TimerComplete(e:TimerEvent)
{
messageTxt.text = "PRESENTATION IS NOW OVER"
timerTxt.text = String("00:00");
}
private function StartNow(e:MouseEvent)
{ myTimer.start(); }
private function PauseNow(e:MouseEvent)
{ myTimer.stop(); }
private function restartNow(e: MouseEvent): void
{
myTimer.stop();
myTimer = new Timer(1000, 300);
myTimer.addEventListener(TimerEvent.TIMER, updateTime);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, TimerComplete);
i = 300;
messageTxt.text = "";
timerTxt.text = String("05:00");
}
} //#end Class
} //#end Package
I need everyone in a room to see timer countdown since its for a auction
This is what timer should look like, I just need eveyone to see counter at same time once i press start.
https://cldup.com/cds0PwoS5Y.swf
thank you
jln

Related

why am I getting 1136: Incorrect number of arguments. Expected 0

Beginning issue: I am continuously getting an error 1136 at var timer:Timer=new Timer(10000,1);. I am not sure if its a computer error or what. I know you put (delay, and then time interval) for Timer, but it still gives me the error message.
update: I uploaded the whole code from the original post to see if anyone could find any errors. My Timers should be correct but I am still getting the error 1136 on my program and it won't even run. When I do take out the timers, the code works perfectly fine.
Is there any other way to. implement a timer to make the game stop and say game over?
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.text.*;
public class Duckhunt extends MovieClip
{
private var player1:Player;
private var counter:Number;
private var points:Number;
private var cursor:Cursor;
private var duckArmy:Vector.<Duck1>;
private var duckArmy2:Vector.<Duck2>;
private var duckCounter:Number;
private var duckCounter2:Number;
private var count:Number=10;
private var timer:Timer;
//var countDownDec:Number=1;
//private var myTimer:Timer=new Timer(0,count);
public function Duckhunt()
{
//# constructor code
// creates a new five-second Timer
var timer:Timer=new Timer(10000,1);
//add event listner to timer
timer.start();
//starts the timer
//myTimer.start();
player1=new Player();
player1.x = 375; player1.y = 400; addChild(player1);
//cursor crosshair
cursor= new Cursor();
cursor.x = 400; cursor.y = 200; addChild(cursor);
//add enemy
duckArmy=new Vector.<Duck1>();
for(var i:Number = 0; i < 30; i++)
{
var duck:Duck1 = new Duck1(400,0);
duckArmy.push(duck);
duck.y = 450;
stage.addChild(duck);
duck.gotoAndPlay("fly");
duck.addEventListener(MouseEvent.CLICK, hitEnemy);
}//end duck1 army
//add second second enemy
duckArmy2=new Vector.<Duck2>();
for(var j:Number=0;j<30;j++)
{
var duck2:Duck2 = new Duck2(0,400);
duckArmy2.push(duck2);
duck2.x = -900;
stage.addChild(duck2);
duck2.gotoAndPlay("fly");
duck2.addEventListener(MouseEvent.CLICK, hitEnemy);
}//end duck2 army
duckCounter = duckCounter2 =counter = points = 0;
addEventListener(Event.ENTER_FRAME, frameMovement);
}//end constructor
}//end class
}//end package
If you've declared your :Timer object as a public or private variable:
private var timer:Timer;
There's no need for declaring yet another new variable (with same name) in later functions;
var timer:Timer=new Timer(10000,1); //creates a new 2nd var.. causes error..
timer.start();
Shoud be:
timer = new Timer (10000,1); //uses existing private var
timer.start();
Also note:
That timer variable name is too similar to the Timer datatype name, consider changing the name's spelling to avoid "clashing" with built-in system names. Never do var int :int = 0;.
Try naming as _timer :Timer; or even myTimer :Timer;

Why are my timer and score count not working?

I am trying to create a game using action script 3 and can't understand why the following code is resulting in my score immediately resetting to 0 and my timer rapidly changing numbers with no consistent pattern? Any help is much appreciated! Thank You!
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
var score:int=0;
var nCount:Number = 5;
var myTimer:Timer = new Timer(3000, nCount);
timer_txt.text = nCount.toString();
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();
function countdown(e:TimerEvent):void{
nCount--;
timer_txt.text = nCount.toString();
}
init();
function init(): void {
Mouse.hide();
addEventListener(Event.ENTER_FRAME, update);
addEventListener(MouseEvent.CLICK, checkIfHit);
}
function update(myEvent:Event):void{
aim_mc.x = this.mouseX;
aim_mc.y = this.mouseY;
score_txt.text = "Score: " + score;
}
function checkIfHit(e:MouseEvent):void{
for(var i:int = 1; i < 4;++i){
var myClip:MovieClip = MovieClip(getChildByName("duck" + i));
if (myClip.hitTestPoint(mouseX,mouseY,true)){
score = score + 1;
}
}
}
As mentioned by #coner in his comment, I think that you have another frame that, maybe, you have added by mistake, and that's why your animation is initialized everytime :
To avoid that, if you need that frame (these frames) for any reason, you can add a stop() in your first one and then you can use gotoAndStop() or gotoAndPlay() in another time to change the frame, or if you don't need that frame and you've added by mistake, you have just to remove it.
Also, don't forget to remove the event listeners and to stop your game after that your countdown is finished ...
Hope that can help.

Unable to stop spawning an enemy

Okay so here's the dealio.
The code below is what I use to spawn a single enemy periodically in my horror game. He spawns at the window every 10 seconds, then every 6 seconds later, he enters the room and you're essentially dead already, because 5 seconds after that you get a lovely jumpscare. The moment the enemy shifts from the window to the bedroom is when I want the window enemy to stop spawning.
EnemyShipTimer is the timer that spawns the enemy.
enemyTimer is the timer that counts how long the enemy has been at
the window, and then in the room.
In the main file, I've attempted to get rid of the enemy a number of ways, to no effect.
So function gtfo is my latest attempt at removing the enemy, by trying to stop the spawning from the class file itself. It still hasn't worked. Maybe I need to somehow remove the class file from my game, but I have no idea how. Please help! ;A;
PS, countClick counts how many clicks you've done to make the enemy go away, although when I make him go away, room enemy still spawns :c
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.utils.getTimer;
import flash.events.TimerEvent;
import flash.text.TextField;
public class EnemyShip extends MovieClip
{
private var clickCount:int = 0;
private var enemyShipTimer:Timer;
private var enemyTimer:Timer;
public function EnemyShip()
{
this.x = 900;
this.y = 214;
addEventListener(MouseEvent.CLICK, addClick);
addEventListener(MouseEvent.CLICK, countclick);
addEventListener(TimerEvent.TIMER, gtfo);
}
function gtfo(e:TimerEvent):void{
if (enemyTimer.currentCount==5)
enemyShipTimer.stop();
stage.removeChild(this);
}
function addClick(event:MouseEvent):void
{
clickCount = clickCount + 1;
trace("Clickage : " + clickCount);
}
function countclick(event:MouseEvent):void
{
if (clickCount ==4)
{
stage.removeChild(this);
}
}
}
}
The following is the relevant main timeline code that may be clashing with the code (I have updated the package class as per #Karma 's helpful instructions c: though the enemy still spawns; I think what the problem is now is that the timer functions clash with timer functions I've put in my main timeline, and I attempted to sort of move them into the package class but I don't know how to add the necessary movieclips so that I won't get access of undefined property errors so I separated the two once more
enemy is the window spawning enemy.
mosnta is the room spawning enemy.
sod is the screen of death (game over screen) which does not play at all.
var enemyShipTimer:Timer;
function sendEnemy(e:Event)
{
var enemy = new EnemyShip();
stage.addChild(enemy);
var enemytimer = new Timer (1000, count);
enemytimer.start();
enemyShipTimer = new Timer(10000);
enemyShipTimer.addEventListener("timer", sendEnemy);
enemyShipTimer.start();
enemytimer.addEventListener(TimerEvent.TIMER, countdowndesu);
function countdowndesu(e:Event):void{
if (enemytimer.currentCount>5){
trace("ur ded");
enemytimer.removeEventListener(TimerEvent.TIMER, countdowndesu);
mosnta.visible=true;
enemy.visible=false;
if (enemytimer.currentCount==8){
sod.visible=true;
sod.gotoAndPlay(2);
}
}
}
}
Not really understanding the dynamics of how your game works, but here's an altered version of your code with working Timers. Maybe this will help you figure out how to fix your issues
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class EnemyShip extends MovieClip
{
private var clickCount:int = 0;
private var enemyShipTimer:Timer;
private var enemyTimer:Timer;
public function EnemyShip()
{
this.x = 900;
this.y = 214;
//only need one click handler
addEventListener(MouseEvent.CLICK, addClick);
//Instantiate Timers
enemyShipTimer = new Timer(10000); //10 seconds
enemyTimer = new Timer(6000); //6 seconds
//add TimerEvent listeners onto Timer Objects
enemyShipTimer.addEventListener(TimerEvent.TIMER, gtfo);
enemyTimer.addEventListener(TimerEvent.TIMER, enemyTimerHandler);
//Start the timers
enemyShipTimer.start();
enemyTimer.start();
}
//This gets called every 10 seconds
private function gtfo(e:TimerEvent):void
{
trace("enemy spawns at window");
if (enemyTimer.currentCount == 5) {
enemyShipTimer.stop();
stage.removeChild(this);
}
}
//This gets called every 6 seconds
private function enemyTimerHandler(e:TimerEvent):void
{
trace("enemy moves to room");
}
private function addClick(event:MouseEvent):void
{
clickCount ++;
trace("Clickage : " + clickCount);
if (clickCount == 4)
{
stage.removeChild(this);
}
}
}
}

Updating and displaying a variable in AS3

I have been following an AS3 tutorial for an avoider game, the trouble is the tutorial is for CS5 and I am using Flash Develop. I am completely stuck trying to update and display the score. I have read through several posts on SO and elsewhere and still can't seem to fix it.
Here is the Score class that I call to display the score and it contains the function that updates the "score" value.
package
{
import flash.display.Sprite;
import flash.events.TextEvent;
import flash.text.TextField
import flash.text.TextFormat
public class Score extends Sprite
{
public var scoreDisplay:String;
public var currentValue:int;
public function Score()
{
updateDisplay()
}
public function updateDisplay():void
{
scoreDisplay = currentValue.toString();
var format:TextFormat = new TextFormat();
format.size = 25;
format.font = "Verdana"
var myText:TextField = new TextField();
myText.defaultTextFormat = format;
myText.text = scoreDisplay;
myText.background = true;
myText.autoSize
myText.width = 50;
myText.height = 35;
addChild(myText)
myText.x = 340
myText.y = 10
myText.mouseEnabled = false
}
public function addToValue( amountToAdd:Number ):void
{
trace("addToValue")
trace(currentValue)
currentValue = currentValue + amountToAdd;
trace(currentValue + " 1")
}
}
}
and here is the Game class I am running to increase the "score" per tick.
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.Timer;
import Score;
public class Game extends Sprite
{
public var army:Array;
public var gameTimer:Timer;
public var player:Player;
public var background:Sprite;
public function Game()
{
army = new Array();
var newenemy:Enemy = new Enemy(100, -15);
army.push(newenemy);
addChild(newenemy);
player = new Player();
addChild(player);
player.x = mouseX;
player.y = mouseY;
gameTimer = new Timer(20);
gameTimer.addEventListener(TimerEvent.TIMER, move);
gameTimer.addEventListener(TimerEvent.TIMER, onTick);
gameTimer.start();
}
private function move(timerEvent:TimerEvent):void
{
player.x = mouseX;
player.y = mouseY;
for each (var enemy:Enemy in army)
{
enemy.moveDownABit();
if (player.hitTestObject(enemy))
{
gameTimer.stop();
dispatchEvent( new AvatarEvent( AvatarEvent.DEAD));
}
}
}
public function onTick(e:Event):void
{
if (Math.random() < .1)
{
var randomX:Number = Math.random() * 400;
var newEnemy:Enemy = new Enemy(randomX, -15);
army.push(newEnemy);
addChild(newEnemy);
var instanceScore:Score = new Score();
instanceScore.addToValue(10)
}
}
}
}
My trace function outputs "addToValue, 0, 10 1" and repeats that, never changing the 'curentValue' variable past 0. I can tell that the 'amountToAdd' is functioning properly but have no idea why 'currentValue' is always reset to 0.
questions on SO that I viewed;
Display dynamic variable on next key frame AS3
Avoider Game Tutorial: Score not working
The problem is this:
var instanceScore:Score = new Score();
This means that every time there is a tick, you create new score. When you create new score, it starts from 0. You update it to 10, and on the next tick, you ignore the old value of 10 by creating a whole new class. The new class has a value of 0, as it never has it's value increased before. Then you increase it again.. and a new tick starts (looping)
What you need to do is to instantiate score instance ONLY ONCE and save it as a class member variable. Then you can call increase to it.

How to add a Timer in AS3

I was wondering how to add an ingame Timer in AS3, I made a health bar for enemy mobs and once mob is attacked, health bar is visible. I need to add piece of code which if Enemy was not attacked for 5 Seconds, healthBar.visible = false; but I have no idea how to deal with time in AS3
any ideas ?
My suggestions are as follows:
Assumed MouseClick is Ninja attacked hero. If you using a this code you need to create Class or fit type. I just written skeleton code.
Download Source
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
stage.addEventListener(MouseEvent.CLICK, onAttacked);
var isAttacked:Boolean;
var timer:Timer = new Timer(100,50);
var alphaCount:Number = 1.0;
timer.addEventListener(TimerEvent.TIMER, onTick);
function onAttacked(e:MouseEvent):void
{
isAttacked = true;
timer.start();
}
function onTick(e:TimerEvent):void
{
alphaCount = 1.0 - ( 2 * timer.currentCount ) / 100;
if(!isAttacked)
{
goFadeStatusBar();
}
else
{
restoreStatusBar();
alphaCount = 1.0;
timer.reset();
timer.start();
isAttacked = false;
}
}
function goFade():void
{
bar.alpha = alphaCount;
}
function restore():void
{
bar.alpha = 1;
}