【发布时间】: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