Note that there are some explanatory texts on larger screens.

plurals
  1. POTouch location CCLayer
    primarykey
    data
    text
    <p>*<strong><em>Working Code Now</em>*</strong></p> <p>OK, I thought that this would be easy to get working but it turns out to not work like I expected.</p> <p>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?</p> <p>Interface #import "cocos2d.h"</p> <pre><code>@interface TestTouch : CCLayer { CCLayerColor *layer; } +(CCScene *) scene; @end </code></pre> <p>Implementation</p> <pre><code>#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 </code></pre> <p>If I change this line to include "self":</p> <blockquote> <p>[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];</p> </blockquote> <p>It will obviously work but then I get the location relevant to the screen and not the layer, which is what I need.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload