KeyCombo

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Reverted edits by Joseph05408 (Talk); changed back to last version by StarManta)
m (Fixing syntax errors)
Line 1: Line 1:
 
This class makes it easy to detect multi-key combos in, for example, fighting games. Created by [[User:StarManta|StarManta]].
 
This class makes it easy to detect multi-key combos in, for example, fighting games. Created by [[User:StarManta|StarManta]].
<javascript>
+
<javascript>class KeyCombo
class KeyCombo {
+
{
var buttons : String[];
+
var buttons : String[];
var currentIndex : int=0; //moves along the array as buttons are pressed
+
var currentIndex : int=0; //moves along the array as buttons are pressed
var allowedTimeBetweenButtons : float = 0.3; //tweak as needed
+
var allowedTimeBetweenButtons : float = 0.3; //tweak as needed
private var timeLastButtonPressed : float;
+
private var timeLastButtonPressed : float;
  
function KeyCombo(b : String[]) {
+
function KeyCombo(b : String[])
buttons = b;
+
{
}
+
buttons = b;
 +
}
  
//usage: call this once a frame. when the combo has been completed, it will return true
+
//usage: call this once a frame. when the combo has been completed, it will return true
function Check() : boolean {
+
function Check() : boolean
if (Time.time > timeLastButtonPressed + allowedTimeBetweenButtons) currentIndex=0;
+
{
if (currentIndex < buttons.length) {
+
if (Time.time > timeLastButtonPressed + allowedTimeBetweenButtons) currentIndex=0;
if ((buttons[currentIndex] == "down" && Input.GetAxisRaw("Vertical") == -1) ||
+
if (currentIndex < buttons.length)
(buttons[currentIndex] == "up" && Input.GetAxisRaw("Vertical") == 1) ||
+
{
(buttons[currentIndex] == "left" && Input.GetAxisRaw("Vertical") == -1) ||
+
if ((buttons[currentIndex] == "down" && Input.GetAxisRaw("Vertical") == -1) ||
(buttons[currentIndex] == "right" && Input.GetAxisRaw("Horizontal") == 1) ||
+
(buttons[currentIndex] == "up" && Input.GetAxisRaw("Vertical") == 1) ||
(buttons[currentIndex] != "down" &&  buttons[currentIndex] != "up" &&  buttons[currentIndex] != "left" &&  buttons[currentIndex] != "right" && Input.GetButtonDown(buttons[currentIndex]) ) {
+
(buttons[currentIndex] == "left" && Input.GetAxisRaw("Vertical") == -1) ||
timeLastButtonPressed = Time.time;
+
(buttons[currentIndex] == "right" && Input.GetAxisRaw("Horizontal") == 1) ||
currentIndex++;
+
(buttons[currentIndex] != "down" &&  buttons[currentIndex] != "up" &&  buttons[currentIndex] != "left" &&  buttons[currentIndex] != "right" && Input.GetButtonDown(buttons[currentIndex])) )
}
+
{
 +
timeLastButtonPressed = Time.time;
 +
currentIndex++;
 +
}
  
if (currentIndex >= buttons.length) {
+
if (currentIndex >= buttons.length)
currentIndex = 0;
+
{
return true;
+
currentIndex = 0;
}
+
return true;
else return false;
+
}
}
+
else return false;
var falconPunch : KeyCombo = KeyCombo(["down","right","punch"]);
+
}
var falconKick : KeyCombo = KeyCombo(["down", "right", "kick"]);
+
}
 +
var falconPunch : KeyCombo = KeyCombo(["down","right","punch"]);
 +
var falconKick : KeyCombo = KeyCombo(["down", "right", "kick"]);
  
function Update() {
+
function Update()
if (falconPunch.Check()) {
+
{
// do the falcon punch
+
if (falconPunch.Check())
}
+
{
if (falconKick.Check()) {
+
// do the falcon punch
// do the falcon kick
+
}
}
+
if (falconKick.Check())
 +
{
 +
// do the falcon kick
 +
}
 +
}
 
}</javascript>
 
}</javascript>

Revision as of 10:23, 14 March 2010

This class makes it easy to detect multi-key combos in, for example, fighting games. Created by StarManta. <javascript>class KeyCombo { var buttons : String[]; var currentIndex : int=0; //moves along the array as buttons are pressed var allowedTimeBetweenButtons : float = 0.3; //tweak as needed private var timeLastButtonPressed : float;

function KeyCombo(b : String[]) { buttons = b; }

//usage: call this once a frame. when the combo has been completed, it will return true function Check() : boolean { if (Time.time > timeLastButtonPressed + allowedTimeBetweenButtons) currentIndex=0; if (currentIndex < buttons.length) { if ((buttons[currentIndex] == "down" && Input.GetAxisRaw("Vertical") == -1) || (buttons[currentIndex] == "up" && Input.GetAxisRaw("Vertical") == 1) || (buttons[currentIndex] == "left" && Input.GetAxisRaw("Vertical") == -1) || (buttons[currentIndex] == "right" && Input.GetAxisRaw("Horizontal") == 1) || (buttons[currentIndex] != "down" && buttons[currentIndex] != "up" && buttons[currentIndex] != "left" && buttons[currentIndex] != "right" && Input.GetButtonDown(buttons[currentIndex])) ) { timeLastButtonPressed = Time.time; currentIndex++; }

if (currentIndex >= buttons.length) { currentIndex = 0; return true; } else return false; } } var falconPunch : KeyCombo = KeyCombo(["down","right","punch"]); var falconKick : KeyCombo = KeyCombo(["down", "right", "kick"]);

function Update() { if (falconPunch.Check()) { // do the falcon punch } if (falconKick.Check()) { // do the falcon kick } } }</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox