好的,我开始学习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");
}
然而,这只适用于一个对象,即我为其创建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
现在我需要弄清楚为什么我的不起作用...嗯...