如何让对象对Cocos2D中的触摸做出反应? [英] How to get objects to react to touches in Cocos2D?

查看:110
本文介绍了如何让对象对Cocos2D中的触摸做出反应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我开始学习更多关于Coco2D,但我有点frusterated。很多教程我找到的是过时的代码版本,所以当我看看他们如何做某些事情,我不能把它翻译成我自己的程序,因为很多已经改变。有人说,我在最新版本的Coco2d,版本0.99。

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.

我想做的是在屏幕上创建一个精灵(完成),然后当我触摸那个sprite,我可以有事情发生。现在,让我们做一个警报。现在,我得到这个代码在一个朋友的帮助下工作。这是头文件:

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

这里是实现文件:

//
// 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");

}

但是,这只适用于1个对象,我创建的CGRect为。我不能做2个精灵,我正在测试。所以我的问题是这样的:如果我触摸屏幕上的所有精灵在屏幕上对同一个事件做出反应?

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?

对于我的程序,需要运行相同的事件所有对象的相同类型,所以应该使它更容易一点。我试图做一个CCNode的子类,然后写的方法,但那只是没有工作在所有...所以我做错了。

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!

通过cocos2D中的Touches项目,看看我是否看到他们做了。看起来他们做了一个子类,并重写了方法:

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

现在我想出了为什么我的工作不工作... hmm ...

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:

头文件:

//
//  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

实施:

//
//  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中的触摸做出反应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