【问题标题】:CCButton not working while touchMoved Implemented实现 touchMoved 时 CCButton 不工作
【发布时间】:2015-01-20 18:09:16
【问题描述】:

我必须将功能放置在可以将 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 不会触发。任何人都可以提出任何替代方案,我可以如何通过目标操作工作来实现具有拖动行为的按钮..?任何提示也将不胜感激。

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

【问题讨论】:

  • 我无法解决这个问题,需要终止这个问题。
  • 无法解决问题并停止开发。

标签: ios drag-and-drop cocos2d-x ccsprite cocos2d-x-3.0


【解决方案1】:

这段代码是 cocos2dx 但对你有用

新建一个类classA和classB

添加此代码 类A.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

【讨论】:

  • 这周我会试试代码,让你知道答案。您没有对A类和B类进行任何澄清。A类应该是CCButton? B类应该是什么?顺便感谢您的回复。
  • classA-implement CCButton and classB是可移动类,你是根据触摸位置改变classB位置。
  • @Himandhu-kalal:谢谢 Himanshu。这周我会检查一下,然后告诉你。
猜你喜欢
  • 1970-01-01
  • 2020-01-30
  • 1970-01-01
  • 2020-07-24
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
  • 1970-01-01
  • 2017-10-25
相关资源
最近更新 更多