PDA

View Full Version : i want to have a timer of my game to last a certain amount of time


LordRoss
02-19-2008, 04:13 PM
Hello all,

I've been using these forums for the past few weeks or so, trying to gather information and help and tutorials to help with creating my own game.

Unfortunately I'm not so good at writing actionscript, but I can read it. Strange I know, but it's the same with any programming language, i can read but can't write. Each programming language has its different way of doing things, and they all confuse me as I tend to try combine them all. Though i'm more focused on the design and look of a game over the programming, but without the programming the game wont work....obviously.

Anywho, i want to have a timer of my game to last a certain amount of time, and after every second i want it to check to see if a button is pressed, and if it is pressed, do this, if not, do that.

The game is music game where you got to press the right button at the right time, so i've been looking for timer tutorials and music game tutorials and help within various forums and websites, though none has really mentioned what i'm after, hence making this forum post.

I hope any of you can help or give me some guidance of any kind, any help will be much appreciated. Thankyou in advance

Rossy

Noct
02-19-2008, 04:42 PM
Welcome aboard.

You could certainly use an interval to check if a key is down when it fires, but assuming I understand what you're describing I would do it differently.

Is this like a traditional music type game where there is some type of onscreen indicator of when to press the button? Like, a shape moves across a certain point or another shape when it is the proper time to press?

If so, I would suggest using a keylistener that checks to see if the moving shape is over the correct area when the key is pressed, using either a hitTest, or just a numeric evaluation of the clip's coordinates.

LordRoss
02-19-2008, 05:05 PM
yea, ya right it does have onscreen indicator of when to press the button, but looking at this post http://www.actionscript.org/forums/showthread.php3?t=154308&highlight=guitar

i thought i could use similar way, though couldn't figure out how to do the hittest (or where to place the coding for it) and i couldn't figure out how to have several different icons flowing towards the indicator at different posistions. So i thought i just sorta bypass those problems and make it look as if im doing that by the use of animation, and just code the timing and button pressing instead. I thought it would be easier, though its proving just as hard.

Is the numeric evaluation an easier way of detecting if a particular icon has passed a particular point?

to explain what i mean, im thinking of a musical score sheet, that notes on various pitches go across. The player plays the correct note with correct timing.

Noct
02-19-2008, 06:55 PM
Well, if you're just doing it all through timeline animation, you don't need a timer or a hitTest.

Simply create a keypress event that checks the frame of the movieClip showing the note animation. If it is on the frame where the shape is over its marker, its a hit. You would obviously have to change the keypress evaluation for every note, which you could either do through an array of the correct answers, or you could just use one variable and continuosly update it.

For instance, in the keypress event, evaluate a variable storing the frameNumber the timeline should be at. In that timeline, set a new value to that variable on the frame following the hitFrame.

You would probably want to disable the keypress on hit and then re-enable it in the same frame you are updating the variable. Otherwise the user could just hold the key down and get a hit.

LordRoss
02-19-2008, 08:07 PM
oh that sounds better, if i dont have to use hit test or timing. I mean I started to think about using a different strategy.

Was gonna make a note movie clip and an invisible box movie clip, and combine them into one movie clip. Then produce more notes and arrange them in a certain order, and create them all into one big movie clip, and move them by use of tweening. Then with another invisible box as an area for when the player is expected to play the notes containing hit test. something like:

press key {
if (boxnote.hitTest(boxarea) {
play sound
add to score (or whatever)
}

Not sure if that makes sense, but that was where I was starting to go. If I go with your idea, I can still use the same idea of using the movie clips in such a way, though how do I create a keypress event? I understand the use of an array or variable, though not sure how to write in code setting up a keypress event that detects if the note is over the area. I certainly don't want no cheating, so thanks for your tip on that, though before I deal with that issue, I just want to get something working.

If i understand what your saying correctly, than your way could make things very simple, and that means good news for me, as I can focus more on the look (what im good at) rather then the coding (as you can see, i'm not good at).

As far as i'm aware, though correct me if i'm wrong, I just need to create a keypress event to listen out for a particular button to be pressed when the movie clips containing the notes reaches a certain point in the timeline. I can see how that will work, though not sure where to write the action script of this or how.

Sorry if my ignorance of coding is annoying, but I have tried searching for answers and failed. Mainly because you can do things 1000 different ways, and i'm not sure which one is the best way for me.

Noct
02-19-2008, 10:14 PM
Okie, here I am storing the two correct frames you can press the key at (30,90) in an array, I'm storing a reference to the timeline the clips are in (that could just be "this" if you are trying to evaluate the main timeline).

Then I'm creating an onEnterFrame that checks what frame the timeline is in and sets the correct answer into the "countNum" variable. The onEnter frame pushes the correct answer variable forward each time the timeline hits that frame so that the keypress is only evaluating if we are on the most current frame to be evaluated.

Then I'm creating a keyListener for the DOWN arrow keyboard key.
When that key is pressed, it checks the current frame of the timeline we're watching, and checks to see if it is equal to the current correct answer in the array. (I'm also doing a + or - 1 in there to make it easier).

//Store reference to timeline:
var myTimeLine:MovieClip = this.myMc;
//Store correct frames in array:
var myArray:Array = [30, 90];
//Create counter
var countNum:Number = 0;
//Watch timeline of movieClip:
myTimeLine.onEnterFrame = function() {
var curFrame:Number = this._currentframe;
//Update (countNum) as timeline advances
if (curFrame>myArray[countNum]) {
countNum++;
}
};
//Create keyListener object
var keyListener:Object = new Object();
//Declare keyPress Function
keyListener.onKeyDown = function():Void {
//If Down arrow is pressed
if (Key.isDown(Key.DOWN)) {
//check timeline of movie:
var curFrame:Number = myTimeLine._currentframe;
//If less then or equal to target frame (+ or - 1), its a hit
if ((curFrame == myArray[countNum]) || (curFrame == (myArray[countNum]-1))
|| (curFrame == (myArray[countNum]+1))
) {
trace("HIT");
} else {
trace("MISS");
}
}
};
//Apply listener
Key.addListener(keyListener);

LordRoss
02-20-2008, 11:47 AM
wow thanks for your help and your demonstration, illustrated perfectly. I will get to work on it using your code as template to go on. Will show you the finished game once done, so you can see your help you made it, as its the best way i can show my appreciation. Will mention you (and this website)in the credits unless you prefer not to.

Thanks for sorting out the cheating bit as well, i noticed you managed to get that done too.

I'm sure I can work out the rest of things myself, menus, different levels and such.

I hope maybe others who are doing similar games will use your example as an aid too. Thankyou muchly, Rossy

Noct
02-20-2008, 03:13 PM
NP, glad to help.