coordinates CCTouch Obj-C - cocos2d-iphone

How do I get the coordinates of an touch event in objective-c using cocos2d?
- (void)touchBegan:(CCTouch *)touch withEvent:(CCTouchEvent *)event
{
CCLOG(#"Received a touchBegan");
}

CCTouch (currently missing in the class reference but will be added soon) has methods to get the touch locations:
// position in scene coordinates
CGPoint touchPos = [touch locationInWorld];
// touch position relative to anyNode's position
CGPoint touchPosNode = [touch locationInNode:anyNode];
Tip: In Xcode right-click a keyword such as CCTouch and select Jump to Definition and it will bring you to the class interface which at least shows you the available properties/methods and for most of them you'll also find associated reference documentation as comments.

Related

CCSprite detection via touch without transparent pixel

I am trying to detect a touch on a CCSprite using:
#implementation MainScene{
CCPhysicsNode *_levelView;
}
- (void)touchBegan:(CCTouch *)touch withEvent:(CCTouchEvent *)event{
CGPoint location = touch.locationInWorld;
CCSprite *clickedSprite;
for (CCSprite *obj in _levelView.children)
{
if (CGRectContainsPoint(obj.boundingBox, location))
clickedSprite = obj;
}
}
which works perfectly fine.
So I can detect clicked Sprites already.
Since I am using sprites which are transparent at some parts, they are also detected when the transparent part of the sprite is clicked.
But I want to exclude the transparent part from the detection…
For the physics I am using physics shape polygon. Is there a way to use this polygon for something like:
polygonContainsPoint(obj.physicsPolygon, location)
? Or maybe even a way saying all pixels but those with transparency 100? Or an even easier solution?

Dragging a sprite with TouchMoved

I'm using Xcode with Cocos2d version 3.0.
I want to drag sprites around the screen. I've done so successfully using the following code:
(void) touchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchLocation = [touch locationInNode:self];
sprite1.position=touchLocation;
sprite2.position=touchLocation;
sprite3.position=touchLocation;
sprite4.position=touchLocation;
}
However, sometimes the sprites stop moving after a second. It's not a lag, because they never catch back up with my movement. They just stop! If I let go and start moving my touch again, the sprites start moving fine again / sometimes do the 'freeze thing' again.
Is it a memory issue?
Ok, I'm sure it must be memory. I copied this code onto a simple game with hardly any sprites and it worked perfectly.
Ok I've got it!
I had to unEnable the UISwipeGestureRecognizers while I moved the sprite.
The game was registering my touchesMoved movement as a swipe, and cancelling the touchesMoved commands.

Cocos2d - Allow touches to be used by multiple classes (sneakyjoystick)

