background preloader

Touches

Facebook Twitter

iPhone War Room: Cocos2d - How to pass touch event to sprite in a layer (CCTouchDispatcher in CCLayer and CCSprite) I have created a layer which is a subclass of CCLayer. I also have a sprite inside the layer using the subclass of CCSprite. Now the question is that when the layer receives the user's touch event, I want it to do some process and also pass the event to the sprite if the touch location is inside the sprite. That way, in the container level(CCLayer), I can do certain things(ex. show another sprite, start a transition to another scene, ...etc) while the sprite can do some additional works(ex. show an animation, hide itself, ...etc) if it's rectangle is touched by the user.

I have tried several ways and search solutions for a while. Finally I figure it out the right fix for this issue. Here is the code and diagram showing the correct solution. . - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event. Tutorials don't mention CCTouchDispatcher::removeDelegate. Levi Participant @levi After a long, long session with instruments, I finally discovered why my scenes were not being deallocated OR leaked. The CCTouchDispatcher was retaining them even after they were removed and released everywhere else.

Which seems like good practice, so I followed suit. Anyway, that’s my story, but I wanted to mention that the two documents I found discussing touch events have no mention of needing to call removeDelegate. Perhaps a quick edit will save other developers the 1 hour headache I endured. TBBle @tbble Lesson 2 should indeed mention it, as it’s touch-enabling a CCScene. However, the touchdelegates instructions don’t tell you _where_ to put your addDelegate call, so you will potentially either not be reregistered if your layer exits and enters, _or_ trigger an assertion when you try to register as both kinds of delegate.

GregX999 @gregx999. CCTouchDispatcher has a bug when add and remove delegate. I found CCTouchDispatcher has a bug when add or remove delegate, let’s take a look at some test code: @interface my : CCNode<CCTargetedTouchDelegate> @end @implementation my -(void) onEnter [super onEnter]; [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO]; -(void) onExit [super onExit]; [[CCTouchDispatcher sharedDispatcher] removeDelegate:self]; -(void) dealloc [super dealloc]; // Set a breakpoint here The interface “my”‘s job is simple: just add delegate in onEnter, and remove delegate in onExit, no other things. Now, I have a layer which can recieve touch event: HelloWorldLayer, I implement the ccTouchBegan method in the layer: -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event my *test = [[my alloc] init]; // 1 [self addChild:test]; // 2 [test release]; // 3 [self removeChild:test cleanup:YES]; // 4 return YES; When go to the step 2, self add “test” as its child, and then go to “test”‘s onEnter method, it will register touch delegate. else.

Recognize Touch and Hold. I've been struggling with this one all last night. I'm trying to write code that will recognize when the user has touched the screen and is holding their finger in that touch position for a duration of time (say 2 or 3 seconds) and then do something as a result. It seems to me that you can't really do this in touchesBegan because the code only runs once. There would be no way to get it to re-evaluate the touch's timestamp and compare it with the initial timestamp of the touch... because the timestamp doesn't change in the touchesBegan phase. You might be able to use a timer.. but that's not really waiting for the user to hold their touch in place for a duration of time. That's just waiting a duration of time once the user touches the screen (not the same thing, as the user could have moved the touch or lifted their finger).

You also can't do this in touchesMoved because the user isn't moving their finger. It's stationary. I think the answer lies in the phase UITouchPhaseStationary. ... Tips:touchdelegates – cocos2d for iPhone. Detect finger stop moving without lifting finger. Cocos Participant @cocos Moving finger generates events via: - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; or - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event; see: To check that finger is not moving but still is not lifted use a timer and take under account events from above functions. ccnewbies @ccnewbies Do you mean put a counter in the update/tick to see whether the ccTouchesMoved increases? MFerron @mferron Not sure what a counter would get you… you will want to be checking the current touch location with the last touch location.

Though like cocos said, you may want to incorporate a timer to widen the ‘stopped’ time to get better results. @ccnewbies This is one of many possible implementations of the detection which you need. ‘fingerStopped’ function is called when finger stops moving for more than ONE_SEC Solution uses timer and the ‘fingerDoesNotMove’ counter. #define ONE_SEC 30 1. Can we calculate touch pressure. Venkat @venkat hi , how can we detect, that with how much pressure the screen is pressed or tapped. i.e when we click or tap the touche screen, at that time can we find out, with how much pressure or force user has clicked or taped the touch screen.

