2010-03-13 29 views
8

Được rồi, vì vậy tôi bắt đầu tìm hiểu thêm về Coco2D, nhưng tôi hơi bối rối. Rất nhiều hướng dẫn tôi đã tìm thấy là các phiên bản lỗi thời của mã, vì vậy khi tôi xem qua và xem cách họ làm những điều nhất định, tôi không thể dịch nó thành chương trình của riêng mình, bởi vì rất nhiều thay đổi. Với điều đó đang được nói, tôi đang làm việc trong phiên bản mới nhất của Coco2d, phiên bản 0.99.Làm thế nào để các đối tượng phản ứng với các chạm trong Cocos2D?

Những gì tôi muốn làm là tạo một sprite trên màn hình (Xong) và sau đó khi tôi chạm vào sprite đó, tôi có thể có "cái gì đó" xảy ra. Để bây giờ, chúng ta hãy làm cho một cảnh báo đi tắt. Bây giờ, tôi nhận được mã này làm việc với sự giúp đỡ của một người bạn. Dưới đây là file header:

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

Và đây là tập tin thực hiện:

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

} 

Tuy nhiên, điều này chỉ làm việc cho 1 đối tượng, ma mà tôi tạo ra CGRect cho. Tôi không thể làm điều đó cho 2 sprites, mà tôi đã thử nghiệm. Vì vậy, câu hỏi của tôi là: Làm thế nào tôi có thể có tất cả các sprites trên màn hình phản ứng với cùng một sự kiện khi chạm vào?

Đối với chương trình của tôi, cùng một sự kiện cần được chạy cho tất cả các đối tượng cùng loại, do đó việc này sẽ dễ dàng hơn. Tôi đã cố gắng làm cho một lớp con của CCNode và hơn viết phương pháp, nhưng điều đó chỉ không làm việc ở tất cả ... vì vậy tôi đang làm một cái gì đó sai. Trợ giúp sẽ được đánh giá cao!

Đi qua dự án "Chạm" trong cocos2D và xem tôi có thấy chúng hoạt động như thế nào không. Dường như họ đã làm cho một lớp con và ghi đè lên các phương pháp:

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

Vì vậy, bây giờ tôi có được để tìm ra lý do tại sao tôi không làm việc ... hmm ...

Trả lời

11

tôi đã nhận nó. Tôi đã có thêm một số mã khác để các lớp tùy chỉnh:

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 

Thực hiện:

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

vì vậy làm cách nào để bạn nhanh chóng tạo lớp mới này bằng tệp png chẳng hạn? –

+0

Bạn vẫn cần biết? Đây chỉ là một phân lớp của CCSprite, vì vậy: CCSprite * aSprite = [CCSprite spriteWIthTexture: [Thêm png vào đây với CCTexture]]; –

+2

Bạn cũng có thể sử dụng thuộc tính boundingBox thay vì phương thức rect tùy chỉnh. –

-2

tôi là một mới bắt đầu tại đây, nhưng tập tin này lưu lại cho tôi rất nhiều của rắc rối, bây giờ tôi hiểu nhiều thứ hơn, và cách để tạo ra điều này đúng là: (ít nhất là tôi đã làm thế nào)

+(id)scene{ 
    CustomCCNode *myNode = [CustomCCNode spriteWithFile:@"myPng.png"]; 
    myNode.position = ccp(150,150); 
    [scene addChild:myNode]; 
    return scene; 
} 

Mở bàn điều khiển và xem các tin nhắn gửi khi các sự kiện cảm ứng được kích hoạt.

Các vấn đề liên quan