touch dispatcher for dummies?

Forums Programming cocos2d support (graphics engine) touch dispatcher for dummies?

This topic contains 44 replies, has 8 voices, and was last updated by  PixelPrincess 1 year, 10 months ago.

Viewing 25 posts - 1 through 25 (of 45 total)
Author Posts
Author Posts
August 19, 2009 at 2:09 pm #216884

alex
Participant
@alex

Hi

Does anyone have anyone have an example on how to use the touch dispatcher on a sprite. I’ve looked at TouchesTest and MenuTest and I still don’t understand it.

Please help.

-alex

August 19, 2009 at 5:04 pm #257614

Clain
Participant
@clain

You’ve looked at TouchesTest… you’ve probably missed the Paddle.m file. This is where the sprite registers to the TouchDispatcher and receives the touches events in the ccTouch____ methods.

August 20, 2009 at 7:42 pm #257615

alex
Participant
@alex

yeah, I’ve had a look and its just too much information for me – I am a complete newbie. I’m wondering if someone can post an example where there would be a sprite and action directe to that sprite and its location ???

August 21, 2009 at 12:35 am #257616

shivaz
@shivaz

Hello Alex

It is not too hard to understand, just break it down to the basics.

in the .h file:

@interface Paddle : TextureNode <TargetedTouchDelegate> {

Register for TargetedTouches ^

You also need the PaddleState typedef and property (you can rename them – just be consistent).

Then in the .m file:

In the init method – set the state to ungrabbed.

In your onEnter and onExit you just need to register for targeted touches.

You need some way of detecting if your touch is in your object. The demo has a containsTouchLocation method – that uses the rect method of TextureNode – if you are not using a TextureNode, then you need to roll your own Rect method.

Finally, you can grab the ccTouch… functions – and it should all work fine.

Now, if you want another demo – go to this thread and grab the sample there (last link) – and I think I put some stuff in there. Hopefully.

http://www.cocos2d-iphone.org/forum/topic/364

Justin

August 23, 2009 at 8:56 am #257617

alex
Participant
@alex

this is just so complicated to understand. I’ve been sitting and trying various things for a few days and I can not get it to work. I can’t get it to work because I do not understand.

All I want to do is to run the following action :

id scaleto = [ScaleTo actionWithDuration: 0.5 scale:0.5f];

id actionBy = [ScaleBy actionWithDuration: 0.5 scale: 2];

id actionByBack = [actionBy reverse];

[self runAction: scaleto];

[self runAction: [Sequence actions:actionBy, actionByBack, nil]];

return kEventHandled;

I can get it to work on a layer and I figured I could just add it to the demo in paddle.m and it would work:

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event

{

if (state != kPaddleStateUngrabbed) return NO;

if ( ![self containsTouchLocation:touch] ) return NO;

state = kPaddleStateGrabbed;

return NO;

id scaleto = [ScaleTo actionWithDuration: 0.5 scale:0.5f];

id actionBy = [ScaleBy actionWithDuration: 0.5 scale: 2];

id actionByBack = [actionBy reverse];

[self runAction: scaleto];

[self runAction: [Sequence actions:actionBy, actionByBack, nil]];

return kEventHandled;

}

but it doesn’t – I get 3 warnings and 1 error:

-warnings being treated as error

-warnings: implicit conversion shorten 64-bit value into a 32-bit value

-warnings: implicit conversion shorten 64-bit value into a 32-bit value

Command/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1

Why can’t I get it to work this way. I just need to see how I could scale the paddles when a touch begins and then maybe I could make another tiny step forward…

SOS #-(

August 23, 2009 at 8:57 am #257618

alex
Participant
@alex

You need some way of detecting if your touch is in your object. The demo has a containsTouchLocation method - that uses the rect method of TextureNode - if you are not using a TextureNode, then you need to roll your own Rect method.

could someone also explain the differences in these 2 methods?

August 23, 2009 at 12:02 pm #257619

alex
Participant
@alex

c’mon anyone – I feel like the village idiot :-)

August 23, 2009 at 12:07 pm #257620

