CCButton在触摸移动实施时不工作



我必须将功能放置在可以将CCButton拖动到适当位置的位置,无论用户需要放置它们的位置。

我为此创建了一个自定义类click但问题是当用户单击按钮时按钮的方法没有被调用。

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

正如代码所解释的,当用户拖动按钮时,我们只是尝试将按钮移动到屏幕上的任何位置。

需要显示的主代码中调用按钮。

        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:)];

现在,当用户单击按钮时,按钮将进入选定状态,但performRightJump永远不会触发。任何人都可以提出任何替代方案,如何通过目标操作的拖动行为实现按钮..?任何提示也将不胜感激。

还有一件事是,在当前代码中,我只能将按钮的锚点移动到新的触摸点。知道我如何以真实的方式移动按钮吗?当前的方法会导致第一次点击移动的问题,按钮的锚点跳到点击点。

这段代码是cocos2dx,但对你很有用

创建新的类 A 类和类 B

添加此代码类A.h

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

A类.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