Welcome to my Programming Blog...

I am currently working on a couple of projects: An original game called "Implosion" which I am porting from Flash to the iPad, and a remake of Q*bert in Python (pygame), as part of the class I am teaching. Please feel free to use the "Labels" (at right) to follow a specific project or theme. If you are one of my Python students, I recommend that you check out the Python thread.

Thursday, January 13, 2011

Touchy, Touchy!

Here are notes from Matt on how to detect touches on the screen.

  • In HelloWorldScene.h type this in the section where your functions are placed:
    • -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event;
    • -(void) registerWithTouchDispatcher;
    • -(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event;
  • In HelloWorldScene.m type this in the “init” function
    • self.isTouchEnabled = YES;
  • Next go to some place in your HelloWorldScene.m file where there is free space between the init and dealloc functions and create the three functions listed above
  • We are going to define them one by one:
    • For the ccTouchBegan, use this code:
      • return YES;
    • For the registerWithTouchDispatcher, use this code:
      • [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate: self
          • priority: 0
          • swallowsTouches:YES];
    • For the ccTouchEnded, it is going to be up to you what you put in there.  However, what is supposed to be placed into this function is defining where you want to move your player or whatever you want your touch to do.  If you want to move your player to where you touched you will want to compare (using the ccpSub on the location of your touch and the location of your player relative to the screen).  Then use this new CGPoint you generated to make your player to move to this new location. If you want to remove something on your touch, you will want to go through a list of enemies you want to remove and then compare each one to a certain distance from your touch location to this temporary enemy. (this code can be found in the tribble project’s “HelloWorldScene.m” in it’s ccTouchEnded function).
    • Note: one thing you will want to do is use the line:
      CGPoint touchLocation = [self convertTouchToNodeSpace: touch];
      to get the location of where you touched on the screen.
This is all on single-touch events. Of course the iPhone is famous for multitouch sensitivity, and you might wish to play around with that. If so, great! I'll be posting on multitouch sometime this weekend, but first, you'll want to master the single touch material. - HH

No comments:

Post a Comment