View Full Version : Assistance with game flow
killerspam
February 1st, 2008, 04:59 PM
Hi guys,
I'm creating a game using AS3 (specifically using FDT). I've spent the past week learning all of the available OO-design patterns, and I'm trying to put them to good use, where appropriate.
I'm having trouble thinking of a solution to a particular element of my game. Over a period of 3 or 5 minutes, I want to draw a series of targets on screen.
For example, at 20-seconds, I want to draw a circle at x/y, and at 25 seconds, I want to check if the user has clicked the target. The user will gain more points the closer they click the object to the 'target' time. So, I would like to create a 'target' object with (for example) the properties 'hitTime', 'xPos', 'yPos'.
Does anybody have any suggestions as to the best way to approach this? I am trying to figure out the best way to check every 0.5-ish seconds to see if any targets are due within the next (for example) 5 seconds, and draw them on the screen.
This may have a simple solution, but hopefully somebody's input will point me in the right direction.
Many thanks :)
_marlon
February 1st, 2008, 05:46 PM
Hey dude,
I'm pretty new to flash too but hopefully I can push you in the right direction.
I'm just gonna give some rough solutions to each bit of the game...
For making the target, I suggest making use of the Graphics class and drawing your own circles. Then add some 'click' event listeners to them. The function that happens when the user clicks on them should do this:
- Get the x and y position of where the user has clicked on the circle
- Work out how close to the center of the circle that click was (by using the radius which you can set somewhere and then some basic maths - google if you need any help on that stuff ;) )
- Then I'm sure doing some more maths you could give them an appropriate number of points (eg: If the user has clicked in the outer 50% of the circle, give them 1 point, if they've clicked in the next 25% of the circle give them 2 points, and for the bullseye give them 3... or whatever :P )
So that covers the scoring!
As for randomly positioning the circles at random times, you could have a timer somewhere like:
var randomTime:Number = 15000 + Math.random() * 5000;
var tMakeTarget:Timer = new Timer(randomTime);
So it'll wait a random time between 15-20 seconds... Then for the function it calls, just have an addChild which adds a target randomly...
If you put the target in a class (the drawing of a circle etc..) then it'll make it alot easier to have random diameters etc...
Sorry if this makes barely any sense, I'm not the best at explaining things, but here's what I think you should have at the end of it:
Game.fla
-> Document class of Main.as
Main.as
-> Timer event to make a target
-> Function to make a target - adds a new Target object, and possibly its own Timer to remove it after 5 seconds or whatever. When you make the target you'll want to add the event listener to it straight away to check for clicks (shoots), and then have your own function to deal with that (removes child and calls upon kill function)
Target.as
-> Constructor calls a function to draw a target
-> Seperate function to "kill" the target (do a removeChild on the shape)
As I was writing that I just realised that if you wanted to have more than one target then you'll need to push each "target" object to an array (so you can keep track of each one)..
But yeah... start it off and see where you run into problems (if any :P)
F1 is damn helpful (if you need to learn about things like the Graphics class which is real easy when you get the hang of it) and also google (if you need more help on classes, event listeners, functions etc...)
Feel free to ask me anything if I've done a crap job explaining here :S
killerspam
February 1st, 2008, 05:58 PM
Hi _marlon,
Thanks for the reply. Fortunately, the reply you've given me is similar to how I've designed the project so far.
I'm very comfortable with OO-design in general, and I've already established classes with views to render the actual targets. Sorry if I hadn't made this clear.
My actual problem lies more to do with the timing. Your suggestion is more of a 'random placement', and you correctly mention that I'll need an array to hold the targets.
In this situation, I need to specify an exact time to perform the 'click' check, as users are going to be encouraged to click on a target as a specified time. 1 second early results in 50 points, exact timing = 100 points, 1 second late = 50 points.
(in-fact, the user won't be clicking at-all. I've been developing a motion-tracking tool that tracks X/Y co-ordinates of user's hands, which is highly accurate. The user must wave their hands in-front of the target at the specified time).
Let's say, for example, that I have a target object that looks like this:
public class DanceTarget extends Sprite {
private var _targetTime:uint;
private var _xPos; // Relative, so not x
private var _yPos;
public function DanceTarget(time:uint, xPos:uint, yPos:uint):void{
// Draw the target code
}
}
the container class would initiate many DanceTargets, and probably a Timer. If an instance had a _targetTime of 10,000 milliseconds, I'd want to draw it to the screen at 5,000 milliseconds.
I'm trying to work out whether I need to create my full array of targets at the start of the level, passing a the timer reference to each one... or perhaps leave the 'draw' instantiator to the container class.
I hope this makes sense to you :)
Regards,
Craig
_marlon
February 1st, 2008, 06:44 PM
Oh sorry about that, my stuff must have seemed pretty useless then XD
I see what you're trying to do now (I think :P) , off the top of my head I think this could work (although I expect there may be a more efficient way to do it).
Have a variable set at 0 or something, then have a timer set to 5 milliseconds or so, whenever the timer triggers, have it +5 to the variable, when the user clicks (or waves) in front of the target, just remove the timer and that variable should have stored the time taken to get click it (in milliseconds)
Only solution I can think of that the moment, hope it helps a bit :)
EDIT: Add the timer to the individual targets when they spawn of course :P
SECOND EDIT: I've just re-read the bottom of your last post and I think I may have answered a question you didn't need answering >_<
killerspam
February 1st, 2008, 07:32 PM
Thanks _marlon; you've given me some food-for-thought :)
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.