我在HelloWorldLayer
下面有一个图层,触摸可以在任何地方工作,但我希望它仅在触摸图层中的精灵时才起作用 -turtle
下方。
如果我尝试添加self.isTouchEnabled = YES;
到CCTurtle
它说的图层
isTouchEnabled
在对象类型 CCTurtle 上找不到属性
我的输出如下
2013-01-08 20:30:14.767 FlashToCocosARC[6746:d503] cocos2d:解除分配
2013-01-08 20:30:15.245 FlashToCocosARC[6746:d503] 播放行走动画2
这是我的 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
//你好世界.h
#import "cocos2d.h"
#import "CCTurtle.h"
@interface HelloWorldLayer : CCLayer
{
CCTurtle *turtle;
}
+(CCScene *) scene;
@end
//CC乌龟
#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;
}
但仍然不会编译,因为仍然返回错误“在对象类型'CCTurtle *'上找不到属性'is TouchEnabled'
我真的不确定我能做些什么来让它现在运行......并且真的需要让它工作(我想我可以制作隐形按钮,但能够正确找到 ccturtle 并了解什么会更好我做错了...希望有人可以提供帮助