I would like to adapt http://www.unifycommunity.com/wiki/index.php?title=KeyCombo to 2DPlatformerController.js?
Here is the code:
KeyCombo.js
class KeyCombo
{
var buttons : String[];
private 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;
}
}
}
PlatformerController.js (I got it from 2D Platformer example)
function Update () {
var movementJS = cameraTransform.TransformDirection( Vector3( 0, moveJoystick.position.y, 0 ));
movementJS.x = 0;
movementJS.Normalize(); // Adjust magnitude after ignoring vertical movement
var absJoyPos = Vector2( Mathf.Abs( moveJoystick.position.x ), Mathf.Abs( moveJoystick.position.y ) );
movementJS *= movement.walkSpeed * ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );
var h = movementJS.y;
if (!canControl)
h = 0.0;
movement.isMoving = h > 0.1;
if (movement.isMoving canControl)
jump.lastButtonTime = Time.time;
UpdateSmoothedMovementDirection();
if (activePlatform != null) {
var newGlobalPlatformPoint = activePlatform.TransformPoint(activeLocalPlatformPoint);
var moveDistance = (newGlobalPlatformPoint - activeGlobalPlatformPoint);
transform.position = transform.position + moveDistance;
lastPlatformVelocity = (newGlobalPlatformPoint - activeGlobalPlatformPoint) / Time.deltaTime;
} else {
lastPlatformVelocity = Vector3.zero;
}
activePlatform = null;
lastPosition = transform.position;
var currentMovementOffset = movement.direction * movement.speed + Vector3 (0, movement.verticalSpeed, 0) + movement.inAirVelocity;
currentMovementOffset *= Time.deltaTime;
movement.collisionFlags = controller.Move (currentMovementOffset);
movement.velocity = (transform.position - lastPosition) / Time.deltaTime;
if (activePlatform != null) {
activeGlobalPlatformPoint = transform.position;
activeLocalPlatformPoint = activePlatform.InverseTransformPoint (transform.position);
}
if (movement.direction.sqrMagnitude > 0.01)
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movement.direction), Time.deltaTime * movement.rotationSmoothing);
if (controller.isGrounded) {
movement.inAirVelocity = Vector3.zero;
if (jump.jumping) {
jump.jumping = false;
var jumpMoveDirection = movement.direction * movement.speed + movement.inAirVelocity;
if (jumpMoveDirection.sqrMagnitude > 0.01)
movement.direction = jumpMoveDirection.normalized;
}
}
comboPunch();
}
function comboPunch()
{
var falconPunch : KeyCombo = KeyCombo(["left", "right", "Fire1"]);
if (falconPunch.Check())
{
// do the falcon punch
Debug.Log("Special PUNCH");
}
}
The code deployed in PlatformerController.js is:
private var falconPunch : KeyCombo = KeyCombo([“left”, “right”, “Fire1”]);
if (falconPunch.Check())
{
// do the falcon punch
Debug.Log(“Special PUNCH”);
}
Q.1 It seems the Check() did not use the iphone touch function to detect the joystick
Q.2 For an iphone joystick, how did it detect “up”, “down”, “left”, “right” and “Fire1” ?
“up”, “down”, “left”, “right” - leftpad (GUITexture)
“Fire1” - rightpad (GUITexture)
As I did not think KeyCombo([“left”, “right”, “Fire1”]); can detect it properly.