Be a Supporter!

(AS2) Reflex game help needed

  • 227 Views
  • 9 Replies
New Topic Respond to this Topic
ComX
ComX
  • Member since: Jun. 10, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
(AS2) Reflex game help needed 2017-05-04 04:25:52 Reply

Hello
I'm trying to make a flash game similar to "Dragon's Lair" in which the animation flows until there's a moment where you need to press a specified button in a short amount of time to progress, or it's game over. I'm not very good with Flash programming (the only one I vaguely understand is AS2) so I'm turning for help to this forum.
I mostly do everything on frames and how I imagine it at the moment is like in this video:
https://www.youtube.com/watch?v=i6em4GRiRY0
When the animation arrives at a certain frame, an element will flash and display which button to press (WASD, not by clicking) and if the button isn't pressed within a few seconds (+/-) it disappears and leads to a frame labeled "GameOver_#", but if it's pressed it leads to frame labeled "Continue_#", and so on until it reaches "Ending". Basically a reflex game.

The problem is I don't know how to make a button like that, so could anyone please help?

My rough idea for the button code is:

(start timer)
on (keyPress"<W>") {
gotoAndPlay(Continue_#);
stop();
}
on (keyPress"<A>") {
gotoAndPlay(GameOver_#);
stop();
}
on (keyPress"<S>") {
gotoAndPlay(GameOver_#);
stop();
}
on (keyPress"<D>") {
gotoAndPlay(GameOver_#);
stop();
}
(if timer runs out)
gotoAndPlay(GameOver_#);

GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 20
Game Developer

At 5/4/17 04:25 AM, ComX wrote: Hello

Made a test fla (via flash CS6)
http://www.newgrounds.com/dump/item/cbe86f6cc51e9dd5e2f5c04652a79b28

It's As3 because I've forgotten everything about as2, and you should too.

It works by tracking keyboard presses, and if you're on the frame "action_1" and press "W" it goes to "press_W_1", or if that frame doesn't exist it goes to "press_any_1" (if it exists). no matter what action your on, it will take that number and determine the "press" frame to go to.

try it out, here.

the code is literally just:

import flash.display.FrameLabel;

const LABEL_PARSER:RegExp = /action_(\d+)(?:_\d+)?/;

var keys:Object = { A:false, S:false, W:false, D:false };
var labels:Object = {};
for each(var label:FrameLabel in currentLabels)
	labels[label.name] = label.frame;

function hasLabel(name:String):Boolean { return name in labels; }

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
function keyHandler(e:KeyboardEvent):void {
	
	var input:String = String.fromCharCode(e.keyCode).toUpperCase();
	if(input in keys) {// check for W,A,S, or D
		
		var isDown:Boolean = e.type == KeyboardEvent.KEY_DOWN
		if(keys[input] == false && isDown)
			handlePress(input);
		
		keys[input] = isDown;
	}
}

function handlePress(key:String):void {
	
	if(LABEL_PARSER.test(currentLabel)){ 
		// label contains "action_#"
		
		var currentAction:int = currentLabel.match(LABEL_PARSER)[1];
		if (hasLabel("press_" + key + "_" + currentAction))
			gotoAndPlay("press_" + key + "_" + currentAction);
		else if (hasLabel("press_any_" + currentAction))
			gotoAndPlay("press_any_" + currentAction);
	}
}

... on the first frame, with a few gotoAndPlay() calls throughout the timeline

ComX
ComX
  • Member since: Jun. 10, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to (AS2) Reflex game help needed 2017-05-04 19:31:11 Reply

I'll stick to AS2, thank you. Also thanks for the hard work but all this code makes my head spin just from looking at it.

Apparently the best and simplest ideas come to my mind after midnight, because I can just place the button on the timeline and make it display only during the frames it needs to be pressed, like this:
Reflex test
You do nothing, the animation just continues and goes to the bad end.
You press the button, the animation jumps to good end/continues.
How did this simple solution elude me makes my head hurt. I guess laziness is the mother of progress XD

GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 20
Game Developer
Response to (AS2) Reflex game help needed 2017-05-04 20:57:48 Reply

At 5/4/17 07:31 PM, ComX wrote: I'll stick to AS2, thank you. Also thanks for the hard work but all this code makes my head spin just from looking at it.

whatever works best for you. good luck!

ComX
ComX
  • Member since: Jun. 10, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to (AS2) Reflex game help needed 2017-05-05 03:57:24 Reply

One more thing - how would I add a score to this game? Like if I wanted to make something similar to Rhythm Heaven series?

My idea is that I'd have a few buttons on the timeline this time - a (too early), (good), (perfect) and (too late) ones with scores 0,1,2,0 respectively. What I want to do is that when reaching a certain score you get a different ending, like:

Game has 10 prompts, code placed on timeline:
if (score"0") pts > poor
if (score"between 1 - 9") pts > OK
if (score"between 10 - 19") pts > good!
if (score"20") pts > perfect!!!

What code do I add to the buttons and timeline to make this work? (AS2 please)

Gimmick
Gimmick
  • Member since: Aug. 20, 2008
  • Offline.
Forum Stats
Member
Level 27
Programmer
Response to (AS2) Reflex game help needed 2017-05-06 23:33:45 Reply

For the score thing, make a variable called score on the timeline (e.g. _root.score = 0) and then inside the button press event increase _root.score.


Slint approves of me! | "This is Newgrounds.com, not Disney.com" - WadeFulp
"Sit look rub panda" - Alan Davies

BBS Signature
ComX
ComX
  • Member since: Jun. 10, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to (AS2) Reflex game help needed 2017-05-07 03:20:50 Reply

At 5/6/17 11:33 PM, Gimmick wrote: For the score thing, make a variable called score on the timeline (e.g. _root.score = 0) and then inside the button press event increase _root.score.

Thanks! But how do I increase it by specific value, and how do I make it go to a specific frame if the score is in certain range?

Gimmick
Gimmick
  • Member since: Aug. 20, 2008
  • Offline.
Forum Stats
Member
Level 27
Programmer
Response to (AS2) Reflex game help needed 2017-05-07 06:50:26 Reply

At 5/7/17 03:20 AM, ComX wrote:
At 5/6/17 11:33 PM, Gimmick wrote: For the score thing, make a variable called score on the timeline (e.g. _root.score = 0) and then inside the button press event increase _root.score.
Thanks! But how do I increase it by specific value, and how do I make it go to a specific frame if the score is in certain range?

Suppose the button can be pressed any time within 100 frames (e.g. the button is on the stage from frames 0 to 100, or from 205 to 305, etc.)

Then you assign ranges to the "too early", "early", "good", "late" and "too late" (insert more or remove as needed).
If the range is divided evenly - in this example there are 5 possible timings - then each one is (duration/5 = 100 / 5 = 20) frames long. This can be generalized into a function which accepts any range, the current frame and the score array, so that it's easy to call; create a function on the timeline that does this. For example:

_root.addScore = function(frameRange:Number, currentFrame:Number, scores:Array):Void {
    var stepSize:Number = frameRange / scores.length;
    var iscore:Number = 0;
    for(var i:Number = 0; i < scores.length; i ++) {
        if(currentFrame < (stepSize * i)) {
             iscore = scores[i];
             break;
        }
    }
    _root.score += iscore;
}

So what this does is that it evenly divides a given range (0 to 100 as the frame duration in which you can press a button, for example) into the number of elements that are present in the scores array. It then checks whether it falls within the range (in the previous example, it checks whether it falls below 20, 40, 60, 80, 100 sequentially) and then when it finds the first match, looks up the respective score in the array, and adds that value to _root.score.

This way, now in the button you have, just call the function _root.addScore with the parameters - your frame range (ie how long the button remains on the stage), the current frame (well, not exactly the current frame; suppose your button is on the screen for 200 frames, then the currentFrame argument is supposed to be how long it has been since the button was on the stage - this is (_root._currentFrame - beginning), e.g. if the button appeared at frame 730 for 200 frames, and it was called at frame 910, then the value of currentFrame is 910 - 730 = 180) and the scores array, which in your case is [0, 1, 2, 0].

As an example, to call this function using a flag called "appears" in the button load event (I'm not sure if this exists for buttons - it's been a long while since I tested any AS2, but I know it works on MovieClips):

onClipEvent(load) {
    appears = _root._currentFrame;
}
on(keyPress"<W>") {
    _root.addScore(100, _root._currentFrame - appears, [0, 1, 2, 0]);
}
//etc.

Again, the above code may not work; in this case, you shall have to either convert the button to a movieclip, or make "appears" global (ie. _root.appears = _root._currentFrame) and set it manually each time the button is supposed to appear.


Slint approves of me! | "This is Newgrounds.com, not Disney.com" - WadeFulp
"Sit look rub panda" - Alan Davies

BBS Signature
Gimmick
Gimmick
  • Member since: Aug. 20, 2008
  • Offline.
Forum Stats
Member
Level 27
Programmer
Response to (AS2) Reflex game help needed 2017-05-07 06:53:38 Reply

At 5/7/17 03:20 AM, ComX wrote:
At 5/6/17 11:33 PM, Gimmick wrote: For the score thing, make a variable called score on the timeline (e.g. _root.score = 0) and then inside the button press event increase _root.score.
Thanks! But how do I increase it by specific value, and how do I make it go to a specific frame if the score is in certain range?

As for making the scene jump to a certain frame if the score is in a certain range, you just use the if conditional statements to check whether it is within your range or not. If you're asking about the game jumping the moment the score is reached, simply put the check within an onEnterFrame event.


Slint approves of me! | "This is Newgrounds.com, not Disney.com" - WadeFulp
"Sit look rub panda" - Alan Davies

BBS Signature
ComX
ComX
  • Member since: Jun. 10, 2008
  • Offline.
Forum Stats
Member
Level 05
Blank Slate

WOW. Pardon me, but I flunked programming and all of it just makes my head spin, not to mention it's becoming needlessly complicated.

Actually what I had in mind is something like this, all it needs is proper code:
Game Engine

I'm aware that if someone mashes the button in the interval between good scores then they'd make an astronomical score, so I don't know how to fix that...

Anyways, can someone please look at that flash project and help me make it work?
I don't want to make anything too fancy, it'll be just a small part of other project, so If I can't make a Rhythm Heaven lookalike then I'll stick with the first, Dragon Lair-ish one.