简体   繁体   中英

CCButton not working while touchMoved Implemented

I have to place functionality where CCButton can be dragged to proper position, wherever user needs to place them.

I have create a custom class for this but the issue is click method of the button is not being called when user clicks on the button.

touchyButton.h

#import "cocos2d.h"
#import "cocos2d-ui.h"

@interface touchyButton : CCButton { ... }
@property (nonatomic, assign) BOOL touchMoved;
@end

touchyButton.m

#import "touchyButton.h"


@implementation touchyButton

- (void) touchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
    NSLog(@"touchMoved...", nil);
    self.touchMoved = YES;
    self.anchorPoint = ccp(0.5, 0.5);
    CGPoint touchLoc = [touch locationInNode:self.parent];
    //CGPoint inTouchLoc = [self convertToNodeSpace:self.anchorPoint];
    //CGPoint touchP = ccpAdd(touchLoc, inTouchLoc);
    //self.position = [self.parent convertToNodeSpace: touchP];
    self.position = touchLoc;
}

- (void) touchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    self.touchMoved = NO;
    NSLog(@"touchEnded...", nil);
}

@end

As the code explains, we are just trying to move the button wherever on the screen when user drags on the button.

Calling of the button in main code where it needs to be displayed.

        touchyButton *btnRight = [touchyButton buttonWithTitle: @"" spriteFrame:[[CCSprite spriteWithImageNamed: @"arrR.png"] spriteFrame]];
        [btnRight setBackgroundOpacity:0.5f forState: CCControlStateNormal];
        [btnRight setAnchorPoint: ccp(1, 0.5)];
        [btnRight setPosition: ccp(viewS.width - 10.f, viewS.height/2)];
        [self addChild: btnRight];
        [btnRight setTarget:self selector: @selector(performRightJump:)];

Now, when user clicks on the button, the button goes into selected state but performRightJump never fires. Can anyone suggest any alternative how I can implement the button with dragging behaviour with target action working..? Any hint would be appreciated as well.

One more thing is, in current code I can only be able to move the button's anchor point to the new touch point. Any idea how I can move the button in real fashion? The current method causes problem of tapping of first time for move, button's anchor point jumps to the tapped point.

this code is cocos2dx but useful for you

create a new class classA and classB

add this code classA.h

{
//here implement 
 CC_SYNTHESIZE_RETAIN(classB *, _ classB, _ ClassB);
}

classA.cpp

bool classA::init()
{
set_classB(classB::initwithclass());
   this->addChild(get_ClassB(),0);


//here create button
   return true;


}
bool classA:: onTouchBegan (Touch *touch ,Event *event)
{

_classB->setposition(touchLoc); 
  return true;
}
void classA:: onTouchMoved(Touch *touch, Event *event)
{
}
void classA:: onTouchEnded(Touch *touch ,Event *event)
{


}
//as well as this code use in cocos2d

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM