Ios 创建CCSprite是CCBatchNode子节点的可触摸CCNode时出现问题

Ios 创建CCSprite是CCBatchNode子节点的可触摸CCNode时出现问题,ios,cocos2d-iphone,Ios,Cocos2d Iphone,这可能需要一点解释,但这里说,我真的非常感谢任何见解 简短版本:我如何创建一个TouchableButton(它自己检测触摸),其CCSprite图像是另一个类中CCSpriteBatchNode的子级?(通常CCSprite是TouchableButton本身的子对象) 长版本: 我正在用Cocos2d构建一个游戏。游戏关注的是一个场景(class EnvironmentView:CCLayer),里面充满了代理(class AgentView:CCNode),这些代理在周围运行并相互交互 E

这可能需要一点解释,但这里说,我真的非常感谢任何见解

简短版本:我如何创建一个TouchableButton(它自己检测触摸),其CCSprite图像是另一个类中CCSpriteBatchNode的子级?(通常CCSprite是TouchableButton本身的子对象)

长版本:

我正在用Cocos2d构建一个游戏。游戏关注的是一个场景(class EnvironmentView:CCLayer),里面充满了代理(class AgentView:CCNode),这些代理在周围运行并相互交互

EnvironmentView维护一个AgentView对象列表,并根据需要创建/销毁它们(取决于它们的交互方式)

每个AgentView都有一个CCSprite@属性,该属性作为CCBatchNode的子级添加(EnvironmentView的@property),后者作为EnvironmentView的子级添加

我正在尝试实现一个功能,用户可以触摸代理并将它们从一个地方移动到另一个地方

因为在EnvironmentView中有很多代理在移动,所以我不想使用标准的方法获取触摸位置,并在所有代理视图ccsprite中循环,以查看触摸是否碰到其中一个(请注意,这会大大降低帧速率:对推广此方法的答案不感兴趣)

相反,我希望将每个AgentView设置为一个可触摸节点(一个知道何时触摸到它的节点,而不是一个知道何时触摸到它的节点(上述方法))

基本上,我想用某种触摸按钮对象替换或增强每个AgentView的CCSprite

我正在使用一个类(我们称之为TouchableButton),它将这种方法用于我的游戏中与UI相关的按钮,它们知道何时被触摸,而不在其父层中实现任何CCToucheSBegind方法。但我无法将TouchableButton应用于此用例,原因如下:

TouchableButtons将CCSprite作为初始参数。此CCSprite设置为按钮的可触摸部分,并作为按钮本身的子级添加。由于我还在EnvironmentView中将CCSpriteBatchNode添加为CCSpriteBatchNode的子对象,因此出现了一个错误(无法将某个对象作为子对象添加两次到两个不同的父对象)。我如何组织事情以避免这种冲突


提前感谢您的帮助

我自己也对此做了很多努力,我的结论是这是不可能的,你必须为每个按钮使用单独的图像文件

我希望这个功能会出现在1.0中,但我不认为它会出现


但希望我错了!:)

简单的回答是:你不能

答案很长:你可以得到同样的效果,但方式不同

CCSpriteBatchNode的工作原理是使用公共纹理(sprite sheet)在一个GLDropeElements调用中绘制其所有CCSprite子级,这就是它具有如此好性能的原因。但结果是,每个子对象都必须是一个精灵,如果将子对象添加到精灵中,它将被忽略

因此,在这一点上,您唯一的办法是将CCSprite子类化为一个按钮,并复制许多功能,如:

ButtonSprite.h:

//
//  ButtonSprite.h
//  TestButtonSprite
//
//  Created by Karl Stenerud on 9/1/11.
//

#import "cocos2d.h"

@class ButtonSprite;

typedef void (^ButtonPressCallback)(ButtonSprite* button);

/**
 * A sprite that can respond to touches.
 * Most of this code was taken from CCLayer.
 */
@interface ButtonSprite : CCSprite <CCStandardTouchDelegate, CCTargetedTouchDelegate>
{
    BOOL touchEnabled_;
    int touchPriority_;
    BOOL swallowTouches_;
    BOOL registeredWithDispatcher_;

    BOOL touchInProgress_;
    BOOL buttonWasDown_;

    ButtonPressCallback onButtonPressedCallback_;
}

/** Priority position in which this node will be handled (lower = sooner) */
@property(nonatomic,readwrite,assign) int touchPriority;

/** If true, no other node will respond to touches this one responds to */
@property(nonatomic,readwrite,assign) BOOL swallowTouches;

/** If true, this node responds to touches. */
@property(nonatomic,readwrite,assign) BOOL touchEnabled;

/** Called whenever a full touch completes */
@property(nonatomic,readwrite,copy) ButtonPressCallback onButtonPressedCallback;

/** Called when a button press is detected. */
- (void) onButtonPressed;

/** Called when a button is pushed down. */
- (void) onButtonDown;

/** Called when a button is released. */
- (void) onButtonUp;

- (BOOL) touchHitsSelf:(UITouch*) touch;

- (BOOL) touch:(UITouch*) touch hitsNode:(CCNode*) node;

@end
像这样使用它:

    ButtonSprite* myButton = [ButtonSprite spriteWithFile:@"button_image.png"];
    myButton.onButtonPressedCallback = ^(ButtonSprite* button)
    {
        NSLog(@"Pressed!");
    };
    [self addChild: myButton];

请注意,如果在批处理节点中使用该类,则它不能有自己的任何子级

+1用于详细说明和格式!
    ButtonSprite* myButton = [ButtonSprite spriteWithFile:@"button_image.png"];
    myButton.onButtonPressedCallback = ^(ButtonSprite* button)
    {
        NSLog(@"Pressed!");
    };
    [self addChild: myButton];