Evening all,
I'll start this question in the time honoured tradition by saying that I've had a good old search on SO and also in the wider world but I'm not quite getting my head around this...
I've implemented a sneakyJoystick which works wonderfully (It moves my sprite around quite happily) however I've now done myself a mischief in thinking about it's positioning.
What I'd like to do is simply change it's position to a touch location and have it move my sprite around but this seems to be out of my knowledge pool. I might be being an idiot but I cannot work it out.
The touch events are already sorted in the sneakyjoystick classes (available on github https://github.com/0xPr0xy/sneaky-joystick-cocos2d). At the moment if I create the joystick during the init method in a class called controlsLayer then everything works fine; Joystick appears and it allows me to move to sprite around
-(id) init
{
if( (self=[super init]) ) {
myJoystickBase = [[[SneakyJoystickSkinnedBase alloc] init] autorelease];
myJoystickBase.backgroundSprite = [CCSprite spriteWithFile:#"dpad.png"];
myJoystickBase.thumbSprite = [CCSprite spriteWithFile:#"joystick.png"];
myJoystickBase.joystick = [[SneakyJoystick alloc] initWithRect:CGRectMake(0, 0, 128, 128)];
myJoystickBase.position = ccp(64, 64);
myJoystickBase.backgroundSprite.opacity = 100;
myJoystickBase.thumbSprite.opacity = 100;
[self addChild:myJoystickBase];
myJoystick = [myJoystickBase.joystick retain];
[self scheduleUpdate];
}
return self;
}
So to begin with I thought about looking at how I could simply get it to show and hide itself and set it's location. To do this I created a ccTouchesbegan method which contains pretty much the same code as my init method did before. This works fine up to a point (the joystick appears centred wherever I touch) but the issue now is that I cannot interact with it. The joystick appears where i want but it will not recognise my movements (the stick on the joystick does not move which in turn means that my sprite is not being told to move either)
if( (self=[super init]) ) {
self.isTouchEnabled = YES;
[self scheduleUpdate];
}
return self;
}
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
location = [self convertToNodeSpace:location];
myJoystickBase = [[[SneakyJoystickSkinnedBase alloc] init] autorelease];
myJoystickBase.backgroundSprite = [CCSprite spriteWithFile:#"dpad.png"];
myJoystickBase.thumbSprite = [CCSprite spriteWithFile:#"joystick.png"];
myJoystickBase.joystick = [[SneakyJoystick alloc] initWithRect:CGRectMake(0, 0, 128, 128)];
myJoystickBase.position = location;
myJoystickBase.backgroundSprite.opacity = 100;
myJoystickBase.thumbSprite.opacity = 100;
[self addChild:myJoystickBase];
myJoystick = [myJoystickBase.joystick retain];
}
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self removeChild:myJoystickBase cleanup:YES];
}
So to my uninitiated brain this is saying that when I touch the controlsLayer class it happily does what I've asked it to but it will not then pass this touch to other classes.
In a nutshell can a touch event be passed to multiple classes at the same time? The sneaky joystick class uses CCTargetedTouchDelegate which worked fine when it was the only thing looking for a touch. However now that I've added a -(void)ccTouchesBegan: in another class it's not happy.
Can anyone tell me if the problem is with the way I'm handling touches or is it possibly an issue with the way that I allocate the joystick in the touch method? Should I be allocating the joystick in the init method and be doing something else in the touchesBegan method? Trial and error isn't getting me anywhere useful at the moment. Feel like I'm banging my head against a brick wall. I'm happy to post up the full class files if necessary. Does this make sense?
The problem is that you create the joystick during touch began, and remove it on touch ended. This does not allow the joystick to receive a touch began event, since the event is already being processed at the time the joystick is being created.
You're also leaking the joystick due to the unnecessary retain (use ARC, please!).
Try creating the joystick in init, disable & hide it until a touch began event is received. This is also faster than recreating the joystick on every touch.
Also, should you have multiple touches enabled, keep in mind that you can receive up to 5 touch began events (5 fingers) without any touch ended in between. That would create 5 joysticks, but only remove one! Every time!

CCDirector setDeviceOrientation into cocos2d v2.0rc1

before to update my cocos2d 1.0.1 to 2.0rc1 in my app, I was calling [[CCDirector sharedDirector] setDeviceOrientation:(ccDeviceOrientation)currentOrientation]; into some scenes when detected one change on orientation and then load other scene in portrait or lanscape depending the currentOrientation.
But now I don't have any idea about how I can do it.
Orientation setOrientation ( Orientation orientation )
Callback by CCDirector for change device orientation.
The defination of orientation which CCDirector want change to.
Returns
The actual orientation of the application.
Open AppDelegate.m and locate this method:
// Supported orientations: Landscape. Customize it for your own needs
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
Return YES for all the orientations your app supports.

cocos2d : why convertToGL, and why addChild CCSprite before its body?

i'm starting to learn cocos2d, and i was wondering why actually we use convertToGL from CCDirector, [[CCDirector sharedDirector] convertToGL: touchLoc]; when we already have the [touch view] from [touch locationInView: [touch view]];
?
Also, we first "addChild" a CCSprite, containing an image, and then we set its body, and the ccsprite becomes the data user of this body. Would'nt it better to "addChild" the body instead? or is it for any particular purpose?
Thanks
This is because the OpenGL View (EAGLView class) uses a different coordinate system than Cocoa Touch does.
For example, the 0,0 position for Cocoa Touch is on the upper left corner, whereas the 0,0 position for OpenGL is in the lower left corner. That's why you need to "convert to GL" all UIView coordinates.
The conversion also takes into account the current device orientation.