下面我有一個圖層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並更好明白什麼即時做錯了...希望有人可以幫助
我對Cocos2D不是很熟悉,但如果說「層」是指「CALayer」的子類,那麼這是錯誤的方法。 CALayer不響應觸摸。有很多關於這個主題的答案,如http://stackoverflow.com/questions/4170288/trigger-an-action-when-a-calayer-is-touched –
通常在cocos2d層是CCLayer的一個實例,它響應觸摸。 – Morion
Helloworld CCLayer正在響應FTCCharacter:CCSprite沒有響應 – StackBuddy