Hi!
Hi can I create something very similar to these movePad things like in the game, “Hungry shark”?
Answer much appreciated!
Thanks!
Hi!
Hi can I create something very similar to these movePad things like in the game, “Hungry shark”?
Answer much appreciated!
Thanks!
The game calls it a virtual D-pad. It looks like it consists of 2 parts; the larger circle that represents the D-pad area, and the smaller circle that represents the location of the touch. Here is how I would do it:
When the player touches the screen, show the virtual D-pad and center it on the player’s finger. When the player moves their finger, if the touch is inside the larger circle, place the smaller circle at the location of the touch; otherwise put it at the edge of the larger circle. Remember to hide the D-Pad when the touch ends.
Some example code for when the touch moves:
// In this example, largerCircle is the background of the D-Pad and smallerCircle is the part that follows the
// player's finger. smallerCircle is a child of the largerCircle. This code is from inside `onTouchMoved`, where the name of the touch variable is `touch`.
// get the position of the touch relative to the center of largerCircle. this will tell you how far away from the center
// of largerCircle to place smallerCircle.
Point relativePosition = (touch->getLocation() - largerCircle->getPosition());
// the radius (half the diameter/width) of largerCircle. because it is a circle, both the width and height are the same
// so we can just use the width.
float radius = largerCircle->getContentSize().width;
// we don't want the smaller circle to go past the edges of the D-Pad, so if the touch has gone past the edge, restrict
// it by keeping the same angle from the center but restricting the distance.
if (relativePosition.length() > radius) {
relativePosition = relativePosition.getNormalized() * radius;
}
// set the smaller circle to its new position
smallerCircle->setPosition(relativePosition);
Thank you both for the info and for the actual name. From now if I need more info, I can go google it, Thanks!!