Yves @yves No you cant :P, but you can find out how much the device moved and make decisions based on that. hi Yves, thank’s for your reply but in my view we cant find the pressure based on device moved. because one persons hold devices stronger or with more energy ,some other person holds in easy or with very less energy and some other with medium energy. even though the users used the same pressure to tap the touchscreen but the devices move will change depending upon the strength that the user hold if person hold strongly then device wont move much if person hold easily then device will move more distance so here device movement is completely depend upon energy used to hold the device in my view if my view is wrong please let me know. gramulho @gramulho. Game development - Cocos2d handling touch with multiple layers. Touch Detection in Cocos2d iPhone. Ivan Moen left a comment asking for an explanation about detecting which sprites have been touched in Cocos2d for the iPhone, and it was something I was intended to write about (eventually), so it seemed like a ripe time to address it.

Before we start, I'd like to mention that Luke Hatcher created much of the code that these snippets are inspired by. Broadly, there are three different approaches to adding touch detection to pixels in Cocos2d iPhone. Which one you should choose depends on the needs of your application. While considering this topic, it's important to keep in mind that you're not just detecting touches, you're integrating a user interface management system to your application. The three approaches are: Well, there you have it, three approaches to handling touch detection for Cocos2d iPhone, presented in a confusing and at most halfway organized article. Let me know if you have any questions, but I hope this is enough to get you moving in the right direction. Cocos2d Game Tutorial - Multitouch Asteroids (Part 1) | Ganbaru Games. So I’ve posted some general-purpose tutorials about some of the lower level components of cocos2d, and I figure it’s as good of a time as any to have a tutorial that makes something “real.”

So, for this next series, we’re going to concentrate on making a full-fledged game – an Asteroids clone. The game logic that controls Asteroids is pretty darn simple, which means that most of the discussion will be about cocos2d and Objective-C. We’ll also discuss adapting a game that uses a traditional control scheme (joystick & buttons) to use a touchscreen instead. Let’s get started! Create a new Xcode project that uses a cocos2d template. 01. 03. 06. 09. 12.- (id)initWithTexture:(CCTexture2D*)texture rect:(CGRect)rect; 15.- (void)update:(ccTime)dt; 17.

You can see that this new class has access to all the features of a regular sprite, but we’re also giving it a new property, “velocity.” 01. 03. 06. 09.- (id)initWithTexture:(CCTexture2D*)texture rect:(CGRect)rect 15. 17. return self; 24. 27. 31. 33. 36. 38. Detecting touch events in cocos2d-iphone | Ganbaru Games. In the previous tutorial I wrote about scenes and layers, the basic structural classes of a cocos2d app.

In this next installment, I’m going to talk about putting some actual content into your scenes and responding to user input. If we’re interested in getting user input, first we should create something that responds to that input. In most cases, this will be a sprite object. A sprite is usually a representation of an entity in your game system, such as the player, an enemy, or a power-up. When you create a new sprite, you give the sprite an image as a parameter. After you add the sprite to a containing layer, that image then appears on the screen, and you can move it around by manipulating properties of the sprite object.

A simple example: 1.CCSprite *mySprite = [CCSprite spriteWithFile:@"Icon.png"]; 2. 3. This is the easiest way to get an image onto the screen. 1. 2. 3. Wait, this post was supposed to be about detecting user touches, right? 01. 03. 06. 09. 11. 02. 04.- (id)init 09. 12. 16. 17. 1. iOS Game Dev with Cocos2D (8) CCAnimation | E v e r G i z m O. In the last article, we talked about CCStandardTouchDelegate which is enabled by default in CCLayer. Today, we’ll implement CCAnimation so that we can demonstrate how the touch event callbacks can be used. The Xcode project can be downloaded from ccTouchesBegan – check if we touch the monster This is rather lengthy method. First of all, it is checking if the monster (or alien, whatever) was touched or not. Then, it sets up an animation action and attach that to the monster sprite object. . - ( void ) ccTouchesBegan : ( NSSet * ) touches withEvent : ( UIEvent * ) event { UITouch * touch = [ touches anyObject ] ; CGPoint touchPos = [ touch locationInView : [ touch view ] ] ; CGPoint position = [ [ CCDirector sharedDirector ] convertToGL : touchPos ] ; // check if touch was inside the monster sprite rectangle if ( !

When ccTouchesBegan is called, we know that a finger (or something) has started touching the screen. If ( !