gpt4 book ai didi

ios - 如何让对象对 Cocos2D 中的触摸使用react?

转载 作者:可可西里 更新时间:2023-11-01 03:09:15 26 4
gpt4 key购买 nike

好吧,我开始学习更多关于 Coco2D 的知识,但我有点沮丧。我找到的很多教程都是针对过时版本的代码,所以当我浏览并了解他们如何做某些事情时,我无法将其转化为我自己的程序,因为很多已经改变了。话虽如此,我正在使用最新版本的 Coco2d,版本 0.99。

我想做的是在屏幕上创建一个 Sprite (完成),然后当我触摸那个 Sprite 时,我可以让“事情”发生。现在,让我们关闭警报。现在,我在 friend 的帮助下得到了这段代码。这是头文件:

// 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 的 Sprite 。我不能为我正在测试的 2 个 Sprite 做这件事。所以我的问题是:如何让屏幕上的所有 Sprite 在触摸时对同一事件使用react?

对于我的程序,需要为所有相同类型的对象运行相同的事件,这样应该会更容易一些。我尝试创建 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 中的触摸使用react?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2437010/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com