Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import «Gamepad.h»
- #import «SneakyJoystick.h»
- #import «SneakyJoystickSkinnedBase.h»
- #import «SneakyButton.h»
- #import «SneakyButtonSkinnedBase.h»
- #import «CCSprite+CCSpriteExtension.h»
- @implementation Gamepad
- @synthesize leftJoystick, B, A;
- -(id)initWithParentLayer:(CCLayer *)parentLayer
- {
- if( (self=[superinit])) {
- CGSize screenSize = [[CCDirectorsharedDirector] winSizeInPixels];
- SneakyJoystickSkinnedBase *leftJoy = [[[SneakyJoystickSkinnedBasealloc] init] autorelease];
- leftJoy.backgroundSprite = [CCSpritespriteWithFile:@"joypad.png"];
- leftJoy.position = ccp(leftJoy.backgroundSprite.width / 2, leftJoy.backgroundSprite.height / 2);
- leftJoy.thumbSprite = [CCSpritespriteWithFile:@"button1.png"];
- leftJoy.joystick = [[SneakyJoystickalloc] initWithRect:CGRectMake(0, 0, leftJoy.backgroundSprite.width, leftJoy.backgroundSprite.height)];
- self.leftJoystick = leftJoy.joystick;
- leftJoystick.isDPad = YES;
- leftJoystick.numberOfDirections = 8;
- [parentLayer addChild:leftJoy];
- SneakyButtonSkinnedBase *rightBut2 = [[[SneakyButtonSkinnedBasealloc] init] autorelease];
- rightBut2.defaultSprite = [CCSpritespriteWithFile:@"button2.png"];
- rightBut2.activatedSprite = [CCSpritespriteWithFile:@"button2.png"];
- rightBut2.pressSprite = [CCSpritespriteWithFile:@"button2.png"];
- rightBut2.position = ccp(screenSize.width — rightBut2.defaultSprite.width / 2, rightBut2.defaultSprite.height / 2);
- rightBut2.button = [[SneakyButtonalloc] initWithRect:CGRectMake(0, 0, rightBut2.defaultSprite.width / 2, rightBut2.defaultSprite.height / 2)];
- self.A = rightBut2.button;
- A.isHoldable = YES;
- [parentLayer addChild:rightBut2];
- SneakyButtonSkinnedBase *rightBut1 = [[[SneakyButtonSkinnedBasealloc] init] autorelease];
- rightBut1.defaultSprite = [CCSpritespriteWithFile:@"button1.png"];
- rightBut1.activatedSprite = [CCSpritespriteWithFile:@"button1.png"];
- rightBut1.pressSprite = [CCSpritespriteWithFile:@"button1.png"];
- rightBut1.position = ccp(rightBut2.position.x — rightBut1.defaultSprite.width, rightBut2.defaultSprite.height + rightBut1.defaultSprite.height / 2);
- rightBut1.button = [[SneakyButtonalloc] initWithRect:CGRectMake(0, 0, rightBut1.defaultSprite.width / 2, rightBut1.defaultSprite.height / 2)];
- self.B = rightBut1.button;
- B.isHoldable = YES;
- [parentLayer addChild:rightBut1];
- }
- returnself;
- }
- -(DPAD)getJoypadDirection
- {
- int x = leftJoystick.stickPosition.x, y = leftJoystick.stickPosition.y;
- if (x == 0) {
- if (y > 0) {
- returnUP;
- } elseif (y < 0) {
- returnDOWN;
- }
- } elseif (y == 0) {
- if (x < 0) {
- returnLEFT;
- } elseif (x > 0) {
- returnRIGHT;
- }
- } elseif (y > 0) {
- //если одного знака
- if (x > 0) {
- returnUP_RIGHT;
- } elseif (x < 0) {
- returnUP_LEFT;
- }
- } else {
- if (x > 0) {
- returnDOWN_RIGHT;
- } elseif (x < 0) {
- returnDOWN_LEFT;
- }
- }
- returnCENTER;
- }
- -(void)dealloc
- {
- [leftJoystickrelease];
- [Arelease];
- [Brelease];
- [superdealloc];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement