<tfoot id='QqDpx'></tfoot>
  • <i id='QqDpx'><tr id='QqDpx'><dt id='QqDpx'><q id='QqDpx'><span id='QqDpx'><b id='QqDpx'><form id='QqDpx'><ins id='QqDpx'></ins><ul id='QqDpx'></ul><sub id='QqDpx'></sub></form><legend id='QqDpx'></legend><bdo id='QqDpx'><pre id='QqDpx'><center id='QqDpx'></center></pre></bdo></b><th id='QqDpx'></th></span></q></dt></tr></i><div id='QqDpx'><tfoot id='QqDpx'></tfoot><dl id='QqDpx'><fieldset id='QqDpx'></fieldset></dl></div>
      <bdo id='QqDpx'></bdo><ul id='QqDpx'></ul>
    <legend id='QqDpx'><style id='QqDpx'><dir id='QqDpx'><q id='QqDpx'></q></dir></style></legend>
    1. <small id='QqDpx'></small><noframes id='QqDpx'>

      1. 如何让对象对 Cocos2D 中的触摸做出反应?

        How to get objects to react to touches in Cocos2D?(如何让对象对 Cocos2D 中的触摸做出反应?)
        <legend id='qj84t'><style id='qj84t'><dir id='qj84t'><q id='qj84t'></q></dir></style></legend>
        <tfoot id='qj84t'></tfoot>

          <small id='qj84t'></small><noframes id='qj84t'>

              <tbody id='qj84t'></tbody>

                • <bdo id='qj84t'></bdo><ul id='qj84t'></ul>

                  <i id='qj84t'><tr id='qj84t'><dt id='qj84t'><q id='qj84t'><span id='qj84t'><b id='qj84t'><form id='qj84t'><ins id='qj84t'></ins><ul id='qj84t'></ul><sub id='qj84t'></sub></form><legend id='qj84t'></legend><bdo id='qj84t'><pre id='qj84t'><center id='qj84t'></center></pre></bdo></b><th id='qj84t'></th></span></q></dt></tr></i><div id='qj84t'><tfoot id='qj84t'></tfoot><dl id='qj84t'><fieldset id='qj84t'></fieldset></dl></div>
                • 本文介绍了如何让对象对 Cocos2D 中的触摸做出反应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Alright, so I'm starting to learn more about Coco2D, but I'm kinda frusterated. A lot of the tutorials I have found are for outdated versions of the code, so when I look through and see how they do certain things, I can't translate it into my own program, because a lot has changed. With that being said, I am working in the latest version of Coco2d, version 0.99.

                  What I want to do is create a sprite on the screen (Done) and then when I touch that sprite, I can have "something" happen. For now, let's just make an alert go off. Now, I got this code working with the help of a friend. Here is the header file:

                  // When you import this file, you import all the cocos2d classes
                  #import "cocos2d.h"
                  
                  // HelloWorld Layer
                  @interface HelloWorld : CCLayer
                  {
                   CGRect spRect;
                  }
                  
                  // returns a Scene that contains the HelloWorld as the only child
                  +(id) scene;
                  
                  @end
                  

                  And here is the implementation file:

                  //
                  // cocos2d Hello World example
                  // http://www.cocos2d-iphone.org
                  //
                  
                  // Import the interfaces
                  #import "HelloWorldScene.h"
                  #import "CustomCCNode.h"
                  
                  // HelloWorld implementation
                  @implementation HelloWorld
                  
                  +(id) scene
                  {
                   // 'scene' is an autorelease object.
                   CCScene *scene = [CCScene node];
                  
                   // 'layer' is an autorelease object.
                   HelloWorld *layer = [HelloWorld node];
                  
                   // add layer as a child to scene
                   [scene addChild: layer];
                  
                   // return the scene
                   return scene;
                  }
                  
                  // on "init" you need to initialize your instance
                  -(id) init
                  {
                   // always call "super" init
                   // Apple recommends to re-assign "self" with the "super" return value
                   if( (self=[super init] )) {
                  
                    // create and initialize a Label
                    CCLabel* label = [CCLabel labelWithString:@"Hello World" fontName:@"Times New Roman" fontSize:64];
                  
                    // ask director the the window size
                    CGSize size = [[CCDirector sharedDirector] winSize];
                  
                    // position the label on the center of the screen
                    label.position =  ccp( size.width /2 , size.height/2 );
                  
                    // add the label as a child to this Layer
                    [self addChild: label];
                  
                    CCSprite *sp = [CCSprite spriteWithFile:@"test2.png"];
                  
                    sp.position = ccp(300,200);
                    [self addChild:sp];
                    float w = [sp contentSize].width;
                    float h = [sp contentSize].height;
                    CGPoint aPoint = CGPointMake([sp position].x - (w/2), [sp position].y - (h/2));
                    spRect = CGRectMake(aPoint.x, aPoint.y, w, h);
                  
                  
                  
                  
                  
                  
                    CCSprite *sprite2 = [CCSprite spriteWithFile:@"test3.png"];
                    sprite2.position = ccp(100,100);
                    [self addChild:sprite2];
                  
                  
                  
                    //[self registerWithTouchDispatcher];
                    self.isTouchEnabled = YES;
                  
                  
                  
                   }
                   return self;
                  }
                  
                  
                  // on "dealloc" you need to release all your retained objects
                  - (void) dealloc
                  {
                   // in case you have something to dealloc, do it in this method
                   // in this particular example nothing needs to be released.
                   // cocos2d will automatically release all the children (Label)
                  
                   // don't forget to call "super dealloc"
                   [super dealloc];
                  }
                  
                  - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
                    UITouch *touch = [touches anyObject];
                    //CGPoint location = [[CCDirector sharedDirector] convertCoordinate:[touch locationInView:touch.view]];
                   CGPoint location = [touch locationInView:[touch view]];
                  
                   location = [[CCDirector sharedDirector] convertToGL:location];
                   if (CGRectContainsPoint(spRect, location)) {
                    UIAlertView *alert = [[UIAlertView alloc]
                           initWithTitle:@"Win"
                           message:@"testing"
                           delegate:nil cancelButtonTitle:@"okay"
                           otherButtonTitles:nil];
                  
                    [alert show];
                    [alert release];
                    NSLog(@"TOUCHES");
                   }
                   NSLog(@"Touch got");
                  
                  }
                  

                  However, this only works for 1 object, the sprite which I create the CGRect for. I can't do it for 2 sprites, which I was testing. So my question is this: How can I have all sprites on the screen react to the same event when touched?

                  For my program, the same event needs to be run for all objects of the same type, so that should make it a tad easier. I tried making a subclass of CCNode and over write the method, but that just didn't work at all... so I'm doing something wrong. Help would be appreciated!

                  Going through the "Touches" project in cocos2D and seeing if I see how they did it. It looks like they made a subclass and overwrote the methods:

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

                  So now I get to figure out why mine doesn't work... hmm...

                  解决方案

                  I got it. I had to add some more code to the custom class:

                  Header File:

                  //
                  //  CustomCCNode.h
                  //  Coco2dTest2
                  //
                  //  Created by Ethan Mick on 3/11/10.
                  //  Copyright 2010 Wayfarer. All rights reserved.
                  //
                  
                  #import "cocos2d.h"
                  
                  
                  @interface CustomCCNode : CCSprite <CCTargetedTouchDelegate> {
                  
                  }
                  
                  @property (nonatomic, readonly) CGRect rect;
                  
                  
                  @end
                  

                  Implementation:

                  //
                  //  CustomCCNode.m
                  //  Coco2dTest2
                  //
                  //  Created by Ethan Mick on 3/11/10.
                  //  Copyright 2010 Wayfarer. All rights reserved.
                  //
                  
                  #import "CustomCCNode.h"
                  #import "cocos2d.h"
                  
                  
                  @implementation CustomCCNode
                  
                  - (CGRect)rect
                  {
                      CGSize s = [self.texture contentSize];
                      return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
                  }
                  
                  
                  
                  - (BOOL)containsTouchLocation:(UITouch *)touch
                  {
                      return CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]);
                  }
                  
                  - (void)onEnter
                  {
                      [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
                      [super onEnter];
                  }
                  
                  
                  - (void)onExit
                  {
                      [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
                      [super onExit];
                  }   
                  
                  
                  
                  
                  
                  - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
                  {
                      NSLog(@"ccTouchBegan Called");
                      if ( ![self containsTouchLocation:touch] ) return NO;
                      NSLog(@"ccTouchBegan returns YES");
                      return YES;
                  }
                  
                  - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
                  {
                      CGPoint touchPoint = [touch locationInView:[touch view]];
                      touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];
                  
                      NSLog(@"ccTouch Moved is called");
                  }
                  
                  - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
                  {
                      NSLog(@"ccTouchEnded is called");
                  }
                  
                  
                  @end
                  

                  这篇关于如何让对象对 Cocos2D 中的触摸做出反应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)
                  Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)
                  Resume game cocos2d(恢复游戏 cocos2d)
                  Highlight Read-Along Text (in a storybook type app for iPhone)(突出显示朗读文本(在 iPhone 的故事书类型应用程序中))
                  Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)
                  How to convert 32 bit PNG to RGB565?(如何将 32 位 PNG 转换为 RGB565?)
                  • <bdo id='DoSqG'></bdo><ul id='DoSqG'></ul>
                    1. <tfoot id='DoSqG'></tfoot>
                        <tbody id='DoSqG'></tbody>

                      <i id='DoSqG'><tr id='DoSqG'><dt id='DoSqG'><q id='DoSqG'><span id='DoSqG'><b id='DoSqG'><form id='DoSqG'><ins id='DoSqG'></ins><ul id='DoSqG'></ul><sub id='DoSqG'></sub></form><legend id='DoSqG'></legend><bdo id='DoSqG'><pre id='DoSqG'><center id='DoSqG'></center></pre></bdo></b><th id='DoSqG'></th></span></q></dt></tr></i><div id='DoSqG'><tfoot id='DoSqG'></tfoot><dl id='DoSqG'><fieldset id='DoSqG'></fieldset></dl></div>
                      <legend id='DoSqG'><style id='DoSqG'><dir id='DoSqG'><q id='DoSqG'></q></dir></style></legend>

                            <small id='DoSqG'></small><noframes id='DoSqG'>