Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3790694
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:21:15+00:00 2026-05-19T12:21:15+00:00

I am writing an app for the iPhone using cocos2d where I have 4

  • 0

I am writing an app for the iPhone using cocos2d where I have 4 Sprites that are buttons. I want to allow and respond to the following (allowing the user 3 seconds to apply his input before timing out):

— Single button presses (I have this part working).
— Pressing of more than 1 button at at the same time (each combination would have a different response).
— Flicking/Swiping gestures that originate from within each of the button Sprites.
— Shaking the iPhone.
— Tilting the iphone up, down, left, right (from landscape mode).

Any insight into different approaches would be greatly appreciated. B)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T12:21:16+00:00Added an answer on May 19, 2026 at 12:21 pm

    Here is how I implemented this in case anyone else is trying to do this (I attempted to add the shakes/gestures/tilts part yet just the Simultaneous button presses and for now I have limited it to only 2-button combos — but I could easily extend the logic to handle 3 and 4-button combos as well).

    spuButton.h (a CCSprite Subclass)

    #import <Foundation/Foundation.h>
    #import "cocos2d.h"
    
    typedef enum tagButtonState {
        kButtonStatePressed,
        kButtonStateNotPressed
    } ButtonState;
    
    typedef enum tagButtonStatus {
        kButtonStatusEnabled,
        kButtonStatusDisabled
    } ButtonStatus;
    
    @interface spuButton : CCSprite <CCTargetedTouchDelegate> {
    @private
        ButtonState buttonState;
        CCTexture2D *buttonNormal;
        CCTexture2D *buttonLit;
        ButtonStatus buttonStatus;  
    }
    
    @property(nonatomic, readonly) CGRect rect;
    
    + (id)spuButtonWithTexture:(CCTexture2D *)normalTexture;
    
    - (void)setNormalTexture:(CCTexture2D *)normalTexture;
    - (void)setLitTexture:(CCTexture2D *)litTexture;
    - (BOOL)isPressed;
    - (BOOL)isNotPressed;
    - (void)makeDisabled;
    - (void)makeEnabled;
    - (BOOL)isEnabled;
    - (BOOL)isDisabled;
    - (void)makeLit;
    - (void)makeNormal;
    - (void)dealloc;
    
    @end
    

    spuButton.m

    #import "spuButton.h"
    #import "cocos2d.h"
    
    @implementation spuButton
    
    - (CGRect)rect {
        CGSize s = [self.texture contentSize];
        return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
    }
    
    + (id)spuButtonWithTexture:(CCTexture2D *)normalTexture {
        return [[[self alloc] initWithTexture:normalTexture] autorelease];
    }
    
    - (void)setNormalTexture:(CCTexture2D *)normalTexture {
        buttonNormal = normalTexture;
    }
    - (void)setLitTexture:(CCTexture2D *)litTexture {
        buttonLit = litTexture;
    }
    
    - (BOOL)isPressed {
        if (buttonState== kButtonStateNotPressed) return NO;
        if (buttonState== kButtonStatePressed) return YES;
        return NO;
    }
    
    - (BOOL)isNotPressed {
        if (buttonState== kButtonStateNotPressed) return YES;
        if (buttonState== kButtonStatePressed) return NO;
        return YES;
    }
    
    - (void)makeDisabled {
        buttonStatus = kButtonStatusDisabled;
        buttonState= kButtonStateNotPressed;
        [self makeNormal];
    }
    - (void)makeEnabled {
        buttonStatus = kButtonStatusEnabled;
        buttonState= kButtonStateNotPressed;
        [self makeNormal];
    }
    
    - (BOOL)isEnabled {
        if (buttonStatus== kButtonStatusDisabled) return NO;
        if (buttonStatus== kButtonStatusEnabled) return YES;
        return NO;
    }
    
    - (BOOL)isDisabled {
        if (buttonStatus== kButtonStatusEnabled) return NO;
        if (buttonStatus== kButtonStatusDisabled) return YES;
        return YES;
    }
    
    - (void)makeLit {
        [self setTexture:buttonLit];
    }
    
    - (void)makeNormal {
        [self setTexture:buttonNormal];
    }
    
    - (id)initWithTexture:(CCTexture2D *)aTexture {
        if ((self = [super initWithTexture:aTexture]) ) {       
            buttonState = kButtonStateNotPressed;
            buttonStatus = kButtonStatusEnabled;
        }
        return self;
    }
    
    - (void)onEnter {
        if (buttonStatus == kButtonStatusDisabled) return;
        [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
        [super onEnter];
    }
    
    - (void)onExit {
        if (buttonStatus == kButtonStatusDisabled) return;
        [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
        [super onExit];
    }   
    
    - (BOOL)containsTouchLocation:(UITouch *)touch {
        return CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]);
    }
    
    - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
        if (buttonStatus == kButtonStatusDisabled) return NO;
        if (buttonState== kButtonStatePressed) return NO;
        if ( ![self containsTouchLocation:touch] ) return NO;
    
        buttonState= kButtonStatePressed;
        [self makeLit];
    
        return YES;
    }
    
    - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
        // If it weren't for the TouchDispatcher, you would need to keep a reference
        // to the touch from touchBegan and check that the current touch is the same
        // as that one.
        // Actually, it would be even more complicated since in the Cocos dispatcher
        // you get NSSets instead of 1 UITouch, so you'd need to loop through the set
        // in each touchXXX method.
    
        if (buttonStatus == kButtonStatusDisabled) return;
        if ([self containsTouchLocation:touch]) return;
    
        buttonState= kButtonStateNotPressed;
        [self makeNormal];
    }
    
    - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
        if (buttonStatus == kButtonStatusDisabled) return;
    
        buttonState= kButtonStateNotPressed;
        [self makeNormal];
    }
    
    - (void)dealloc {
        [buttonNormal release];
        [buttonLit release];
        [super dealloc];
    }
    
    @end
    

    HelloWorldScene.m (Just my tick: method to keep my other functions from confusing the example)

    -(void)tick:(ccTime)dt {
        if ([[_otherControlsArray objectAtIndex:0] wasPressed]) {
            [[_otherControlsArray objectAtIndex:0] setWasPressed:NO];
            [self removeChild:[_otherControlsArray objectAtIndex:0] cleanup:YES];
            [self addChild:[_otherControlsArray objectAtIndex:1]];
            NSLog(@"Play");
    
            _gameHasNotBeenPlayedYet = NO;
            Snarfle_s_PowerUPAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
            [delegate makeNotPaused];
            [self gameLogic];
        }
    
        if (_gameHasNotBeenPlayedYet) {
            return;
        }
    
        if (_buttonsPressedAndReleased > 0) {  //respond to button(s) released and reset
            NSLog(@"Buttons Pressed and Released-->%d",_buttonsPressedAndReleased);
            if ([self checkButtons:_buttonsPressedAndReleased]);
            _buttonsPressed = 0;
            _buttonsPressedAndReleased = 0;
    
            return;
        }
        if (_buttonsPressed <= 4) { // two buttons have not already been pressed
            for (spuButton *aButton in _fourButtonsArray) {
                if ([aButton isNotPressed]) continue; //this button is not pressed
                if (_buttonsPressed == 0) { //this button is pressed and no other buttons have been pressed
                    _buttonsPressed = aButton.tag;
                    continue;
                }
                //this button is pressed while another has been pressed
                //figure out which two buttons have been pressed
                if (_buttonsPressed == 1) {  //red plus another
                    switch (aButton.tag) {
                        case 2:   //blue
                            _buttonsPressed = 5;
                            [[_fourButtonsArray objectAtIndex:2] makeDisabled];
                            [[_fourButtonsArray objectAtIndex:3] makeDisabled];
                            break;
                        case 3:  //green
                            _buttonsPressed = 6;
                            [[_fourButtonsArray objectAtIndex:1] makeDisabled];
                            [[_fourButtonsArray objectAtIndex:3] makeDisabled];
                            break;
                        case 4:  //yellow
                            _buttonsPressed = 7;
                            [[_fourButtonsArray objectAtIndex:1] makeDisabled];
                            [[_fourButtonsArray objectAtIndex:2] makeDisabled];
                            break;
                        default:
                            _buttonsPressed = 1;
                            break;
                    }
                }
                if (_buttonsPressed == 2) {  //blue plus another
                    switch (aButton.tag) {
                        case 1:   //red
                            _buttonsPressed = 5;
                            [[_fourButtonsArray objectAtIndex:2] makeDisabled];
                            [[_fourButtonsArray objectAtIndex:3] makeDisabled];
                            break;
                        case 3:  //green
                            _buttonsPressed = 8;
                            [[_fourButtonsArray objectAtIndex:0] makeDisabled];
                            [[_fourButtonsArray objectAtIndex:3] makeDisabled];
                            break;
                        case 4:  //yellow
                            _buttonsPressed = 9;
                            [[_fourButtonsArray objectAtIndex:0] makeDisabled];
                            [[_fourButtonsArray objectAtIndex:2] makeDisabled];
                            break;
                        default:
                            _buttonsPressed = 2;
                            break;
                    }
                }
                if (_buttonsPressed == 3) {  //green plus another
                    switch (aButton.tag) {
                        case 1:   //red
                            _buttonsPressed = 6;
                            [[_fourButtonsArray objectAtIndex:1] makeDisabled];
                            [[_fourButtonsArray objectAtIndex:3] makeDisabled];
                            break;
                        case 2:  //blue
                            _buttonsPressed = 8;
                            [[_fourButtonsArray objectAtIndex:0] makeDisabled];
                            [[_fourButtonsArray objectAtIndex:3] makeDisabled];
                            break;
                        case 4:  //yellow
                            _buttonsPressed = 10;
                            [[_fourButtonsArray objectAtIndex:0] makeDisabled];
                            [[_fourButtonsArray objectAtIndex:1] makeDisabled];
                            break;
                        default:
                            _buttonsPressed = 3;
                            break;
                    }
                }
                if (_buttonsPressed == 4) {  //yellow plus another
                    switch (aButton.tag) {
                        case 1:   //red
                            _buttonsPressed = 7;
                            [[_fourButtonsArray objectAtIndex:1] makeDisabled];
                            [[_fourButtonsArray objectAtIndex:2] makeDisabled];
                            break;
                        case 2:  //blue
                            _buttonsPressed = 9;
                            [[_fourButtonsArray objectAtIndex:0] makeDisabled];
                            [[_fourButtonsArray objectAtIndex:2] makeDisabled];
                            break;
                        case 3:  //green
                            _buttonsPressed = 10;
                            [[_fourButtonsArray objectAtIndex:0] makeDisabled];
                            [[_fourButtonsArray objectAtIndex:1] makeDisabled];
                            break;
                        default:
                            _buttonsPressed = 4;
                            break;
                    }
                }
                if (_buttonsPressed > 4) break;  //more than one has been pressed and identified
            }
        }
        //now we know what buttons have been pressed now check to see if they have been released
        //if more than one has been pressed disable the other two
        //also if more than one has been pressed and one of them gets released disable the released one but keep it lit
        switch (_buttonsPressed) {
            case 1:  //red
                if ([[_fourButtonsArray objectAtIndex:0] isNotPressed]) _buttonsPressedAndReleased = 1;
                break;
            case 2:  //blue
                if ([[_fourButtonsArray objectAtIndex:1] isNotPressed]) _buttonsPressedAndReleased = 2;
                break;
            case 3:  //green
                if ([[_fourButtonsArray objectAtIndex:2] isNotPressed]) _buttonsPressedAndReleased = 3;
                break;
            case 4:  //yellow
                if ([[_fourButtonsArray objectAtIndex:3] isNotPressed]) _buttonsPressedAndReleased = 4;
                break;
            case 5:  //red & blue
                if (([[_fourButtonsArray objectAtIndex:0] isNotPressed]) && ([[_fourButtonsArray objectAtIndex:1] isNotPressed])) _buttonsPressedAndReleased = 5;
                else {
                    if ([[_fourButtonsArray objectAtIndex:0] isNotPressed]) {
                        [[_fourButtonsArray objectAtIndex:0] makeDisabled];
                        [[_fourButtonsArray objectAtIndex:0] makeLit];
                    }
                    if ([[_fourButtonsArray objectAtIndex:1] isNotPressed]) {
                        [[_fourButtonsArray objectAtIndex:1] makeDisabled];
                        [[_fourButtonsArray objectAtIndex:1] makeLit];
                    }
                }
                break;
            case 6:  //red & green
                if (([[_fourButtonsArray objectAtIndex:0] isNotPressed]) && ([[_fourButtonsArray objectAtIndex:2] isNotPressed])) _buttonsPressedAndReleased = 6;
                else {
                    if ([[_fourButtonsArray objectAtIndex:0] isNotPressed]) {
                        [[_fourButtonsArray objectAtIndex:0] makeDisabled];
                        [[_fourButtonsArray objectAtIndex:0] makeLit];
                    }
                    if ([[_fourButtonsArray objectAtIndex:2] isNotPressed]) {
                        [[_fourButtonsArray objectAtIndex:2] makeDisabled];
                        [[_fourButtonsArray objectAtIndex:2] makeLit];
                    }
                }
                break;
            case 7:  //red & yellow
                if (([[_fourButtonsArray objectAtIndex:0] isNotPressed]) && ([[_fourButtonsArray objectAtIndex:3] isNotPressed])) _buttonsPressedAndReleased = 7;
                else {
                    if ([[_fourButtonsArray objectAtIndex:0] isNotPressed]) {
                        [[_fourButtonsArray objectAtIndex:0] makeDisabled];
                        [[_fourButtonsArray objectAtIndex:0] makeLit];
                    }
                    if ([[_fourButtonsArray objectAtIndex:3] isNotPressed]) {
                        [[_fourButtonsArray objectAtIndex:3] makeDisabled];
                        [[_fourButtonsArray objectAtIndex:3] makeLit];
                    }
                }
                break;
            case 8:  //blue & green
                if (([[_fourButtonsArray objectAtIndex:1] isNotPressed]) && ([[_fourButtonsArray objectAtIndex:2] isNotPressed])) _buttonsPressedAndReleased = 8;
                else {
                    if ([[_fourButtonsArray objectAtIndex:1] isNotPressed]) {
                        [[_fourButtonsArray objectAtIndex:1] makeDisabled];
                        [[_fourButtonsArray objectAtIndex:1] makeLit];
                    }
                    if ([[_fourButtonsArray objectAtIndex:2] isNotPressed]) {
                        [[_fourButtonsArray objectAtIndex:2] makeDisabled];
                        [[_fourButtonsArray objectAtIndex:2] makeLit];
                    }
                }
                break;
            case 9:  //blue & yellow
                if (([[_fourButtonsArray objectAtIndex:1] isNotPressed]) && ([[_fourButtonsArray objectAtIndex:3] isNotPressed])) _buttonsPressedAndReleased = 9;
                else {
                    if ([[_fourButtonsArray objectAtIndex:1] isNotPressed]) {
                        [[_fourButtonsArray objectAtIndex:1] makeDisabled];
                        [[_fourButtonsArray objectAtIndex:1] makeLit];
                    }
                    if ([[_fourButtonsArray objectAtIndex:3] isNotPressed]) {
                        [[_fourButtonsArray objectAtIndex:3] makeDisabled];
                        [[_fourButtonsArray objectAtIndex:3] makeLit];
                    }
                }
                break;
            case 10:  //green & yellow
                if (([[_fourButtonsArray objectAtIndex:2] isNotPressed]) && ([[_fourButtonsArray objectAtIndex:3] isNotPressed])) _buttonsPressedAndReleased = 10;
                else {
                    if ([[_fourButtonsArray objectAtIndex:2] isNotPressed]) {
                        [[_fourButtonsArray objectAtIndex:2] makeDisabled];
                        [[_fourButtonsArray objectAtIndex:2] makeLit];
                    }
                    if ([[_fourButtonsArray objectAtIndex:3] isNotPressed]) {
                        [[_fourButtonsArray objectAtIndex:3] makeDisabled];
                        [[_fourButtonsArray objectAtIndex:3] makeLit];
                    }
                }
                break;
            default:
                _buttonsPressedAndReleased = 0;
                break;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an app for the iPhone using cocos2d where I have 4
I'm writing an iPhone app, and I want to handle multitouches. I'm using cocos2d
I'm writing an iPhone App that relies on getting the device location. Management have
I have a 32bit iMac that I am writing an iPhone app in Xcode
I'm writing a reminders app for iPhone that displays reminders using local notifications. If
I am writing a game for the iPhone/iPodTouch (using Cocos2d) and I have noticed
I'm writing an iPhone app that's using Facebook Connect. While testing, you normally embed
I'm writing an iPhone app for a client, and they have requested a feature
I'm writing a Navigation-Based iPhone app, and I'd like to have a UIToolBar docked
I'm writing an iPhone app that includes in-app purchasing. It downloads a zip file,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.