Game Development Community

iTorque Simple Virtual dPad / Joystick Howto

by Watermark · in iTorque 2D · 08/21/2011 (10:36 pm) · 0 replies

I got a working virtual control pad prototype set up like thus:

1. First make a gui using GUI Editor with 4 buttons: Up, Down, Left, Right. The "Command" of each is set to the appropriate Press function. i.e. clicking the up button calls function ControlPad::PressUp().
2. In the level, I have a character Sprite called testChara. By pressing the buttons, the testChara will move 1 step in the appropriate direction.
3. The code is below:

function ControlPad::PressUp()
{
ControlPad:updateStep(1, testChara.moveSpeed, testChara);
}

// Same for down, right, and left...

Function updateStep(%pDirection, %pSpeed, %pSprite)
{
%nx1 = 0;
%ny1 = 0;

Switch(%pDirection)
{
Case 1:
%ny1 = -1 * 32;
Break;
Case 2:
%nx1 = -1 * 32;
Break;
Case 3:
%ny1 = 32;
Break;
Case 4:
%nx1 = 32;
Break;
}

%nx2 = %Sprite.getPositionX() + %nx1;
%ny2 = %Sprite.getPositionY() + %ny1;
%nvector = %nx2 SPC %ny2;
%pSprite.moveTo(%nvector, %pSpeed);
}

This works but My Problem: How do I make the buttons press-and-hold or "rapid fire"?

The problem now is I must press the button everytime I want to move the character. In iphone games we know that you can press and hold the control pad and the character will continue in that direction and stop when you release. Also, it is necessary for the character to move a single step (e.g. 32 pixels) every time so I don't think I can just use setLinearVelocity.

I understand there's a better version at:
http://www.garagegames.com/community/blogs/view/18925

But this involves C code and I really just need a simple pad to move 1 step at a time. Is there a way to do it with just script? Thanks.

About the author

Three iTorque 1.5 games published: Sorceria 1: The Mad Doctor RPG Sorceria 2: Sunken City Sagas: RPG Boardgame and Name Generator For more info see our site: http://wmrpg.weebly.com