触摸位置CCLayer

发布于 2024-12-09 19:36:04 字数 1730 浏览 1 评论 0原文

*现在工作代码*

好吧,我认为这很容易工作,但事实证明它并不像我预期的那样工作。

我正在尝试从可以移动或缩放的 CCLayer 获取触摸位置,而不是屏幕本身上的位置?这就是我认为它会起作用但它崩溃了的方式?

界面 #import "cocos2d.h"

@interface TestTouch : CCLayer {
   CCLayerColor *layer;
}

+(CCScene *) scene;


@end

实施

#import "TestTouch.h"

@implementation TestTouch

+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];

// 'layer' is an autorelease object.
TestTouch *layer = [TestTouch node];

// add layer as a child to scene
[scene addChild: layer];

// return the scene
return scene;
}

- (id)init
{
    self = [super init];
    if (self) {
        CGSize winsize = [[CCDirector sharedDirector]winSize];
        CCLayerColor *layer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)];
//        layer.scale = 0.7f;
        layer.contentSize = CGSizeMake(640, 960);
        layer.position = CGPointMake(winsize.width/2, winsize.height/2);
        layer.isRelativeAnchorPoint = YES;
        [self addChild:layer z:0];
        [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:layer priority:0 swallowsTouches:YES];
    }

    return self;
}

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint touchStart = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
    touchStart = [layer convertToNodeSpace:touchStart];
    NSLog(@"Touch:%f,%f",touchStart.x, touchStart.y);

    return YES;
}

@end

如果我更改此行以包含“self”:

[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self 优先级:0 SwallowsTouches:YES];

它显然可以工作,但随后我会得到与屏幕相关的位置,而不是与图层相关的位置,这正是我所需要的。

*Working Code Now*

OK, I thought that this would be easy to get working but it turns out to not work like I expected.

I am trying to get the Touch location from a CCLayer that can be moved or zoomed, not the location on the screen itself? here is how I thought that it would work but it crashes?

Interface
#import "cocos2d.h"

@interface TestTouch : CCLayer {
   CCLayerColor *layer;
}

+(CCScene *) scene;


@end

Implementation

#import "TestTouch.h"

@implementation TestTouch

+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];

// 'layer' is an autorelease object.
TestTouch *layer = [TestTouch node];

// add layer as a child to scene
[scene addChild: layer];

// return the scene
return scene;
}

- (id)init
{
    self = [super init];
    if (self) {
        CGSize winsize = [[CCDirector sharedDirector]winSize];
        CCLayerColor *layer = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)];
//        layer.scale = 0.7f;
        layer.contentSize = CGSizeMake(640, 960);
        layer.position = CGPointMake(winsize.width/2, winsize.height/2);
        layer.isRelativeAnchorPoint = YES;
        [self addChild:layer z:0];
        [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:layer priority:0 swallowsTouches:YES];
    }

    return self;
}

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint touchStart = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
    touchStart = [layer convertToNodeSpace:touchStart];
    NSLog(@"Touch:%f,%f",touchStart.x, touchStart.y);

    return YES;
}

@end

If I change this line to include "self":

[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];

It will obviously work but then I get the location relevant to the screen and not the layer, which is what I need.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

著墨染雨君画夕 2024-12-16 19:36:05

您需要将屏幕上的位置转换为图层的“节点空间”:

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint touchStart = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];

    // convert touch location to layer space
    touchStart = [self convertToNodeSpace:touchStart];

    NSLog(@"Touch:%f,%f",touchStart.x, touchStart.y);

    return YES;
}

You need to convert the location on the screen to the layer's "node space":

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint touchStart = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];

    // convert touch location to layer space
    touchStart = [self convertToNodeSpace:touchStart];

    NSLog(@"Touch:%f,%f",touchStart.x, touchStart.y);

    return YES;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文