晚上好,我将以历史悠久的传统开始这个问题,说我已经对 SO 以及更广阔的世界进行了很好的旧搜索,但我并没有完全理解这个......我已经实现了一个很好用的sneakyJoystick(它非常愉快地移动了我的精灵)但是我现在在考虑它的定位时给自己做了一个恶作剧。我想做的只是将它的位置更改为触摸位置并让它移动我的精灵,但这似乎超出了我的知识库。我可能是个白痴,但我无法解决。
触摸事件已经在sneakyjoystick 类中排序(在github https://github.com/0xPr0xy/sneaky-joystick-cocos2d上可用)。目前,如果我在名为 controlsLayer 的类中的 init 方法中创建操纵杆,那么一切正常;操纵杆出现,它允许我移动精灵
-(id) init
{
if( (self=[super init]) ) {
myJoystickBase = [[[SneakyJoystickSkinnedBase alloc] init] autorelease];
myJoystickBase.backgroundSprite = [CCSprite spriteWithFile:@"dpad.png"];
myJoystickBase.thumbSprite = [CCSprite spriteWithFile:@"joystick.png"];
myJoystickBase.joystick = [[SneakyJoystick alloc] initWithRect:CGRectMake(0, 0, 128, 128)];
myJoystickBase.position = ccp(64, 64);
myJoystickBase.backgroundSprite.opacity = 100;
myJoystickBase.thumbSprite.opacity = 100;
[self addChild:myJoystickBase];
myJoystick = [myJoystickBase.joystick retain];
[self scheduleUpdate];
}
return self;
}
所以一开始我想看看我如何简单地让它显示和隐藏自己并设置它的位置。为此,我创建了一个 ccTouchesbegan 方法,其中包含与我之前的 init 方法几乎相同的代码。这在一定程度上可以正常工作(操纵杆出现在我触摸的任何地方的中心)但现在的问题是我无法与之交互。操纵杆出现在我想要的位置,但它无法识别我的动作(操纵杆上的操纵杆没有移动,这反过来意味着我的精灵也没有被告知移动)
if( (self=[super init]) ) {
self.isTouchEnabled = YES;
[self scheduleUpdate];
}
return self;
}
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
location = [self convertToNodeSpace:location];
myJoystickBase = [[[SneakyJoystickSkinnedBase alloc] init] autorelease];
myJoystickBase.backgroundSprite = [CCSprite spriteWithFile:@"dpad.png"];
myJoystickBase.thumbSprite = [CCSprite spriteWithFile:@"joystick.png"];
myJoystickBase.joystick = [[SneakyJoystick alloc] initWithRect:CGRectMake(0, 0, 128, 128)];
myJoystickBase.position = location;
myJoystickBase.backgroundSprite.opacity = 100;
myJoystickBase.thumbSprite.opacity = 100;
[self addChild:myJoystickBase];
myJoystick = [myJoystickBase.joystick retain];
}
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self removeChild:myJoystickBase cleanup:YES];
}
所以对于我的外行来说,这就是说当我触摸 controlsLayer 类时,它会很高兴地按照我的要求去做,但它不会将这种触摸传递给其他类。
简而言之,一个触摸事件可以同时传递给多个类吗?鬼鬼祟祟的操纵杆类使用 CCTargetedTouchDelegate,当它是唯一需要触摸的东西时,它工作得很好。然而,现在我已经添加了一个 -(void)ccTouchesBegan: 在另一个类中它并不高兴。
谁能告诉我问题是我处理触摸的方式还是我在触摸方法中分配操纵杆的方式有问题?我应该在 init 方法中分配操纵杆并在 touchesBegan 方法中做其他事情吗?目前,反复试验并没有让我有任何用处。感觉就像我的头撞在砖墙上一样。如有必要,我很乐意发布完整的课程文件。这有意义吗?