Quote:
Originally Posted by Chessin
I have seen in some of these cocos 2d games or 3d games and it controls the character that moves back and forth. Does anyone have a working control pad or a tutorial somewhere on the internet. I have seen nothing so far. Thanks!
|
Yeah you create a Class that inherits from UIImageView(You need to enable user interaction)(or a UIView but that is a little harder). Set the image of the UIImageView to your d-pad image.
Here is a little code for you
In the dpad.m file:
Code:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
if(touchPoint.y < self.view.center.y && touchPoint.x < self.view.center.x)
{
//Move right
}
if(touchPoint.y > self.view.center.y && touchPoint.x > self.view.center.x)
{
//Move left
}
if (touchPoint.y > self.view.center.y && touchPoint.x < self.view.center.x) {
//Move Up
}
if (touchPoint.y < self.view.center.y && touchPoint.x > self.view.center.x) {
//Move Down
}
}
This is a awkward d-pad though because instead of hitting the buttons you hit the sides. To fix this you can hit the sides this can be fixed by off centering all the buttons on the picture by about 1 pixel and you'd be fine. Theres another way to do this. Instead of making only one class you make 4 one for each button. They can both inherit from UIImageView and in the touchesBegin you put the move up,move down, move left, move right depending on the button. Hope that helps