簡體   English   中英

如何讓對象對Cocos2D中的觸摸作出反應?

[英]How to get objects to react to touches in Cocos2D?

好吧,所以我開始學習更多關於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

我是這方面的乞丐,但這個文件給我帶來了很多麻煩,我現在明白了很多東西,正確實例化的方法是:(至少我是怎么做的)

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

打開控制台,查看觸發事件觸發時發送的消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM