2013-01-08 24 views
1

下面我有一個圖層HelloWorldLayer,其中觸摸可以在任何地方工作,但我希望它只在觸摸圖層中的精靈時才起作用 - turtle檢測到嵌套的自定義精靈圖層上的觸摸

如果我嘗試添加self.isTouchEnabled = YES;CCTurtle層,它說

財產isTouchEnabled對象類型CCTurtle沒有找到

我的輸出如下

2013年1月8日20:30:14.767 FlashToCocARAR [6746:d503] cocos2d:釋放

201 08年3月1日20:30:15.245 FlashToCocosARC [6746:D503]打步行animation2

這裏是我的HelloWorldLayer代碼:

#import "HelloWorldLayer.h" 
#import "CCTurtle.h" 

@implementation HelloWorldLayer 

+(CCScene *) scene 
{ 
    CCScene *scene = [CCScene node]; 
    HelloWorldLayer *layer = [HelloWorldLayer node]; 
    [scene addChild: layer]; 
    return scene; 
} 

-(id) init 
{ 
    if((self=[super init])) { 
     turtle= [[CCTurtle alloc] init]; 
     [turtle setPosition:ccp(300, 100)]; 
     [self addChild:turtle]; 
     ///addChild:child z:z tag:aTag; 
     self.isTouchEnabled = YES; 
     turtle. tag=4; 
    // 

    } 
return self; 
} 



//- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
//{ 
// // Processing all touches for multi-touch support 
// UITouch *touch = [touches anyObject]; 
// if ([[touch view] isKindOfClass:[turtle class]]) { 
//  NSLog(@"[touch view].tag = %d", [touch view].tag); 
//  [self toggleTurtle]; 
// } 
//} 
-(BOOL)containsTouch:(UITouch *)touch { 
    CGRect r=[turtle textureRect]; 
    CGPoint p=[turtle convertTouchToNodeSpace:touch]; 
    return CGRectContainsPoint(r, p); 
} 
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    //////GENERAL TOUCH SCREEN 
    for (UITouch *touch in touches) { 
     CGPoint touchLocation = [touch locationInView:[touch view]]; 
     touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation]; 
     [self toggleTurtle]; 
     ///// 
    } 

} 
-(void) toggleTurtle 
{ 
    NSLog(@"playing walk animation2"); 
    [turtle playAnimation:@"walk_in" loop:NO wait:YES]; 
} 



@end 

//你好world.h

#import "cocos2d.h" 
    #import "CCTurtle.h" 

    @interface HelloWorldLayer : CCLayer 
    { 
    CCTurtle    *turtle; 
    } 

    +(CCScene *) scene; 

    @end 

// CCturtle

#import <Foundation/Foundation.h> 
    #import "FTCCharacter.h" 

    @interface CCTurtle : FTCCharacter <FTCCharacterDelegate, CCTargetedTouchDelegate> 
    { 

    } 
    @end 

我使用cocos2d的cocos2d的V1.0.1(啓用拱),和我在iPad 4.3模擬器測試。 與感謝娜塔莉

香港專業教育學院試圖直接把觸摸到ccturtle.m所以它可以使用CCTargetedTouchDelegate同上,但使用

CCturtle處理自己的觸摸///我改變了文件,這個嘗試以不同的方式發現被觸摸的面積...

- (CGRect)rect 
    { 
    CGSize s = [self.texture contentSize]; 
    return CGRectMake(-s.width/2, -s.height/2, s.width, s.height); 
    } 

    -(BOOL) didTouch: (UITouch*)touch { 
    return CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]); 
    //return CGRectContainsPoint([self rect], [self convertTouchToNodeSpaceAR: touch]); 
    } 

    -(BOOL) ccTouchBegan:(UITouch*)touch withEvent: (UIEvent*)event { 
    NSLog(@"attempting touch."); 
    if([self didTouch: touch]) { 
    return [self tsTouchBegan:touch withEvent: event]; 
    } 
    return NO; 
    } 

,但仍然不會編譯爲仍返回錯誤「財產‘被TouchEnabled’對象類型‘CCTurtle *’

沒有找到

我真的不知道我能做些什麼來讓它現在運行...並且真的需要讓它工作(我想我可以製作隱形按鈕,但是能夠正確找到ccturtle並更好明白什麼即時做錯了...希望有人可以幫助

+0

我對Cocos2D不是很熟悉,但如果說「層」是指「CALayer」的子類,那麼這是錯誤的方法。 CALayer不響應觸摸。有很多關於這個主題的答案,如http://stackoverflow.com/questions/4170288/trigger-an-action-when-a-calayer-is-touched –

+0

通常在cocos2d層是CCLayer的一個實例,它響應觸摸。 – Morion

+0

Helloworld CCLayer正在響應FTCCharacter:CCSprite沒有響應 – StackBuddy

回答

1

首先,我看不到任何呼叫containsTouch:方法的任何地方。這裏有幾個建議:

使用boundingBox而不是textureRect來獲得您的節點(在這種情況下,你的烏龜)的地方rect。或者,只需將containsTouch:方法替換爲您的龜類來封裝。例如,如果你想讓你的龜的接觸面積變大/變小,這會很有幫助。你只需要改變你的龜類的一個小方法。

在你的ccTouchesBegan:withEvent:方法只是檢查每一隻烏龜,如果它被觸摸命中。然後,例如,您可以創建字典,將touch作爲鍵和相應的烏龜數組作爲值。然後,您只需在ccTouchesMoved:withEvent:方法中更新所有海龜位置以便移動觸摸,然後從ccTouchesEnded:withEvent:和ccTouchCancelled:withEvent:方法的字典中刪除此海龜陣列。

+0

謝謝莫里安!生病有一個嘗試,並回到你:) – StackBuddy

+0

moriOn,但不客氣=) – Morion

+0

儘量不要在評論中張貼代碼。它很難閱讀。我仍然建議將'containsTouch:'方法移動到烏龜類,並在你的'ccTouchBegan:withEvent:'方法 – Morion

0

如果您希望CCTurtle對象接受目標觸摸,可以通過指定它符合CCTargetedTouchDelegate協議來實現。在你CCTurtle @interface,你這樣聲明:

@interface CCTurtle : CCNode <CCTargetedTouchDelegate> 

然後在執行,你告訴它接受觸摸和實施ccTouch方法:

@implementation CCTurtle 

-(id) init { 
. 
. 
    [[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self priority:1 swallowsTouches:YES]; 
    return self; 
} 

-(void) onExit { 
    [[CCDirector sharedDirector].touchDispatcher removeDelegate:self]; 
} 

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 
    CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]]; 
    if (CGRectContainsPoint([self boundingBox], location] { 
     // some code here 
    } 
} 


-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event { // code here } 
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { // code here} 
-(void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event { // code here} 
+0

thankyou Sylvan爲你提供幫助,可悲的是CCTurtle:FTCCharacter 返回錯誤屬性觸摸調度器沒有找到對象CCDirector – StackBuddy

+0

@Natty你的Cocos2D是什麼版本使用? – Sylvan

+0

嗨,我正在使用Cocos2D cocos2d v1.0.1(啓用arch),並正在測試ipad 4.3模擬器 – StackBuddy

0

由於原型的問題得到解決, 雖然CCTurtle用型精靈的層上,它被嵌套精靈 這意味着cocos2d的有創建正確的邊框爲我 問題「包含觸摸法」

所以有點魔力,他將他的「get full bounding box」方法結合到包含觸摸方法來說明精靈的子代,並且代碼現在依靠碰撞檢測來處理觸摸。 目前我很高興爲他換來一些漂亮的圖標

但是想對所有幫助的人表示感謝,並希望這種方法適用於任何有相同問題的人!