单击按钮,移动水龙头,然后按另一个按钮
Posted
技术标签:
【中文标题】单击按钮,移动水龙头,然后按另一个按钮【英文标题】:With single tap on button, move tap, and press another button 【发布时间】:2014-05-18 14:17:05 【问题描述】:我在 ios 设备上使用 Cocos2D 3.0。
在我的 HUD 类中,我有 4 个按钮(SneakyInput 库),左右 ButtonA 和 ButtonB,一切正常,但是当我移动手指(不抬起屏幕)时,按钮被按住。
现在发生的情况是,当我按下一个按钮并移动我的手指按钮时,直到您抬起手指为止。
我想要:
当我触摸 buttonA 并将手指移到 buttonB 时,buttonB 按钮起作用。
我见过很多游戏都可以毫无问题地实现这一点,但现在我做不到。 >_<.>
我的游戏运行良好,我只是想实现这个选项以使其更易于管理。
下面是 Sneaky 按钮的实现代码:
-(void) loadControls
//Screen Size
CGSize winSize = [[CCDirector sharedDirector] viewSize];
//Load Spritesheets
CCSprite *botonA = [CCSprite spriteWithImageNamed:@"botona.png"];
CCSprite *left = [CCSprite spriteWithImageNamed:@"izquierda.png"];
//Boton A
SneakyButtonSkinnedBase *baseBotonA = [[SneakyButtonSkinnedBase alloc] init];
baseBotonA.position = ccp(winSize.width - botonA.contentSize.width ,botonA.contentSize.height);
baseBotonA.defaultSprite = [CCSprite spriteWithImageNamed:@"botona.png"];
baseBotonA.activatedSprite = [CCSprite spriteWithImageNamed:@"botona.png"];
baseBotonA.pressSprite = [CCSprite spriteWithImageNamed:@"botonaselected.png"];
baseBotonA.button = [[SneakyButton alloc] initWithRect:CGRectMake(0, 0, 100, 100)]; //Ajustar
buttonA = baseBotonA.button;
buttonA.isToggleable = YES;
//Boton B
SneakyButtonSkinnedBase *baseBotonB = [[SneakyButtonSkinnedBase alloc] init];
baseBotonB.position = ccp(winSize.width - (botonA.contentSize.width * 2) - (botonA.contentSize.width / 2) ,botonA.contentSize.height);
baseBotonB.defaultSprite = [CCSprite spriteWithImageNamed:@"botonb.png"];
baseBotonB.activatedSprite = [CCSprite spriteWithImageNamed:@"botonb.png"];
baseBotonB.pressSprite = [CCSprite spriteWithImageNamed:@"botonbselected.png"];
baseBotonB.button = [[SneakyButton alloc] initWithRect:CGRectMake(0, 0, 100, 100)];
buttonB = baseBotonB.button;
buttonB.isToggleable = YES;
//Boton Left
SneakyButtonSkinnedBase *baseBotonLeft = [[SneakyButtonSkinnedBase alloc] init];
baseBotonLeft.position = ccp(left.contentSize.width,left.contentSize.height);
baseBotonLeft.defaultSprite = [CCSprite spriteWithImageNamed:@"izquierda.png"];
baseBotonLeft.activatedSprite = [CCSprite spriteWithImageNamed:@"izquierda.png"];
baseBotonLeft.pressSprite = [CCSprite spriteWithImageNamed:@"izquierdaselected.png"];
baseBotonLeft.button = [[SneakyButton alloc] initWithRect:CGRectMake(0, 0, 100, 100)];
buttonLeft = baseBotonLeft.button;
buttonLeft.isToggleable = YES;
//Boton Right
SneakyButtonSkinnedBase *baseBotonRight = [[SneakyButtonSkinnedBase alloc] init];
baseBotonRight.position = ccp((left.contentSize.width * 2) + (left.contentSize.width / 2),left.contentSize.height);
baseBotonRight.defaultSprite = [CCSprite spriteWithImageNamed:@"derecha.png"];
baseBotonRight.activatedSprite = [CCSprite spriteWithImageNamed:@"derecha.png"];
baseBotonRight.pressSprite = [CCSprite spriteWithImageNamed:@"derechaselected.png"];
baseBotonRight.button = [[SneakyButton alloc] initWithRect:CGRectMake(0, 0, 100, 100)];
buttonRight = baseBotonRight.button;
buttonRight.isToggleable = YES;
[layoutBox addChild:baseBotonA];
[layoutBox addChild:baseBotonB];
[layoutBox addChild:baseBotonLeft];
[layoutBox addChild:baseBotonRight];
[self addChild:layoutBox];
对不起,我的英语不好,我在没有翻译的情况下尝试:(
【问题讨论】:
【参考方案1】:将按钮上的claimsUserInteraction
属性设置为NO
。
来自CCResponder
的 Cocos2D 文档:
/**
* Locks the touch to the node if touch moved outside
* If a node claims user interaction, the touch will continue to be sent to the node, no matter where the touch is moved
* If the node does not claim user interaction, a touch will be cancelled, if moved outside the nodes detection area
* If the node does not claim user interaction, and a touch is moved from outside the nodes detection area, to inside, a touchBegan will be generated.
*/
@property (nonatomic, assign) BOOL claimsUserInteraction;
【讨论】:
以上是关于单击按钮,移动水龙头,然后按另一个按钮的主要内容,如果未能解决你的问题,请参考以下文章