質問

Evening all, I'll start this question in the time honoured tradition by saying that I've had a good old search on SO and also in the wider world but I'm not quite getting my head around this... I've implemented a sneakyJoystick which works wonderfully (It moves my sprite around quite happily) however I've now done myself a mischief in thinking about it's positioning. What I'd like to do is simply change it's position to a touch location and have it move my sprite around but this seems to be out of my knowledge pool. I might be being an idiot but I cannot work it out.

The touch events are already sorted in the sneakyjoystick classes (available on github https://github.com/0xPr0xy/sneaky-joystick-cocos2d). At the moment if I create the joystick during the init method in a class called controlsLayer then everything works fine; Joystick appears and it allows me to move to sprite around

-(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;
}

So to begin with I thought about looking at how I could simply get it to show and hide itself and set it's location. To do this I created a ccTouchesbegan method which contains pretty much the same code as my init method did before. This works fine up to a point (the joystick appears centred wherever I touch) but the issue now is that I cannot interact with it. The joystick appears where i want but it will not recognise my movements (the stick on the joystick does not move which in turn means that my sprite is not being told to move either)

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];
}

So to my uninitiated brain this is saying that when I touch the controlsLayer class it happily does what I've asked it to but it will not then pass this touch to other classes.

In a nutshell can a touch event be passed to multiple classes at the same time? The sneaky joystick class uses CCTargetedTouchDelegate which worked fine when it was the only thing looking for a touch. However now that I've added a -(void)ccTouchesBegan: in another class it's not happy.

Can anyone tell me if the problem is with the way I'm handling touches or is it possibly an issue with the way that I allocate the joystick in the touch method? Should I be allocating the joystick in the init method and be doing something else in the touchesBegan method? Trial and error isn't getting me anywhere useful at the moment. Feel like I'm banging my head against a brick wall. I'm happy to post up the full class files if necessary. Does this make sense?

役に立ちましたか?

解決

The problem is that you create the joystick during touch began, and remove it on touch ended. This does not allow the joystick to receive a touch began event, since the event is already being processed at the time the joystick is being created.

You're also leaking the joystick due to the unnecessary retain (use ARC, please!).

Try creating the joystick in init, disable & hide it until a touch began event is received. This is also faster than recreating the joystick on every touch.

Also, should you have multiple touches enabled, keep in mind that you can receive up to 5 touch began events (5 fingers) without any touch ended in between. That would create 5 joysticks, but only remove one! Every time!

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top