ios – 如何让对象对Cocos2D中的触摸作出反应?

ios – 如何让对象对Cocos2D中的触摸作出反应?,第1张

概述好吧,所以我开始更多地了解Coco2D,但我有点冷漠.我发现的许多教程都是针对代码的过时版本,因此当我查看并查看它们如何执行某些 *** 作时,我无法将其转换为我自己的程序,因为已经发生了很多变化.话虽如此,我正在使用最新版本的Coco2d,版本0.99. 我想要做的是在屏幕上创建一个精灵(完成)然后当我触摸那个精灵时,我可以发生“某事”.现在,让我们发出警报.现在,我在朋友的帮助下得到了这段代码.这是头 好吧,所以我开始更多地了解Coco2D,但我有点冷漠.我发现的许多教程都是针对代码的过时版本,因此当我查看并查看它们如何执行某些 *** 作时,我无法将其转换为我自己的程序,因为已经发生了很多变化.话虽如此,我正在使用最新版本的Coco2d,版本0.99.

我想要做的是在屏幕上创建一个精灵(完成)然后当我触摸那个精灵时,我可以发生“某事”.现在,让我们发出警报.现在,我在朋友的帮助下得到了这段代码.这是头文件:

// 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个精灵做这个,我正在测试.所以我的问题是:如何触摸屏幕上的所有精灵对同一事件作出反应?

对于我的程序,需要为相同类型的所有对象运行相同的事件,这样才能使它更容易.我尝试创建CCNode的子类并重写该方法,但这根本不起作用…所以我做错了.帮助将不胜感激!

通过cocos2D中的“touches”项目,看看我是否看到了它们是如何做到的.看起来他们创建了一个子类并覆盖了这些方法:

- (BOol)cctouchBegan:(UItouch *)touch withEvent:(UIEvent *)event- (voID)cctouchmoved:(UItouch *)touch withEvent:(UIEvent *)event- (voID)cctouchended:(UItouch *)touch withEvent:(UIEvent *)event

所以现在我弄明白为什么我的不行……嗯……

解决方法 我知道了.我不得不在自定义类中添加更多代码:

头文件:

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

以上是内存溢出为你收集整理的ios – 如何让对象对Cocos2D中的触摸作出反应?全部内容,希望文章能够帮你解决ios – 如何让对象对Cocos2D中的触摸作出反应?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1103468.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存