self.isTouchEnabled = YES;
in init method ofcourse.
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
}
-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
}
in Above code ccTouchesMoved works fine but ccTouchMoved doesn't call..
any help ?!
Cocos2d supports two different ways of handling touch events. These are defined by two different types of delegates (both defined in CCTouchDelegateProtocol.h).
Standard Touch Delegate:
#optional
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
These are the same sorts of events you'd get in a standard CocoaTouch app. You'll get all events, and all touches; it will be up to you to sort out which touches you care about in a multi-touch environment.To get these events in a CCLayer subclass, you simply set isTouchEnabled = YES, like so:
self.isTouchEnabled = YES;
Targeted Touch Delegate
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event;
#optional
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event;
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event;
- (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event;
Note two important differences between the targeted and the standard touch delegate:
These methods provide only a single touch, rather than a set of them – and for that reason, the method names start with “ccTouch” rather than “ccTouches”.
The ccTouchBegan method is required and returns a boolean value.
So ccTouchBegan will be invoked separately for each of the available touches, and you return YES to indicate a touch you care about. Only touches claimed by ccTouchBegan will be subsequently passed on to the Moved, Ended, and Cancelled events (all of which are optional).
To receive these events, you must register as a targeted touch delegate with the global dispatcher. In a CCLayer subclass, override registerWithTouchDispatcher as follows:
-(void) registerWithTouchDispatcher {
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
(which will mean importing “CCTouchDispatcher.h” at the top of your file).
Which to use?
Apart from the more complex registration, the targeted touch delegate is generally easier to use, since you don't have to split the NSSet up yourself, and you don't have to keep checking whether the event is the one you want in the Moved/Ended/Cancelled events. But if you want to deal with multiple touches in one method (for example, because you combine them into a zoom or rotate input), you'll probably want to use the standard touch delegate instead.
Note that you can only use one or the other.
There's 2 behaviours: one for standard touches, one for multiple touches. You don't have to addTargetedDelegate:::, you can simply set the touchMode property to the value you like. The CCLayer will take care of the registration for you.
- (void)onEnter
{
[self setTouchMode: kCCTouchesAllAtOnce]; //resp kCCTouchesOneByOne
}
Behind the scenes, changing the touchMode will disable then re-enable the touches, and enabling the touches (enableTouch) will register the proper delegate for you, by calling either addStandardDelegate or addTargetedDelegate.
Related
I'm trying to start developing cocos2d games. So, I'm new in cocos2d, but I developed few applications on iPhone. I installed cocos templates (v2.0) and created new project with box2d phisics. Here I can see a demo project with blocks and some menus. When I tap screen, new block appears, and falls to the botton of screen. Than must be implemented here:
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//Add a new body/atlas sprite at the touched location
for( UITouch *touch in touches ) {
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL: location];
[self addNewSpriteAtPosition: location];
}
}
so, sprite appears when touches ended. But how to do something when touches begun or moved? I cant this finds methods for cocos. I saw some tutorials, there is method like this:
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
return YES;
}
but it never called... What am I doung wrong?
Implement
(BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
And ensure you have
self.isTouchEnabled = YES;.
in your init method for that layer.
Implement:
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
// whatever is needed here
return YES;
}
in your cocos2d class (the same implementing ccTouchesEnded), then it will be called.
I've a view with 10 character labels (big letters). Each character (label) has a different tag. The functionality that I'm developing is "Trace".
When user moves his finger over the view, i want to detect which character is touched.
I think I've to implement,
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
But I'm not knowing how to identify touch on the label and identify the character.
Can some one help me?
If you want to know the view that the touch began on (which view the user put their finger down on), you can read any of the touch items returned in the NSSet like so:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSInteger viewTag = [[[touches anyObject] view] tag];
//Check your view using the tags you've set...
}
However, even as the finger moves across the screen, this view property will ONLY return the view initially touched and not the view currently under the finger. To do this, you will need to track the coordinates of the current touch and determine which view it falls into, perhaps like this:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
//If all your views are collected as an array
for(UIView *view in views) {
if (CGRectContainsPoint([view frame], point))
{
//Bingo
}
}
}
The key to success on this is to make sure that you are getting your touches in coordinates reference to the proper views, so make sure to call locationInView: and CGRectContainsPoint() appropriately with values that match your application's view hierarchy and where your placing this code (i.e. the View, ViewController, etc.)
To detect simple touches you can use UIGestureRecognizer. Read the documentation for more on those. For more complex operations you do need to implement:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
To identify which item was touched you can give your item's tags values:
myView.tag = 4;
Then just check the tag value of the view reporting the touch and you know which it is.
I have a view that the users are allowed to finger paint. The code is working perfectly if the area is touched with one finger. For example: I touch it with one finger and move the finger. Then, a line is drawn as I move the first finger. If I touch with a second finger the same view, the line that was being drawn by the first finger stops.
I would like to ignore any touch beyond the first, i.e., to track the first touch but ignore all others to the same view.
I am using touchesBegan/moved/ended.
I have used this to detect the touches
UITouch *touch = [[event allTouches] anyObject];
lastPoint = [touch locationInView:myView];
I have also tried this
lastPoint = [[touches anyObject] locationInView:myView];
but nothing changed.
How do I do that - track the first touch and ignore any subsequent touch to a view?
thanks.
NOTE: the view is NOT adjusted to detect multiple touches.
A given touch will maintain the same memory address as long as it is in contact with the screen. This means you can save the address as an instance variable and ignore any events from other objects. However, do not retain the touch. If you do, a different address will be used and your code won't work.
Example:
Add currentTouch to your interface:
#interface MyView : UIView {
UITouch *currentTouch;
...
}
...
#end
Modify touchesBegan: to ignore the touch if one is already being tracked:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if(currentTouch) return;
currentTouch = [touches anyObject];
...
}
Modify touchesMoved: to use currentTouch instead of getting a touch from the set:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(!currentTouch) return;
CGPoint currentPoint = [currentTouch locationInView:myView];
...
}
Modify touchesEnded: and touchesCancelled: to clear currentTouch, but only if currentTouch has ended or been cancelled.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(currentTouch && currentTouch.phase == UITouchPhaseEnded) {
...
currentTouch = nil;
}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
if(currentTouch && currentTouch.phase == UITouchPhaseCancelled) {
...
currentTouch = nil;
}
}
yourView.multipleTouchEnabled = NO;
From the reference documents on UIView
multipleTouchEnabled
A Boolean value that indicates whether
the receiver handles multitouch
events.
#property(nonatomic, getter=isMultipleTouchEnabled) BOOL
multipleTouchEnabled Discussion
When set to YES, the receiver receives
all touches associated with a
multitouch sequence. When set to NO,
the receiver receives only the first
touch event in a multitouch sequence.
The default value of this property is
NO.
Other views in the same window can
still receive touch events when this
property is NO. If you want this view
to handle multitouch events
exclusively, set the values of both
this property and the exclusiveTouch
property to YES.
I will describe the problem, I have sprites that are all letters of the alphabet, and I wonder how I can do that when you touch the letters form a word and I can generate a string that word and through it to compare with a string that have a plist. I need any idea that might help me, thank you.
Yes, this is possible. One way to test for touches is by using the CCTouchDispatcher.
Overview
Determine which class will monitor for touches of your letter sprites.
Make the class a delegate of CCTargetedTouchDelegate.
Add code to the class to register with the CCTouchDispatcher.
Add code to the class to unregister with the CCTouchDispatcher.
Add the touch callback methods to your class. In the touch callback methods, you must add code for determining which sprite was touched.
Register and unregister from Dispatcher
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
CallBack Methods To Implement
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
Example Code For Testing If Touch In Sprite
- (BOOL) isTouch:(UITouch *)touch InSprite:(CCSprite *)sprite
{
CGPoint touchLocation = [touch locationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
CGPoint localLocation = [sprite convertToNodeSpace:touchLocation];
CGRect spriteRect = [sprite textureRect];
spriteRect.origin = CGPointZero;
if(CGRectContainsPoint(spriteRect, localLocation))
{
return YES;
}
return NO;
}
Maybe it will be more simple to use CCMenuItem (it can also be created from sprite) because it's already touchable. Everything you have to do is to specify a function will be called, when CCMenuItem is touched.
Take a look at the official programming guide:
http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:lesson_3._menus_and_scenes
I'm not sure what the iPhone version of this is called but in HTML it is an image map. Certain areas of the map route you to different pages. I'd like to use an image, mostly in a scrollview, that I can have certain areas perform different actions. For example, Chemical Touch, http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=288060442&mt=8, does this. Click on an element and it grows in size to reveal more details. What is this type of technique called and how is it created?
I'd like to be a little more interactive with the user, how can I do any of these:
drop map pins onto this image
display a UITextField depending if I have text in that area
overlay text depending on user actions
Any suggestions here are greatly appreciated.
What you basically need to do is override any or all of:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
in your UIView subclass (or actually any subclass of UIResponder). Then you can handle the event however you wish, such as doing any of the things you mentioned in your question.
For example, here is Apple's sample code for a simple event handler:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
NSUInteger numTaps = [touch tapCount];
if (numTaps < 2) {
[self.nextResponder touchesBegan:touches withEvent:event];
} else {
[self handleDoubleTap:touch];
}
}
The Event Handling section in the iPhone Application Programming Guide should give you all the info you need to get started.