View Full Version : Timer question
ø
September 8th, 2006, 05:38 PM
Hi folks:D
Im working on a game (link (http://img104.imageshack.us/my.php?image=gameconseptxa6.swf)) and I want to have a timer. The timer is the numbers on the top right side. The timer shall start on 0 and count 1,2,3 etc (seconds) until the player "dies". By that I mean gets hit by an enemy or go outside the stage. So the timer will stop when you are brought to frame 2, the "game over" part.
What is a smart way of doing that?
Also.. I have an enemy in my library, with it's identifier name "enemy". And I want to add a new enemy to the stage every 10 seconds.
Now how can I do this the best way?
thanks and..
peace;)
Joppe
September 8th, 2006, 06:41 PM
Maybe with an function like this
time = 0;
count = function(){
_root.time += 1
}
c = setInterval(count,1000)
REEF·
September 8th, 2006, 07:13 PM
The timer shall start on 0 and count 1,2,3 etc (seconds) until the player "dies". By that I mean gets hit by an enemy or go outside the stage. So the timer will stop when you are brought to frame 2, the "game over" part. What is a smart way of doing that?
I assume you already have your code for detecting death using hitTests? When he dies, set a variable using playerDead = true; So heres a timer code:
var gameTime = 1;
var timeWhenGameEnds = 0;
var playerDead = false;
function keepTime() {
_root.playerDead ? (_root.timeWhenGameEnds = _root.gameTime, clearInterval(timeInterval)) : _root.gameTime++;
}
Also.. I have an enemy in my library, with it's identifier name "enemy". And I want to add a new enemy to the stage every 10 seconds.
Try adding this:
function addNewEnemy(identifier) {
trace('new');
_root.playerDead ? clearInterval(enemyInterval) : _root.attachMovie(identifier, identifier + _root.gameTime, _root.gameTime);
_root[identifier + gameTime]._x = 100;
_root[identifier + gameTime]._y = 100;
}
timeInterval = setInterval(keepTime, 1000);
enemyInterval = setInterval(addNewEnemy,10000, "enemy");
ø
September 9th, 2006, 09:49 AM
Yes I've got the code for detecting death using hitTests! You see that when you test my game (link in previous post). But what do you mean by "when he dies, set a variable"?
And thanks! I will have a look at it and see if I can work it out:)
EDIT:
Okey I got this done:
The timer.. and it counts in seconds!
And every 10th seconds an output message says: "new"
But there is no enemy added onto the stage:/
Here is the code I've got:
stop();
gameTime = 0;
count = function(){
_root.gameTime += 1
}
c = setInterval(count,1000)
function addNewEnemy(enemy) {
trace('new');
_root.playerDead ? clearInterval(enemyInterval) : _root.attachMovie(enemy, enemy + _root.gameTime, _root.gameTime);
_root[enemy + gameTime]._x = 100;
_root[enemy + gameTime]._y = 100;
}
timeInterval = setInterval(keepTime, 1000);
enemyInterval = setInterval(addNewEnemy,10000, "enemy");
character.onEnterFrame = function() {
if (walls.hitTest(this._x - this._width / 2, this._y, true)) {
_root.gotoAndStop(2);
} else if (walls.hitTest(this._x + this._width / 2, this._y, true)) {
_root.gotoAndStop(2);
} else if (walls.hitTest(this._x, this._y - this._height / 2, true)) {
_root.gotoAndStop(2);
} else if (walls.hitTest(this._x, this._y + this._height / 2, true)) {
_root.gotoAndStop(2);
}
if (enemy.hitTest(this._x - this._width / 2, this._y, true)) {
_root.gotoAndStop(2);
} else if (enemy.hitTest(this._x + this._width / 2, this._y, true)) {
_root.gotoAndStop(2);
} else if (enemy.hitTest(this._x, this._y - this._height / 2, true)) {
_root.gotoAndStop(2);
} else if (enemy.hitTest(this._x, this._y + this._height / 2, true)) {
_root.gotoAndStop(2);
}
};
REEF·
September 9th, 2006, 01:30 PM
Did you bother to read my code? The second part is running function keepTime but you didnt add that function to your movie. The second part of the code I posted comes hand in hand with the first, you cant leave one without the other. Replace joppes code in lines 2~6 with the first part of the code I gave you.
I assume _root.gotoAndStop(2); means death, so right under each of these, have this code:
_root.playerDead = true;
mnkeymasta
September 16th, 2006, 08:27 PM
What did you get the idea for this game from, neopets?
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.