同步精灵触摸检测-iPhone和;cocos2d
我正在用cocos2d为iPhone编写一个应用程序,其中有4个精灵是按钮。我想我应该通过使用两个sprite来实现按钮功能,使用不同的.png文件来表示按下/未按下的状态。我认为这比使用动画要好。除了检测按钮何时按下和释放外,我还需要检测两个按钮组合何时按下/释放(如和弦)。我甚至可以把这个结转到3键和4键组合。我想实现的另一个功能是检测一个按钮是否发出了一个轻弹,并用另一个动作序列进行响应 这方面的最佳方法是什么?我会为四个按钮(它们是正方形)和燕子触摸使用边界矩形/或精灵吗?然后我会使用较小的精灵矩形来确定触摸发生在哪一个上吗?或者我会让单独的精灵矩形进行检测,然后让他们检查其他按钮是否同时按下然后释放?对于这两种方式,我会使用Bool状态变量吗 我使用CCMenu和CCMenuitem,通过为每个按钮精灵制作一个菜单/菜单项,使其工作得非常好;但是,我不知道如何检测一个菜单中的菜单项是否同时被按下和释放,另一个菜单中的菜单项是否同时被按下和释放。我使用单独的菜单,因为如果我对多个菜单项使用相同的菜单,那么一次只能按一个。我说它工作得很好,这意味着它可以使用单个和多个印刷机,但它会将多个印刷机视为两个连续印刷机,而不是同时印刷机。IE:单次按下导致运行单个动作序列(如预期),同时按下组合导致运行每个按钮的单个按钮动作序列,就像我分别按下每个按钮一样(我希望它运行特定于该按钮组合的一个动作序列)。这就是为什么我决定最好尝试实现自己的按钮系统,而不是使用CCMenu系统。我离开CCMenu对吗 很抱歉描述得太长,但它确实说得很具体。(B)同步精灵触摸检测-iPhone和;cocos2d,iphone,button,cocos2d-iphone,touch,sprite,Iphone,Button,Cocos2d Iphone,Touch,Sprite,我正在用cocos2d为iPhone编写一个应用程序,其中有4个精灵是按钮。我想我应该通过使用两个sprite来实现按钮功能,使用不同的.png文件来表示按下/未按下的状态。我认为这比使用动画要好。除了检测按钮何时按下和释放外,我还需要检测两个按钮组合何时按下/释放(如和弦)。我甚至可以把这个结转到3键和4键组合。我想实现的另一个功能是检测一个按钮是否发出了一个轻弹,并用另一个动作序列进行响应 这方面的最佳方法是什么?我会为四个按钮(它们是正方形)和燕子触摸使用边界矩形/或精灵吗?然后我会使用较
任何关于其他人如何实现或将如何实现这一点的见解都将不胜感激 下面是我如何实现这一点的,以防其他人尝试这样做 spuButton.h(一个CCSprite子类) HelloWorldScene.m(只是我的勾号:方法,以防止我的其他函数混淆示例)
-(无效)勾选:(ccTime)dt{
如果([[u otherControlsArray objectAtIndex:0]被按下]){
[[u otherControlsArray objectAtIndex:0]设置被按下:否];
[self-removeChild:[\u otherControlsArray对象索引:0]清理:是];
[self addChild:[u otherControlsArray objectAtIndex:1];
NSLog(“播放”);
_gameHasNotBeenPlayedYet=否;
Snarfle_s_PowerUPAppDelegate*delegate=[[UIApplication sharedApplication]delegate];
[delegate makeNotPaused];
[自我博弈逻辑];
}
如果(_gamehasnobeenplayet){
返回;
}
如果(_buttonPressedAndReleased>0){//响应释放的按钮并重置
NSLog(@“按钮按下并释放-->%d”,u按钮按下并释放);
如果([自检按钮:_按钮按下并释放]);
_按钮压力=0;
_buttonsPressedAndReleased=0;
返回;
}
如果(_buttonpressed4)break;//按下并标识了多个按钮
}
}
//现在我们知道哪些按钮被按下了,现在检查它们是否被释放
//如果按下了多个按钮,则禁用其他两个
//此外,如果按下了多个按钮,其中一个被释放,则禁用已释放的按钮,但保持点亮
开关(按下按钮){
案例1://红色
如果([[[u fourButtonsArray objectAtIndex:0]未按下])\u buttonsPressedReleased=1;
打破
案例2://蓝色
如果([[[u fourButtonsArray objectAtIndex:1]未按下])\u buttonsPressedReleased=2;
打破
案例3://绿色
如果([[[u fourButtonsArray objectAtIndex:2]未按下])\u buttonsPressedReleased=3;
打破
案例4://黄色
如果([[[u fourButtonsArray objectAtIndex:3]未按下])\u buttonsPressedReleased=4;
打破
案例5://红色和蓝色
如果([[[u-fourButtonsArray objectAtIndex:0]未按下])和([[u-fourButtonsArray objectAtIndex:1]未按下])\u-ButtonSpresseandReleased=5;
否则{
如果([[\u fourButtonsArray对象索引:0]未按下]){
[[u fourButtonsArray objectAtIndex:0]makeDisabled];
[[u fourButtonsArray objectAtIndex:0]makeLit];
}
如果([[[u fourButtonsArray objectAtIndex:1]未按下]){
[[u fourButtonsArray对象索引:1]makeDisabled];
[[u fourButtonsArray objectAtIndex:1]makeLit];
}
}
打破
案例6://红色和绿色
如果([[[u-fourButtonsArray-objectAtIndex:0]未按下])和([[u-fourButtonsArray-objectAtIndex:2]未按下])\u-ButtonSpresseandReleased=6;
否则{
如果([[\u fourButtonsArray对象索引:0]未按下]){
[[u fourButtonsArray objectAtIndex:0]makeDisabled];
[[u fourButtonsArray objectAtIndex:0]makeLit];
}
如果([[u fourButtonsArray objectAtIndex:2]未按下]){
[[u fourbuttonArray对象索引:2]makeDisabled];
[[u fourButtonsArray objectAtIndex:2]makeLit];
}
}
打破
案例7://红色和黄色
如果([[u]fourButtonsAr
#import <Foundation/Foundation.h>
#import "cocos2d.h"
typedef enum tagButtonState {
kButtonStatePressed,
kButtonStateNotPressed
} ButtonState;
typedef enum tagButtonStatus {
kButtonStatusEnabled,
kButtonStatusDisabled
} ButtonStatus;
@interface spuButton : CCSprite <CCTargetedTouchDelegate> {
@private
ButtonState buttonState;
CCTexture2D *buttonNormal;
CCTexture2D *buttonLit;
ButtonStatus buttonStatus;
}
@property(nonatomic, readonly) CGRect rect;
+ (id)spuButtonWithTexture:(CCTexture2D *)normalTexture;
- (void)setNormalTexture:(CCTexture2D *)normalTexture;
- (void)setLitTexture:(CCTexture2D *)litTexture;
- (BOOL)isPressed;
- (BOOL)isNotPressed;
- (void)makeDisabled;
- (void)makeEnabled;
- (BOOL)isEnabled;
- (BOOL)isDisabled;
- (void)makeLit;
- (void)makeNormal;
- (void)dealloc;
@end
#import "spuButton.h"
#import "cocos2d.h"
@implementation spuButton
- (CGRect)rect {
CGSize s = [self.texture contentSize];
return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
}
+ (id)spuButtonWithTexture:(CCTexture2D *)normalTexture {
return [[[self alloc] initWithTexture:normalTexture] autorelease];
}
- (void)setNormalTexture:(CCTexture2D *)normalTexture {
buttonNormal = normalTexture;
}
- (void)setLitTexture:(CCTexture2D *)litTexture {
buttonLit = litTexture;
}
- (BOOL)isPressed {
if (buttonState== kButtonStateNotPressed) return NO;
if (buttonState== kButtonStatePressed) return YES;
return NO;
}
- (BOOL)isNotPressed {
if (buttonState== kButtonStateNotPressed) return YES;
if (buttonState== kButtonStatePressed) return NO;
return YES;
}
- (void)makeDisabled {
buttonStatus = kButtonStatusDisabled;
buttonState= kButtonStateNotPressed;
[self makeNormal];
}
- (void)makeEnabled {
buttonStatus = kButtonStatusEnabled;
buttonState= kButtonStateNotPressed;
[self makeNormal];
}
- (BOOL)isEnabled {
if (buttonStatus== kButtonStatusDisabled) return NO;
if (buttonStatus== kButtonStatusEnabled) return YES;
return NO;
}
- (BOOL)isDisabled {
if (buttonStatus== kButtonStatusEnabled) return NO;
if (buttonStatus== kButtonStatusDisabled) return YES;
return YES;
}
- (void)makeLit {
[self setTexture:buttonLit];
}
- (void)makeNormal {
[self setTexture:buttonNormal];
}
- (id)initWithTexture:(CCTexture2D *)aTexture {
if ((self = [super initWithTexture:aTexture]) ) {
buttonState = kButtonStateNotPressed;
buttonStatus = kButtonStatusEnabled;
}
return self;
}
- (void)onEnter {
if (buttonStatus == kButtonStatusDisabled) return;
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
[super onEnter];
}
- (void)onExit {
if (buttonStatus == kButtonStatusDisabled) return;
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
[super onExit];
}
- (BOOL)containsTouchLocation:(UITouch *)touch {
return CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]);
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
if (buttonStatus == kButtonStatusDisabled) return NO;
if (buttonState== kButtonStatePressed) return NO;
if ( ![self containsTouchLocation:touch] ) return NO;
buttonState= kButtonStatePressed;
[self makeLit];
return YES;
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
// If it weren't for the TouchDispatcher, you would need to keep a reference
// to the touch from touchBegan and check that the current touch is the same
// as that one.
// Actually, it would be even more complicated since in the Cocos dispatcher
// you get NSSets instead of 1 UITouch, so you'd need to loop through the set
// in each touchXXX method.
if (buttonStatus == kButtonStatusDisabled) return;
if ([self containsTouchLocation:touch]) return;
buttonState= kButtonStateNotPressed;
[self makeNormal];
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
if (buttonStatus == kButtonStatusDisabled) return;
buttonState= kButtonStateNotPressed;
[self makeNormal];
}
- (void)dealloc {
[buttonNormal release];
[buttonLit release];
[super dealloc];
}
@end
-(void)tick:(ccTime)dt {
if ([[_otherControlsArray objectAtIndex:0] wasPressed]) {
[[_otherControlsArray objectAtIndex:0] setWasPressed:NO];
[self removeChild:[_otherControlsArray objectAtIndex:0] cleanup:YES];
[self addChild:[_otherControlsArray objectAtIndex:1]];
NSLog(@"Play");
_gameHasNotBeenPlayedYet = NO;
Snarfle_s_PowerUPAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate makeNotPaused];
[self gameLogic];
}
if (_gameHasNotBeenPlayedYet) {
return;
}
if (_buttonsPressedAndReleased > 0) { //respond to button(s) released and reset
NSLog(@"Buttons Pressed and Released-->%d",_buttonsPressedAndReleased);
if ([self checkButtons:_buttonsPressedAndReleased]);
_buttonsPressed = 0;
_buttonsPressedAndReleased = 0;
return;
}
if (_buttonsPressed <= 4) { // two buttons have not already been pressed
for (spuButton *aButton in _fourButtonsArray) {
if ([aButton isNotPressed]) continue; //this button is not pressed
if (_buttonsPressed == 0) { //this button is pressed and no other buttons have been pressed
_buttonsPressed = aButton.tag;
continue;
}
//this button is pressed while another has been pressed
//figure out which two buttons have been pressed
if (_buttonsPressed == 1) { //red plus another
switch (aButton.tag) {
case 2: //blue
_buttonsPressed = 5;
[[_fourButtonsArray objectAtIndex:2] makeDisabled];
[[_fourButtonsArray objectAtIndex:3] makeDisabled];
break;
case 3: //green
_buttonsPressed = 6;
[[_fourButtonsArray objectAtIndex:1] makeDisabled];
[[_fourButtonsArray objectAtIndex:3] makeDisabled];
break;
case 4: //yellow
_buttonsPressed = 7;
[[_fourButtonsArray objectAtIndex:1] makeDisabled];
[[_fourButtonsArray objectAtIndex:2] makeDisabled];
break;
default:
_buttonsPressed = 1;
break;
}
}
if (_buttonsPressed == 2) { //blue plus another
switch (aButton.tag) {
case 1: //red
_buttonsPressed = 5;
[[_fourButtonsArray objectAtIndex:2] makeDisabled];
[[_fourButtonsArray objectAtIndex:3] makeDisabled];
break;
case 3: //green
_buttonsPressed = 8;
[[_fourButtonsArray objectAtIndex:0] makeDisabled];
[[_fourButtonsArray objectAtIndex:3] makeDisabled];
break;
case 4: //yellow
_buttonsPressed = 9;
[[_fourButtonsArray objectAtIndex:0] makeDisabled];
[[_fourButtonsArray objectAtIndex:2] makeDisabled];
break;
default:
_buttonsPressed = 2;
break;
}
}
if (_buttonsPressed == 3) { //green plus another
switch (aButton.tag) {
case 1: //red
_buttonsPressed = 6;
[[_fourButtonsArray objectAtIndex:1] makeDisabled];
[[_fourButtonsArray objectAtIndex:3] makeDisabled];
break;
case 2: //blue
_buttonsPressed = 8;
[[_fourButtonsArray objectAtIndex:0] makeDisabled];
[[_fourButtonsArray objectAtIndex:3] makeDisabled];
break;
case 4: //yellow
_buttonsPressed = 10;
[[_fourButtonsArray objectAtIndex:0] makeDisabled];
[[_fourButtonsArray objectAtIndex:1] makeDisabled];
break;
default:
_buttonsPressed = 3;
break;
}
}
if (_buttonsPressed == 4) { //yellow plus another
switch (aButton.tag) {
case 1: //red
_buttonsPressed = 7;
[[_fourButtonsArray objectAtIndex:1] makeDisabled];
[[_fourButtonsArray objectAtIndex:2] makeDisabled];
break;
case 2: //blue
_buttonsPressed = 9;
[[_fourButtonsArray objectAtIndex:0] makeDisabled];
[[_fourButtonsArray objectAtIndex:2] makeDisabled];
break;
case 3: //green
_buttonsPressed = 10;
[[_fourButtonsArray objectAtIndex:0] makeDisabled];
[[_fourButtonsArray objectAtIndex:1] makeDisabled];
break;
default:
_buttonsPressed = 4;
break;
}
}
if (_buttonsPressed > 4) break; //more than one has been pressed and identified
}
}
//now we know what buttons have been pressed now check to see if they have been released
//if more than one has been pressed disable the other two
//also if more than one has been pressed and one of them gets released disable the released one but keep it lit
switch (_buttonsPressed) {
case 1: //red
if ([[_fourButtonsArray objectAtIndex:0] isNotPressed]) _buttonsPressedAndReleased = 1;
break;
case 2: //blue
if ([[_fourButtonsArray objectAtIndex:1] isNotPressed]) _buttonsPressedAndReleased = 2;
break;
case 3: //green
if ([[_fourButtonsArray objectAtIndex:2] isNotPressed]) _buttonsPressedAndReleased = 3;
break;
case 4: //yellow
if ([[_fourButtonsArray objectAtIndex:3] isNotPressed]) _buttonsPressedAndReleased = 4;
break;
case 5: //red & blue
if (([[_fourButtonsArray objectAtIndex:0] isNotPressed]) && ([[_fourButtonsArray objectAtIndex:1] isNotPressed])) _buttonsPressedAndReleased = 5;
else {
if ([[_fourButtonsArray objectAtIndex:0] isNotPressed]) {
[[_fourButtonsArray objectAtIndex:0] makeDisabled];
[[_fourButtonsArray objectAtIndex:0] makeLit];
}
if ([[_fourButtonsArray objectAtIndex:1] isNotPressed]) {
[[_fourButtonsArray objectAtIndex:1] makeDisabled];
[[_fourButtonsArray objectAtIndex:1] makeLit];
}
}
break;
case 6: //red & green
if (([[_fourButtonsArray objectAtIndex:0] isNotPressed]) && ([[_fourButtonsArray objectAtIndex:2] isNotPressed])) _buttonsPressedAndReleased = 6;
else {
if ([[_fourButtonsArray objectAtIndex:0] isNotPressed]) {
[[_fourButtonsArray objectAtIndex:0] makeDisabled];
[[_fourButtonsArray objectAtIndex:0] makeLit];
}
if ([[_fourButtonsArray objectAtIndex:2] isNotPressed]) {
[[_fourButtonsArray objectAtIndex:2] makeDisabled];
[[_fourButtonsArray objectAtIndex:2] makeLit];
}
}
break;
case 7: //red & yellow
if (([[_fourButtonsArray objectAtIndex:0] isNotPressed]) && ([[_fourButtonsArray objectAtIndex:3] isNotPressed])) _buttonsPressedAndReleased = 7;
else {
if ([[_fourButtonsArray objectAtIndex:0] isNotPressed]) {
[[_fourButtonsArray objectAtIndex:0] makeDisabled];
[[_fourButtonsArray objectAtIndex:0] makeLit];
}
if ([[_fourButtonsArray objectAtIndex:3] isNotPressed]) {
[[_fourButtonsArray objectAtIndex:3] makeDisabled];
[[_fourButtonsArray objectAtIndex:3] makeLit];
}
}
break;
case 8: //blue & green
if (([[_fourButtonsArray objectAtIndex:1] isNotPressed]) && ([[_fourButtonsArray objectAtIndex:2] isNotPressed])) _buttonsPressedAndReleased = 8;
else {
if ([[_fourButtonsArray objectAtIndex:1] isNotPressed]) {
[[_fourButtonsArray objectAtIndex:1] makeDisabled];
[[_fourButtonsArray objectAtIndex:1] makeLit];
}
if ([[_fourButtonsArray objectAtIndex:2] isNotPressed]) {
[[_fourButtonsArray objectAtIndex:2] makeDisabled];
[[_fourButtonsArray objectAtIndex:2] makeLit];
}
}
break;
case 9: //blue & yellow
if (([[_fourButtonsArray objectAtIndex:1] isNotPressed]) && ([[_fourButtonsArray objectAtIndex:3] isNotPressed])) _buttonsPressedAndReleased = 9;
else {
if ([[_fourButtonsArray objectAtIndex:1] isNotPressed]) {
[[_fourButtonsArray objectAtIndex:1] makeDisabled];
[[_fourButtonsArray objectAtIndex:1] makeLit];
}
if ([[_fourButtonsArray objectAtIndex:3] isNotPressed]) {
[[_fourButtonsArray objectAtIndex:3] makeDisabled];
[[_fourButtonsArray objectAtIndex:3] makeLit];
}
}
break;
case 10: //green & yellow
if (([[_fourButtonsArray objectAtIndex:2] isNotPressed]) && ([[_fourButtonsArray objectAtIndex:3] isNotPressed])) _buttonsPressedAndReleased = 10;
else {
if ([[_fourButtonsArray objectAtIndex:2] isNotPressed]) {
[[_fourButtonsArray objectAtIndex:2] makeDisabled];
[[_fourButtonsArray objectAtIndex:2] makeLit];
}
if ([[_fourButtonsArray objectAtIndex:3] isNotPressed]) {
[[_fourButtonsArray objectAtIndex:3] makeDisabled];
[[_fourButtonsArray objectAtIndex:3] makeLit];
}
}
break;
default:
_buttonsPressedAndReleased = 0;
break;
}
}