Problem with cocos2D for iPhone and touch detection

老子叫甜甜 提交于 2019-12-03 05:11:33

问题


I just don't get it. I use cocos2d for development of a small game on the iPhone/Pod. The framework is just great, but I fail at touch detection. I read that you just need to overwrite the proper functions (e.g. "touchesBegan" ) in the implementation of a class wich subclasses CocosNode. But it doesn't work. What could I do wrong?

the function:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{NSLog(@"tickle, hihi!");}

did I get it totally wrong?

Thank you!


回答1:


Layer is the only cocos2d class which gets touches.

The trick is that ALL instances of Layer get passed the touch events, one after the other, so your code has to handle this.

I did it like this:

-(BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
CGPoint cLoc = [[Director sharedDirector] convertCoordinate: location];

float labelX = self.position.x - HALF_WIDTH;
float labelY = self.position.y - HALF_WIDTH;
float labelXWidth = labelX + WIDTH;
float labelYHeight = labelY + WIDTH;

if( labelX < cLoc.x &&
    labelY < cLoc.y &&
    labelXWidth > cLoc.x &&
    labelYHeight > cLoc.y){
        NSLog(@"WE ARE TOUCHED AND I AM A %@", self.labelString);
        return kEventHandled;
    } else {
        return kEventIgnored;
    }

}

Note that the cocos2d library has a "ccTouchesEnded" implementation, rather than the Apple standard. It allows you to return a BOOL indicating whether or not you handled the event.

Good luck!




回答2:


Have you added this to your layers init method?

    // isTouchEnabled is an property of Layer (the super class).
    // When it is YES, then the touches will be enabled
    self.isTouchEnabled = YES;

    // isAccelerometerEnabled is property of Layer (the super class).
    // When it is YES, then the accelerometer will be enabled
    self.isAccelerometerEnabled = YES;



回答3:


In order to detect touches, you need to subclass from UIResponder (which UIView does as well) . I am not familiar with cocos2D, but a quick look at the documentation reveals that CocosNode does not derive from UIResponder.

Upon further investigation, it looks like Cocos folks created a Layer class that derives from CocosNode. And that class implements the touch event handlers. But those are prefixed by cc.

See http://code.google.com/p/cocos2d-iphone/source/browse/trunk/cocos2d/Layer.h

Also see menu.m code and the below blog post article for more info on this:

http://blog.sapusmedia.com/2008/12/cocos2d-propagating-touch-events.html




回答4:


maw, the CGPoint struct members x,y are floats. use @"%f" to format floats for printf/NSLog.




回答5:


If you use the 0.9 beta of cocos2D it has a really simple touch detection for CocosNodes. The real beauty of this new detection is that it handles multiple touch tracking really well.

An example of this can be found here

http://code.google.com/p/cocos2d-iphone/source/browse/#svn/trunk/tests/TouchesTest




回答6:


- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

        //Add a new body/atlas sprite at the touched location
        CGPoint tapPosition;
        for( UITouch *touch in touches ) {
            CGPoint location = [touch locationInView: [touch view]];

            tapPosition = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:location]];     // get the tapped position





    }
}

think this can help you....




回答7:


-Make your scene conforms to protocol CCTargetedTouchDelegate -Add This line to init of your scene:

[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];

-Implement these functions:

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
 {
   return  YES;
 }
 -(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
  {
    //here touch is ended
  }


来源:https://stackoverflow.com/questions/377785/problem-with-cocos2d-for-iphone-and-touch-detection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!