2015-01-20 3 views
1

사용자가 CCButton을 배치해야하는 위치에 CCButton을 드래그 할 수있는 기능을 배치해야합니다.touchMoved 동안 CCButton이 작동하지 않습니다.

사용자 지정 클래스를 만들었지 만 문제는 click입니다. 사용자가 단추를 클릭 할 때 단추의 메서드가 호출되지 않습니다.

#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.m

#import "cocos2d.h" 
#import "cocos2d-ui.h" 

@interface touchyButton : CCButton { ... } 
@property (nonatomic, assign) BOOL touchMoved; 
@end 

touchyButton.h

, 우리는 사용자가 버튼을 드래그 할 때 화면에 어디든지 버튼을 이동하려고합니다.

표시해야 할 주 코드에서 버튼을 호출하는 중입니다. 사용자가 버튼을 클릭 할 때

 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 결코 발사하지 않는다. 아무도 대안을 제안 할 수 어떻게 대상 작업을 작동 끌기와 함께 버튼을 구현할 수 있습니다 ..? 어떤 힌트도 감사 할 것입니다.

더 많은 것은 현재 코드에서 버튼의 앵커 포인트를 새 터치 포인트로만 이동할 수 있다는 것입니다. 어떤 방법으로 버튼을 실제 패션으로 옮길 수 있습니까? 현재의 방법은 이동을위한 처음 탭핑 문제를 야기하며, 버튼의 앵커 포인트는 탭 포인트로 점프한다. 당신

이를 ClassA와 ClassB가

이 코드를 추가하여 새로운 클래스를 생성하기위한

+0

이 문제를 해결할 수 없으므로 질문을 종료해야합니다. –

+0

문제를 해결할 수 없으며 개발을 떠났다. –

답변

1

이 코드는 classA.h

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

에게 classA.cpp

cocos2dx하지만 유용
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 
+0

이번 주에 코드를 사용해보고 답변을 알려 드리겠습니다. Class A와 Class B에 대한 설명을 제공하지 않았습니다. Class A는 CCButton이어야합니까? 그리고 클래스 B에 무엇이 있어야합니까? 귀하의 회신에 감사드립니다. –

+0

classA- 구현 CCButton 및 classB는 이동 가능한 클래스입니다. 터치 루프에 따라 클래스 B 위치가 변경됩니다. –

+0

@ Himandhu-kalal : 감사합니다. 이번 주에 이것을 확인하고 알려 드리겠습니다. –