cjl
Participant
@cjl

@Alex:

I can post a simple example of the touchdispatcher later today, if I get a chance. And no, you’re not idiot, this stuff is tricky.

Check this thread later, I’ll post an update when I have the code ready.

August 23, 2009 at 12:26 pm #257621

alex
Participant
@alex

ok thanks a lot :-)

August 24, 2009 at 6:43 am #257622

alex
Participant
@alex

anyone? SOS!

August 24, 2009 at 11:13 am #257623

pabloruiz55
Participant
@pabloruiz55

Also,

but it doesn’t – I get 3 warnings and 1 error:

-warnings being treated as error

-warnings: implicit conversion shorten 64-bit value into a 32-bit value

-warnings: implicit conversion shorten 64-bit value into a 32-bit value

Command/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1

Check the project configuration and uncheck “treat warnings as errors” since the compiling error you are getting is because of that. Having any warning will yield an error.

August 24, 2009 at 12:44 pm #257624

alex
Participant
@alex

@pabloruiz55 THANK YOU – the actions work but what about the warnings – is this a major no-no?

August 24, 2009 at 12:56 pm #257625

pabloruiz55
Participant
@pabloruiz55

Could you check where it is throwing these warnings? “implicit conversion shorten 64-bit value into a 32-bit value”

August 24, 2009 at 1:05 pm #257626

alex
Participant
@alex

here in the actions that I’ve added:

id scaleto = [ScaleTo actionWithDuration: 0.5 scale:0.5f];

id actionBy = [ScaleBy actionWithDuration: 0.5 scale: 2];

August 24, 2009 at 1:22 pm #257627

Steffen Altwiese
Moderator
@steve-oldmeadow
id scaleto = [ScaleTo actionWithDuration: 0.5f scale:0.5f];
id actionBy = [ScaleBy actionWithDuration: 0.5f scale: 2.0f];

August 24, 2009 at 2:29 pm #257628

alex
Participant
@alex

@Steve Oldmeadow – u know I tried that before when I had all the warnings and it made no difference.

Will have to work on consistency though ;-)

August 24, 2009 at 2:34 pm #257629

Steffen Altwiese
Moderator
@steve-oldmeadow

That is strange, did it fix the warnings now?

August 24, 2009 at 5:44 pm #257630

alex
Participant
@alex

Now it did, I must have missed something before ;-) Thank you.

August 25, 2009 at 10:08 am #257631

alex
Participant
@alex

how does one limit the touch so the action is completed before it can be restarted?

August 25, 2009 at 11:33 am #257632

Steffen Altwiese
Moderator
@steve-oldmeadow

@Alex – you already have a model to follow in the code you posted. Look at how they check if the paddle has already been grabbed. You need to do something similar but check if your action is running. Actions have an isDone method to check if they are complete or you could use a Sequence with a CallFunc action at the end that calls a method that resets a flag that will allow the action to run again.

August 25, 2009 at 11:35 am #257633

cjl
Participant
@cjl

@Alex:

Sorry, I was ‘sidelined’ by my wife, for some reason she doesn’t think iPhone programming is a top priority.

Do you still need a simple example? It looks like from your posts you have it working now.

August 25, 2009 at 11:41 am #257634

alex
Participant
@alex

yeah, i would actually. one that implements CGRect and touch location if possible?

August 25, 2009 at 11:43 am #257635

alex
Participant
@alex

how does one limit the touch so the action is completed before it can be restarted?

August 25, 2009 at 12:03 pm #257636

cjl
Participant
@cjl

@Alex

Read Steve’s response above. When the action triggered by the touch starts running you can set a flag (some people would use a variable of type BOOL):

running = YES;

Then, you can change your action so that it finishes with a callback to a function that resets the flag:

running = NO;

Then, in your code that triggers the action you would need something like:

if (!running) {
//run actions here
}

Anyway, that is one way to do it.

August 25, 2009 at 12:12 pm #257637

alex
Participant
@alex

thanks, what about a dummy example including touch location?

Viewing 25 posts - 1 through 25 (of 45 total)

You must be logged in to reply to this topic.