Advertisement
Guest User

Untitled

a guest
Mar 24th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import «Gamepad.h»
  2.  
  3. #import «SneakyJoystick.h»
  4. #import «SneakyJoystickSkinnedBase.h»
  5. #import «SneakyButton.h»
  6. #import «SneakyButtonSkinnedBase.h»
  7.  
  8. #import «CCSprite+CCSpriteExtension.h»
  9.  
  10. @implementation Gamepad
  11.  
  12. @synthesize leftJoystick, B, A;
  13.  
  14. -(id)initWithParentLayer:(CCLayer *)parentLayer
  15. {
  16. if( (self=[superinit])) {
  17. CGSize screenSize = [[CCDirectorsharedDirector] winSizeInPixels];
  18. SneakyJoystickSkinnedBase *leftJoy = [[[SneakyJoystickSkinnedBasealloc] init] autorelease];
  19. leftJoy.backgroundSprite = [CCSpritespriteWithFile:@"joypad.png"];
  20. leftJoy.position = ccp(leftJoy.backgroundSprite.width / 2, leftJoy.backgroundSprite.height / 2);
  21. leftJoy.thumbSprite = [CCSpritespriteWithFile:@"button1.png"];
  22. leftJoy.joystick = [[SneakyJoystickalloc] initWithRect:CGRectMake(0, 0, leftJoy.backgroundSprite.width, leftJoy.backgroundSprite.height)];
  23. self.leftJoystick = leftJoy.joystick;
  24. leftJoystick.isDPad = YES;
  25. leftJoystick.numberOfDirections = 8;
  26. [parentLayer addChild:leftJoy];
  27.  
  28. SneakyButtonSkinnedBase *rightBut2 = [[[SneakyButtonSkinnedBasealloc] init] autorelease];
  29. rightBut2.defaultSprite = [CCSpritespriteWithFile:@"button2.png"];
  30. rightBut2.activatedSprite = [CCSpritespriteWithFile:@"button2.png"];
  31. rightBut2.pressSprite = [CCSpritespriteWithFile:@"button2.png"];
  32. rightBut2.position = ccp(screenSize.width — rightBut2.defaultSprite.width / 2, rightBut2.defaultSprite.height / 2);
  33. rightBut2.button = [[SneakyButtonalloc] initWithRect:CGRectMake(0, 0, rightBut2.defaultSprite.width / 2, rightBut2.defaultSprite.height / 2)];
  34. self.A = rightBut2.button;
  35. A.isHoldable = YES;
  36. [parentLayer addChild:rightBut2];
  37.  
  38. SneakyButtonSkinnedBase *rightBut1 = [[[SneakyButtonSkinnedBasealloc] init] autorelease];
  39. rightBut1.defaultSprite = [CCSpritespriteWithFile:@"button1.png"];
  40. rightBut1.activatedSprite = [CCSpritespriteWithFile:@"button1.png"];
  41. rightBut1.pressSprite = [CCSpritespriteWithFile:@"button1.png"];
  42. rightBut1.position = ccp(rightBut2.position.x — rightBut1.defaultSprite.width, rightBut2.defaultSprite.height + rightBut1.defaultSprite.height / 2);
  43. rightBut1.button = [[SneakyButtonalloc] initWithRect:CGRectMake(0, 0, rightBut1.defaultSprite.width / 2, rightBut1.defaultSprite.height / 2)];
  44. self.B = rightBut1.button;
  45. B.isHoldable = YES;
  46. [parentLayer addChild:rightBut1];
  47. }
  48. returnself;
  49. }
  50.  
  51. -(DPAD)getJoypadDirection
  52. {
  53. int x = leftJoystick.stickPosition.x, y = leftJoystick.stickPosition.y;
  54. if (x == 0) {
  55. if (y > 0) {
  56. returnUP;
  57. } elseif (y < 0) {
  58. returnDOWN;
  59. }
  60. } elseif (y == 0) {
  61. if (x < 0) {
  62. returnLEFT;
  63. } elseif (x > 0) {
  64. returnRIGHT;
  65. }
  66. } elseif (y > 0) {
  67. //если одного знака
  68. if (x > 0) {
  69. returnUP_RIGHT;
  70. } elseif (x < 0) {
  71. returnUP_LEFT;
  72. }
  73. } else {
  74. if (x > 0) {
  75. returnDOWN_RIGHT;
  76. } elseif (x < 0) {
  77. returnDOWN_LEFT;
  78. }
  79. }
  80. returnCENTER;
  81. }
  82.  
  83. -(void)dealloc
  84. {
  85. [leftJoystickrelease];
  86. [Arelease];
  87. [Brelease];
  88. [superdealloc];
  89. }
  90.  
  91. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement